Search code examples
sqlsql-servert-sqlselectunique

Query SQL to get all column and min max datetime by MSSQL


I'm using Microsoft SQL Server. And Here is my table enter image description here Added sample data format text as table

╒══════════╤═════════════╤══════════╤═══════╤═════════════╤═════════════════════╤══════════════════╤══════════════════════╤══════════╤═════════════╤═════╤════════════╤════════════╤════════════╤══════════╕
│ RecordID │ ID Employee │ Resource │ Shift │ ProjectID   │ Drawing No          │ Production order │ PN                   │ Quantity │ ProductName │ BNo │ Start Date │ Start Time │ End Date   │ End Time │
╞══════════╪═════════════╪══════════╪═══════╪═════════════╪═════════════════════╪══════════════════╪══════════════════════╪══════════╪═════════════╪═════╪════════════╪════════════╪════════════╪══════════╡
│ 60431    │ 2088        │ M-JO     │ HC    │ E195256-A01 │ No.31               │ MA-000000001     │ SHAFT-DBT-999M       │ 1        │ NULL        │ B01 │ 2020-10-05 │ 13:23:27   │ NULL       │ NULL     │
├──────────┼─────────────┼──────────┼───────┼─────────────┼─────────────────────┼──────────────────┼──────────────────────┼──────────┼─────────────┼─────┼────────────┼────────────┼────────────┼──────────┤
│ 8354     │ 2029        │ M-JO     │ HC    │ E183127-A01 │ VPR180714801/603-F2 │ MA-000001FAB     │ VY1200-DISE-700F     │ 7        │ NULL        │ B01 │ 2019-09-23 │ 09:41:48   │ 2019-09-23 │ 14:38:18 │
├──────────┼─────────────┼──────────┼───────┼─────────────┼─────────────────────┼──────────────────┼──────────────────────┼──────────┼─────────────┼─────┼────────────┼────────────┼────────────┼──────────┤
│ 8408     │ 2058        │ M-MD2.5  │ 1     │ E183127-A01 │ VPR180714801/603-F2 │ MA-000001FAB     │ VY1200-DISE-700F     │ 7        │ NULL        │ B01 │ 2019-09-23 │ 15:32:53   │ 2019-09-23 │ 16:51:19 │
├──────────┼─────────────┼──────────┼───────┼─────────────┼─────────────────────┼──────────────────┼──────────────────────┼──────────┼─────────────┼─────┼────────────┼────────────┼────────────┼──────────┤
│ 50130    │ 2175        │ M-ML1.5  │ HC    │ L190004-A01 │ VS1-0931991         │ MA-000001PHA     │ L190004-A01-051-023C │ 2        │ NULL        │ B01 │ 2020-05-19 │ 15:59:23   │ 2020-05-19 │ 18:06:14 │
├──────────┼─────────────┼──────────┼───────┼─────────────┼─────────────────────┼──────────────────┼──────────────────────┼──────────┼─────────────┼─────┼────────────┼────────────┼────────────┼──────────┤
│ 50231    │ 2175        │ M-ML1.5  │ HC    │ L190004-A01 │ VS1-0931991         │ MA-000001PHA     │ L190004-A01-051-023C │ 2        │ NULL        │ B01 │ 2020-05-20 │ 08:04:39   │ 2020-05-20 │ 16:53:53 │
├──────────┼─────────────┼──────────┼───────┼─────────────┼─────────────────────┼──────────────────┼──────────────────────┼──────────┼─────────────┼─────┼────────────┼────────────┼────────────┼──────────┤
│ 50874    │ 2134        │ M-ML2    │ HC    │ L190004-A01 │ VS1-1633944-00      │ MA-000002PHA     │ L190004-A01-005-023C │ 2        │ NULL        │ B01 │ 2020-05-22 │ 10:11:08   │ 2020-05-22 │ 16:39:43 │
├──────────┼─────────────┼──────────┼───────┼─────────────┼─────────────────────┼──────────────────┼──────────────────────┼──────────┼─────────────┼─────┼────────────┼────────────┼────────────┼──────────┤
│ 51030    │ 2134        │ M-ML2    │ HC    │ L190004-A01 │ VS1-1633944-00      │ MA-000002PHA     │ L190004-A01-005-023C │ 2        │ NULL        │ B01 │ 2020-05-23 │ 08:06:43   │ 2020-05-23 │ 11:38:03 │
├──────────┼─────────────┼──────────┼───────┼─────────────┼─────────────────────┼──────────────────┼──────────────────────┼──────────┼─────────────┼─────┼────────────┼────────────┼────────────┼──────────┤
│ 52063    │ 2134        │ M-ML2    │ HC    │ E203089-A01 │ VS1-1633944-00      │ MA-000003PHA     │ E203089-A01-005-023C │ 1        │ NULL        │ B01 │ 2020-05-28 │ 13:23:48   │ 2020-05-28 │ 18:29:19 │
├──────────┼─────────────┼──────────┼───────┼─────────────┼─────────────────────┼──────────────────┼──────────────────────┼──────────┼─────────────┼─────┼────────────┼────────────┼────────────┼──────────┤
│ 52204    │ 2134        │ M-ML2    │ HC    │ E203089-A01 │ VS1-1633944-00      │ MA-000003PHA     │ E203089-A01-005-023C │ 1        │ NULL        │ B01 │ 2020-05-29 │ 08:05:22   │ 2020-05-29 │ 12:51:25 │
├──────────┼─────────────┼──────────┼───────┼─────────────┼─────────────────────┼──────────────────┼──────────────────────┼──────────┼─────────────┼─────┼────────────┼────────────┼────────────┼──────────┤
│ 8051     │ 2163        │ M-MLV1.6 │ 3     │ E183116-A03 │ VP7R810400/021X1    │ MA-000005198     │ VY0750031-SUCB-819M  │ 1        │ NULL        │ B01 │ 2019-09-21 │ 02:30:14   │ 2019-09-21 │ 06:00:00 │
├──────────┼─────────────┼──────────┼───────┼─────────────┼─────────────────────┼──────────────────┼──────────────────────┼──────────┼─────────────┼─────┼────────────┼────────────┼────────────┼──────────┤
│ 8052     │ 2028        │ M-MLV1.6 │ 1     │ E183116-A03 │ VP7R810400/021X1    │ MA-000005198     │ VY0750031-SUCB-819M  │ 1        │ NULL        │ B01 │ 2019-09-21 │ 08:10:59   │ 2019-09-21 │ 10:00:00 │
├──────────┼─────────────┼──────────┼───────┼─────────────┼─────────────────────┼──────────────────┼──────────────────────┼──────────┼─────────────┼─────┼────────────┼────────────┼────────────┼──────────┤
│ 8100     │ 2029        │ M-JO     │ 2     │ E183116-A03 │ VP7R810400/021X1    │ MA-000005198     │ VY0750031-SUCB-819M  │ 1        │ NULL        │ B01 │ 2019-09-21 │ 14:05:12   │ 2019-09-21 │ 15:36:38 │
├──────────┼─────────────┼──────────┼───────┼─────────────┼─────────────────────┼──────────────────┼──────────────────────┼──────────┼─────────────┼─────┼────────────┼────────────┼────────────┼──────────┤
│ 8222     │ 2058        │ M-MD2.5  │ HC    │ E183116-A03 │ VP7R810400/021X1    │ MA-000005198     │ VY0750031-SUCB-819M  │ 1        │ NULL        │ B01 │ 2019-09-22 │ 11:24:54   │ 2019-09-22 │ 14:43:19 │
├──────────┼─────────────┼──────────┼───────┼─────────────┼─────────────────────┼──────────────────┼──────────────────────┼──────────┼─────────────┼─────┼────────────┼────────────┼────────────┼──────────┤
│ 15490    │ 2228        │ M-MP3    │ 1     │ E183116-A03 │ VP7R810400/021X1    │ MA-000005198     │ VY0750031-SUCB-819M  │ 1        │ NULL        │ B01 │ 2019-10-26 │ 06:51:06   │ 2019-10-26 │ 14:00:50 │
├──────────┼─────────────┼──────────┼───────┼─────────────┼─────────────────────┼──────────────────┼──────────────────────┼──────────┼─────────────┼─────┼────────────┼────────────┼────────────┼──────────┤
│ 15585    │ 2226        │ M-MP3    │ 2     │ E183116-A03 │ VP7R810400/021X1    │ MA-000005198     │ VY0750031-SUCB-819M  │ 1        │ NULL        │ B01 │ 2019-10-26 │ 14:03:21   │ 2019-10-26 │ 19:58:07 │
├──────────┼─────────────┼──────────┼───────┼─────────────┼─────────────────────┼──────────────────┼──────────────────────┼──────────┼─────────────┼─────┼────────────┼────────────┼────────────┼──────────┤
│ 15979    │ 2034        │ M-MD3    │ 1     │ E183116-A03 │ VP7R810400/021X1    │ MA-000005198     │ VY0750031-SUCB-819M  │ 1        │ NULL        │ B01 │ 2019-10-29 │ 09:08:52   │ 2019-10-29 │ 13:17:48 │
├──────────┼─────────────┼──────────┼───────┼─────────────┼─────────────────────┼──────────────────┼──────────────────────┼──────────┼─────────────┼─────┼────────────┼────────────┼────────────┼──────────┤
│ 16056    │ 2212        │ M-MLV1.4 │ 1     │ E183116-A03 │ VP7R810400/021X1    │ MA-000005198     │ VY0750031-SUCB-819M  │ 1        │ NULL        │ B01 │ 2019-10-29 │ 14:23:47   │ 2019-10-29 │ 17:51:46 │
├──────────┼─────────────┼──────────┼───────┼─────────────┼─────────────────────┼──────────────────┼──────────────────────┼──────────┼─────────────┼─────┼────────────┼────────────┼────────────┼──────────┤
│ 16136    │ 2087        │ M-MLV1.4 │ 3     │ E183116-A03 │ VP7R810400/021X1    │ MA-000005198     │ VY0750031-SUCB-819M  │ 1        │ NULL        │ B01 │ 2019-10-29 │ 19:08:29   │ 2019-10-30 │ 05:53:22 │
├──────────┼─────────────┼──────────┼───────┼─────────────┼─────────────────────┼──────────────────┼──────────────────────┼──────────┼─────────────┼─────┼────────────┼────────────┼────────────┼──────────┤
│ 16188    │ 2212        │ M-MLV1.4 │ 1     │ E183116-A03 │ VP7R810400/021X1    │ MA-000005198     │ VY0750031-SUCB-819M  │ 1        │ NULL        │ B01 │ 2019-10-30 │ 06:09:27   │ 2019-10-30 │ 08:22:06 │
├──────────┼─────────────┼──────────┼───────┼─────────────┼─────────────────────┼──────────────────┼──────────────────────┼──────────┼─────────────┼─────┼────────────┼────────────┼────────────┼──────────┤
│ 16445    │ 2058        │ M-MD2.5  │ 3     │ E183116-A03 │ VP7R810400/021X1    │ MA-000005198     │ VY0750031-SUCB-819M  │ 1        │ NULL        │ B01 │ 2019-10-30 │ 20:59:08   │ 2019-10-30 │ 22:29:19 │
├──────────┼─────────────┼──────────┼───────┼─────────────┼─────────────────────┼──────────────────┼──────────────────────┼──────────┼─────────────┼─────┼────────────┼────────────┼────────────┼──────────┤
│ 16456    │ 2087        │ M-MLV1.4 │ 3     │ E183116-A03 │ VP7R810400/021X1    │ MA-000005198     │ VY0750031-SUCB-819M  │ 1        │ NULL        │ B01 │ 2019-10-31 │ 00:05:14   │ 2019-10-31 │ 03:05:41 │
╘══════════╧═════════════╧══════════╧═══════╧═════════════╧═════════════════════╧══════════════════╧══════════════════════╧══════════╧═════════════╧═════╧════════════╧════════════╧════════════╧══════════╛

