问题
A carer can provide support to two different service at the same Start time and End time this is called 1:2 Service type. The unique ID for this type of shift type are generated as the same unique ID for this type of service. I want to add 2 at the end of the unique ID for every second service. How do I find this? Bare in mind I am using this within the UNION with other 3 select statement. The first query above the Union is the one
My query below:
SELECT DISTINCT
CASE
WHEN SHIFT_CODE = '1:2 Support'
THEN '3'
ELSE ''
END [Datatype Id],
CAST(SS.Schedule_Subshift_ID AS VARCHAR(20)) + ' 1:2' [Unique Visit ID]
'' [Date],
'' [Start Time],
''+ ',' [End Time],
''+ ',,,' [Unique Client ID],
'' [Client Forename]
FROM
Subshift
UNION
SELECT DISTINCT
CASE
WHEN CAST(CAST(EE.Start_Date_SK AS VARCHAR(255)) AS DATE) >= (GETDATE() - 41)
THEN ' 11 '
ELSE ''
END [DataTypeID],
'' [Unique Visit ID],
'' [Date],
'' [Start Time],
''+ ',' [End Time],
''+ ',,,' [Unique Client ID],
'' [Client Forename]
FROM
Subshift
Result I get is:
Service Unique Visit ID Carer Date Start Time End Time SHIFT_PROPERTY_CODE
Moira 211870 2 A 16/12/2019 19:00 22:00, 1:2 Support
Marta 211870 2 A 16/12/2019 19:00 22:00, 1:2 Support
Rebecca 211871 2 B 19/12/2019 19:00 22:00, 1:2 Support
Elsie 211871 2 B 19/12/2019 19:00 22:00, 1:2 Support
Desired Result should be:
Service Unique Visit ID Carer Date Start Time End Time SHIFT_PROPERTY_CODE
Moira 211870 A 16/12/2019 19:00 22:00, 1:2 Support
Marta 211870 2 A 16/12/2019 19:00 22:00, 1:2 Support
Rebecca 211871 B 19/12/2019 19:00 22:00, 1:2 Support
Elsie 211871 2 B 19/12/2019 19:00 22:00, 1:2 Support
回答1:
You can try something along these lines:
SELECT
Case Row_Number() OVER (Partition By SS.Schedule_Subshift_ID Order By ClientName)
when 1 then S.Schedule_Subshift_ID
when 2 then S.Schedule_Subshift_ID + ' 2'
end
FROM Subshift
You can partition by service id and order by time for example, I don't exactly understand your case. Please keep in mind that the Query you copied DOES NOT result in the result sets below. So if you want me to be more specific - please provide the proper query.
来源:https://stackoverflow.com/questions/59787550/how-do-i-put-a-condition-in-the-select-statement-in-t-sql