Please, consider the following scenario. There are 2 tables: Core and History. Core looks as follows
HWDateStart | HWDateEnd | HWQueueID |
2022-05-29 10:00:00 | 2022-06-04 00:45:00 | WIN-S671INNTGRE.P00-K0-01 |
2022-05-29 10:00:00 | 2022-06-04 00:45:00 | WIN-S671INNTGRE.P00-K0-02 |
History looks as follows
cntDatetime | cntSerialNumber | cntQueueName | cntQueueID |
2022-05-29 02:28:00 | SN01-01 | p00-0000-01 | WIN-S671INNTGRE.P00-K0-01 |
2022-05-29 02:28:00 | SN02-01 | p00-0000-02 | WIN-S671INNTGRE.P00-K0-02 |
2022-06-04 00:26:00 | SN02-02 | p00-0000-02 | WIN-S671INNTGRE.P00-K0-02 |
2022-06-04 00:26:00 | SN01-01 | p00-0000-01 | WIN-S671INNTGRE.P00-K0-01 |
History holds a list of cntSerialNumber changes through the time The following script that gives the user SerialNumber as it was at the beginning and the end of a certain period
SELECT Convert(date,[HWDateStart])
,Convert(date,[HWDateEnd])
,[HWQueueID]
, HS.cntSerialNumber
, HE.cntSerialNumber
FROM [watchdocstats].[dbo].[tblJT] SNTarget,
[watchdocstats].[dbo].[tblQueueByConfig] HS,
[watchdocstats].[dbo].[tblQueueByConfig] HE
WHERE
SNTarget.HWQueueID = HS.cntQueueID
AND SNTarget.HWQueueID = HE.cntQueueID
AND Convert(date,SNTarget.HWDateStart) = Convert(date,HS.cntDatetime)
AND Convert(date,SNTarget.HWDateEnd) = Convert(date,HE.cntDatetime)
But what if SerialNumber has been changed more than once down the road Is there a way to select a table that would show commaseparated SerialNumbers in cntSerialNumber column? Smth, like
(Start) | (End) | HWQueueID | cntSNHist |
2022-05-29 | 2022-06-04 | WIN-S671INNTGRE.P00-K0-01 | SN01-01 |
2022-05-29 | 2022-06-04 | WIN-S671INNTGRE.P00-K0-02 | SN02-01,SN02-02,SN02-03 |
CodePudding user response:
Depending on your DBMS, you should be able to use the LISTAGG function along with a GROUP BY clause