I have follow statement:
select ID,
Systemname,
Mode,
Evtdate,
Sattus,
Desc
From dbo.MainTasksStatus;
ID SystemName Mode EvtDate Status Desc
----------------------------------------------------------
125 Almo Mode1 9/29/2023 9:11:00 TRUE Complete All
126 Almo Mode2 9/28/2023 11:14:00 FALSE Not Complete
127 Almo Mode3 9/29/2023 10:28:00 TRUE Complete Partial
237 Dido Mode2 9/27/2023 8:14:00 TRUE Complete All
238 Dido Mode3 9/28/2023 12:48:00 TRUE Complete Partial
315 Faroon Mode1 9/27/2023 9:11:00 FALSE Not Complete
316 Faroon Mode3 9/28/2023 15:22:00 TRUE Complete All
As you can see, the records are based on the systemName. Systems are related to three Modes(Mode1,Mode2,Mode3) . some systems have all three modes and some have some of them.
I want select to produce the following output
In the search, I noticed that this is done with pivot, but I don't know exactly how to create this output.
1条答案
按热度按时间oxcyiej71#
You can do it with finding out what the names are and then left join a record from each mode to them.
Test sample:
Query:
Fiddle: http://sqlfiddle.com/#!18/2e215/13
Result:
One can also run a single scan with conditional aggregation as @Thom A pointed out in the comment section. I have implemented and tested the idea, reaching this query:
See: http://sqlfiddle.com/#!18/2e215/14