I tried to get unique Production order, Min start date, start time and Max end date and end time like

SELECT
        [Production order], MIN( CAST([Start Date] AS DATETIME) + CAST([Start Time] AS DATETIME))   AS MIN_DATE_TIME, MAX(CAST([End Date] AS DATETIME) + CAST([End Time] AS DATETIME)) AS MAX_DATE_TIME
    FROM
        TIMEDATA1
    WHERE RESOURCE not like 'I-%'
    GROUP BY
        [Production order]
    order by 
        [Production order] ASC

enter image description here

However, There are only Production order, MIN_DATE_TIME, MAX_DATE_TIME column. That I can query. I tried with query

   SELECT
   distinct [Production order] ,PN, ProjectID,[Drawing No], MIN( CAST([Start Date] AS DATETIME) + CAST([Start Time] AS DATETIME))   AS MIN_DATE_TIME, MAX(CAST([End Date] AS DATETIME) + CAST([End Time] AS DATETIME)) AS MAX_DATE_TIME
FROM
    TIMEDATA1
WHERE RESOURCE not like 'I-%'
GROUP BY
    [Production order]
order by 
    [Production order] ASC

It showed error like this enter image description here

I expect to get more column such as PN, ProjectID, Drawing No

Please help me, Thank you so much !


Solution

  • Include the additional columns in the GROUP BY:

    SELECT [Production order], PN, ProjectID,[ Drawing No],
           MIN( CAST([Start Date] AS DATETIME) + CAST([Start Time] AS DATETIME)) AS MIN_DATE_TIME,
           MAX(CAST([End Date] AS DATETIME) + CAST([End Time] AS DATETIME)) AS MAX_DATE_TIME
    FROM TIMEDATA1
    WHERE RESOURCE not like 'I-%'
    GROUP BY [Production order], PN, ProjectID,[ Drawing No]
    ORDER BY [Production order] ASC;
    

    All non-aggregated columns should be in the GROUP BY.

    If this returns multiple rows per production order, that is because the values are different. You need to specify what you want to do in such a situation. If this is an issue, I would recommend that you ask a new question, providing sample data, desired results, and a clear explanation of what to do. The data should be text tables not images.