我正在使用SQL Server,我的任务是编译一个我们没有从(file1,File2,File3)收到所有3个文件的商店列表。换句话说,每个商店都需要发送这些文件。如果商店没有发送文件,我需要能够识别该商店和缺少的特定文件。
例如,
StoreName|StoreID|NumberReceived|Missing Files
StoreABC 1234 2 File1
SuperStore 4321 1 File1, File2
这是我到目前为止所拥有的。我一直试图找出丢失的文件,并将它们放在一个用逗号分隔的符号列中
SELECT
ds.StoreName,
StoreID,
COUNT(DISTINCT FileType) as 'NumberReceived'
from ETL.CustomerFileExtractLog cfe
inner join Final.DimStore ds
on cfe.StoreID = ds.NABPNumber
where Customer ='ComputerRx'
and TimeStamp >= DATEADD(DAY, -1, GETDATE())
Group by StoreName,
StoreID
having COUNT(DISTINCT FileType) < 3
order by StoreName
答案 0 :(得分:1)
使用STUFF和XML PATH()我们可以得到逗号分隔,只需根据你的要求修改它就是假设代码
;WITH CTE AS (
SELECT
ds.StoreName,
StoreID,
FileType
FROM fILETYPE t
INNER JOIN ETL.CustomerFileExtractLog cfe
inner join Final.DimStore ds
on cfe.StoreID = ds.NABPNumber
where Customer ='ComputerRx'
and TimeStamp >= DATEADD(DAY, -1, GETDATE())
Group by StoreName,
StoreID
having COUNT(DISTINCT FileType) < 3 )
Select
StoreName,
StoreID,
COUNT(DISTINCT FileType) as 'NumberReceived',
STUFF((SELECT ', ' + CAST(Files AS VARCHAR(50)) FileNAmes
FROM CTE
WHERE StoreID = C.StoreID
FOR XML PATH(''), TYPE)
.value('.','NVARCHAR(MAX)'),1,2,' ')
FROM CTE C