Answer the question
In order to leave comments, you need to log in
How to collect customer data in one record?
There is a table with customers and the dates of formation and completion of the order, of the form
ID NAME BEGIN END
234 SOKOL 01.02.2020 08.02.2020
145 EVA 03.02.2020 06.02.2020
145 EVA 16.02.2020 20.02.2020
ID NAME BEGIN1 END1 BEGIN2 END2 BEGIN3 END3
234 SOKOL 01.02.2020 08.02.2020
145 EVA 03.02.2020 06.02.2020 16.02.2020 20.02.2020
Answer the question
In order to leave comments, you need to log in
Track portions of unfilled periods.
It is necessary that the request itself collects data for each customer in one line.
DECLARE @T TABLE ( ID INT, [Name] VARCHAR(50), [Begin] DATE, [End] DATE)
INSERT @T VALUES ( 234, 'SOKOL', '01.02.2020', '08.02.2020' ),
(145, 'EVA', '03.02.2020', '06.02.2020'),
(145, 'EVA', '16.02.2020', '20.02.2020')
SELECT ID, [Name],
STRING_AGG( CONVERT(VARCHAR, [Begin], 104) + '~' +
CONVERT(VARCHAR, [End], 104), ', ')
WITHIN GROUP (ORDER BY [Begin] ) AS [Orders]
FROM @T
GROUP BY ID, [Name]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question