Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

How to get unique row, all column and min max datetime by query on SQL Server

Query in Microsoft SQL Server

select * 
from TIMEDATA1 
order by [Production order] asc

I get data like this:

╔══════════╤═════════════╤══════════╤═══════╤═════════════╤════════════╤══════════════════╤══════════════════════╤══════════╤═════════════╤═════╤════════════╤════════════╤════════════╤══════════╗
║ 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 │ 1A         │ MA-000000001     │ SHAFT-DBT-999M       │ 1        │ NULL        │ B01 │ 2020-10-05 │ 13:23:27   │ NULL       │ NULL     ║
╟──────────┼─────────────┼──────────┼───────┼─────────────┼────────────┼──────────────────┼──────────────────────┼──────────┼─────────────┼─────┼────────────┼────────────┼────────────┼──────────╢
║ 8354     │ 2029        │ M-JO     │ HC    │ E183127-A01 │ 2A         │ MA-000001FAB     │ VY1200-DISE-700F1    │ 7        │ NULL        │ B01 │ 2019-09-23 │ 09:41:48   │ 2019-09-23 │ 14:38:18 ║
╟──────────┼─────────────┼──────────┼───────┼─────────────┼────────────┼──────────────────┼──────────────────────┼──────────┼─────────────┼─────┼────────────┼────────────┼────────────┼──────────╢
║ 8408     │ 2058        │ M-MD2.5  │ 1     │ E183127-A02 │ 3A         │ MA-000001FAB     │ VY1200-DISE-700F2    │ 7        │ NULL        │ B01 │ 2019-09-23 │ 15:32:53   │ 2019-09-23 │ 16:51:19 ║
╟──────────┼─────────────┼──────────┼───────┼─────────────┼────────────┼──────────────────┼──────────────────────┼──────────┼─────────────┼─────┼────────────┼────────────┼────────────┼──────────╢
║ 50130    │ 2175        │ M-ML1.5  │ HC    │ L190004-A01 │ 4A         │ 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-A02 │ 4B         │ 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-A07 │ 5C         │ 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-A08 │ 5C         │ 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-A05 │ 2AW        │ 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 │ 1A         │ 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 │ Q          │ MA-000005198     │ VY0750031-SUCB-819M1 │ 1        │ NULL        │ B01 │ 2019-09-21 │ 02:30:14   │ 2019-09-21 │ 06:00:00 ║
╟──────────┼─────────────┼──────────┼───────┼─────────────┼────────────┼──────────────────┼──────────────────────┼──────────┼─────────────┼─────┼────────────┼────────────┼────────────┼──────────╢
║ 8052     │ 2028        │ M-MLV1.6 │ 1     │ E183116-A02 │ P          │ 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 │ X          │ MA-000005198     │ VY0750031-SUCB-819M  │ 1        │ NULL        │ B01 │ 2019-09-21 │ 14:05:12   │ 2019-09-21 │ 15:36:38 ║
╚══════════╧═════════════╧══════════╧═══════╧═════════════╧════════════╧══════════════════╧══════════════════════╧══════════╧═════════════╧═════╧════════════╧════════════╧════════════╧══════════╝

I tried with query

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;

However It return multiple rows per production order. I expect to query to have table as

╔══════════════════╤══════════════════════╤═════════════╤════════════╤═════════════════════════╤═════════════════════════╗
║ Production order │ PN                   │ ProjectID   │ Drawing No │ MIN_DATE_TIME           │ MAX_DATE_TIME           ║
╠══════════════════╪══════════════════════╪═════════════╪════════════╪═════════════════════════╪═════════════════════════╣
║ MA-000000001     │ SHAFT-DBT-999M       │ E195256-A01 │ 1A         │ 2020-10-05 13:23:27.000 │ NULL                    ║
╟──────────────────┼──────────────────────┼─────────────┼────────────┼─────────────────────────┼─────────────────────────╢
║ MA-000001FAB     │ VY1200-DISE-700F1    │ E183127-A01 │ 2A         │ 2019-09-23 09:41:48.000 │ 2019-09-23 16:51:19.000 ║
╟──────────────────┼──────────────────────┼─────────────┼────────────┼─────────────────────────┼─────────────────────────╢
║ MA-000001PHA     │ L190004-A01-051-023C │ L190004-A01 │ 4A         │ 2020-05-19 15:59:23.000 │ 2020-05-20 16:53:53.000 ║
╟──────────────────┼──────────────────────┼─────────────┼────────────┼─────────────────────────┼─────────────────────────╢
║ MA-000002PHA     │ L190004-A01-005-023C │ L190004-A07 │ 5C         │ 2020-05-22 10:11:08.000 │ 2020-05-23 11:38:03.000 ║
╟──────────────────┼──────────────────────┼─────────────┼────────────┼─────────────────────────┼─────────────────────────╢
║ MA-000003PHA     │ E203089-A01-005-023C │ E203089-A01 │ 2AW        │ 2020-05-28 13:23:48.000 │ 2020-05-29 12:51:25.000 ║
╟──────────────────┼──────────────────────┼─────────────┼────────────┼─────────────────────────┼─────────────────────────╢
║ MA-000005198     │ VY0750031-SUCB-819M1 │ E183116-A03 │ Q          │ 2019-09-21 02:30:14.000 │ 2019-10-21 15:36:38.000 ║
╚══════════════════╧══════════════════════╧═════════════╧════════════╧═════════════════════════╧═════════════════════════╝

Note: Production order should be unique value. PN, ProjectID, Drawing No should be unique value also .That can get from first row or any.

MIN_DATE_TIME is MIN( CAST([Start Date] AS DATETIME) + CAST([Start Time] AS DATETIME))

MAX_DATE_TIME is MAX(CAST([End Date] AS DATETIME) + CAST([End Time] AS DATETIME))

I had a question similar like this from Query SQL to get all column and min max datetime by MSSQL

I try with result of answer however @Gordon Linoff mentioned this returns multiple rows per production order, that is because the values are different Please help me. Thank you !

EDIT

Data type

Start Date: date

End Date: date

Start Time: time(0)

End Time: time(0)

I know this report is poor report design. But I make it for other purpose

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I need to know the data type for your date and time columns to know the best way to finish the last two columns. Since that's not in the question, here's the rest of it, which is enough to get you started:

select [Production order]
   , MIN(PN) as PN
   , MIN(ProjectID) as ProjectID
   , MIN([Drawing No]) as [Drawing No]
from TIMEDATA1 
group by [Production order]
order by [Production order] asc

I also noticed this:

PN, ProjectID, Drawing No should be unique value also .That can get from first row or any.

This is also a common requirement, but it's really poor report design, because it's very easy to end up showing data that doesn't make sense.

For example, looking at the sample result data we see these values for production order, drawing, and end time:

MA-000001FAB, 2A, 2019-09-23 16:51:19.000

However, looking at the original data for drawing 2A we can see this end time is not associated with that drawing. In other words, this is data that doesn't make any sense to show side-by-side. It's your job to know this and either push back against that kind of requirement or understand how to expand it to show the correct row.

If you must settle for this poor requirement, it's better to do something like this:

select *
from (
    select [Production order], PN, ProjectID, [Drawing No]
        , row_number() over (partition by [production order] order by [production order], end_date, end_time) as rn
    from TIMEDATA1 
) t1
where rn = 1
order by [production order]

This way, at least all of the random values will come from the same row, and it will be the row that matches the min end date and time.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...