我有一个内部联接的嵌套查询,它返回给定"软件"的多个记录,但我需要在一行中压缩结果... (输出附件) 对于一个软件,您会看到多个记录,其中包含已解决的字段'并且'没有解决',我需要将其压缩到一行,分别显示已解决 - 已解决 - 未解决...例如。 但有时候,同一列会有两个或多个" Resolved"记录。我想创建一个新列,如SPPA2或WCS2等...如果发生这种情况,并将同一列中每条记录的日期合并为"已解决"或"不解决" 我是个新手,所以试着理解这个逻辑。
SELECT * FROM
(SELECT act.Vendor + ' ' + act.[Application Name] + ' ' + act.Version as "Software name"
,act.srmNumber as SRM#
,ssd.DateAssign
,IIF(ssd.DateResolv IS NOT NULL AND ssd.EntryText LIKE '%Step 1%' AND ssd.TargetUnitID = 48, 'Resolved', 'Not Resolved') as SPPA
,IIF(ssd.DateResolv IS NOT NULL AND ssd.EntryText LIKE '%Step 2%' AND ssd.TargetUnitID = 39, 'Resolved', 'Not Resolved') as WCS
,IIF(ssd.DateResolv IS NOT NULL AND ssd.EntryText LIKE '%Step 3%' AND ssd.TargetUnitID = 738, 'Resolved', 'Not Resolved') as SAM
FROM [SAMI_Main].dbo.vw_SAM_Software_Deployment AS ssd INNER JOIN [SAMI_Main].dbo.ACT_Inventory AS act ON act.srmNumber = RIGHT(ssd.callID,7)) AS T1
WHERE t1.SPPA = 'Resolved' OR t1.WCS = 'Resolved' OR t1.SAM = 'Resolved'
;
答案 0 :(得分:0)
这是预期的输出吗?
|Software Name|SRM#|SPPA|WCS|SAM|
|SAP...|1456484|Resolved 2015-09-22|Not Resolved|Not Resolved|
|Dessci...|1514703|Resolved 2016-01-20|Not Resolved|Not Resolved|
|Adobe Air...|1525807|Resolved 2016-02-11|Not Resolved|Not Resolved|
如果是这样,此查询可能有效。我无法访问SQL Server,所以我不确定语法:
SELECT DISTINCT software_name AS [Software Name],
SRM#,
IIF(SPPA = 'Resolved', 'Resolved' + ' ' + MAX(DateAssign OVER (PARTITION BY software_name, SPPA), 'Not Resolved') AS SPPA,
IIF(WCS = 'Resolved', 'Resolved' + ' ' + MAX(DateAssign OVER (PARTITION BY software_name, WCS), 'Not Resolved') AS WCS,
IIF(SAM = 'Resolved', 'Resolved' + ' ' + MAX(DateAssign OVER (PARTITION BY software_name, SAM), 'Not Resolved') AS SAM
FROM
(SELECT act.Vendor + ' ' + act.[Application Name] + ' ' + act.Version as software_name
,act.srmNumber as SRM#
,ssd.DateAssign
,IIF(ssd.DateResolv IS NOT NULL AND ssd.EntryText LIKE '%Step 1%' AND ssd.TargetUnitID = 48, 'Resolved', 'Not Resolved') as SPPA
,IIF(ssd.DateResolv IS NOT NULL AND ssd.EntryText LIKE '%Step 2%' AND ssd.TargetUnitID = 39, 'Resolved', 'Not Resolved') as WCS
,IIF(ssd.DateResolv IS NOT NULL AND ssd.EntryText LIKE '%Step 3%' AND ssd.TargetUnitID = 738, 'Resolved', 'Not Resolved') as SAM
FROM [SAMI_Main].dbo.vw_SAM_Software_Deployment AS ssd
INNER JOIN [SAMI_Main].dbo.ACT_Inventory AS act ON act.srmNumber = RIGHT(ssd.callID,7)) AS T1
WHERE t1.SPPA = 'Resolved' OR t1.WCS = 'Resolved' OR t1.SAM = 'Resolved';
我在这里使用MAX关键字作为分析函数。我以前是written about the MAX keyword,但是从Oracle的角度来看。我理解它在SQL Server中的工作方式类似。
答案 1 :(得分:0)
感谢您的回应,我使用MAX()为SqlServ清理了代码中的语法。 它将输出作为附件...... OUTPUT HERE
所以我只需要将记录压缩到一条记录,每个软件显示每个Resolved列,如果你提供了MAX()日期,如果每个列中都存在...那么在这个例子中,它将是&#34 ; Adobe Air"作为一个记录,所有列"已解决"。另外两个软件是预期的。
SELECT DISTINCT software_name AS [Software Name],
SRM#,
IIF(SPPA ='已解决','已解决' +'(' + MAX(DateAssign)OVER(分区来自software_name,SPPA)+') ','未解决')作为SPPA,
IIF(WCS ='已解决','已解决' +'(' + MAX(DateAssign)OVER(按软件名称分配,WCS)+') ','未解决')作为WCS,
IIF(SAM ='已解决','已解决' +'(' + MAX(DateAssign)OVER(PARTITION BY software_name,SAM)+') ','未解决')作为SAM
从
(SELECT act.Vendor +'' + act。[Application Name] +'' + act.Version as software_name
,act.srmNumber as SRM#
,ssd.DateAssign
,IIF(ssd.DateResolv IS NOT NULL和ssd.EntryText LIKE'%Step 1%' AND ssd.TargetUnitID = 48,' Resolved',' Not Resolved&#39 ;)作为SPPA
,IIF(ssd.DateResolv IS NOT NULL和ssd.EntryText LIKE'%Step 2%' AND ssd.TargetUnitID = 39,' Resolved',' Not Resolved&#39 ;)作为WCS
,IIF(ssd.DateResolv IS NOT NULL和ssd.EntryText LIKE'%Step 3%' AND ssd.TargetUnitID = 738,' Resolved',' Not Resolved&#39 ;)作为SAM
FROM [SAMI_Main] .dbo.vw_SAM_Software_Deployment AS ssd
INNER JOIN [SAMI_Main] .dbo.ACT_Inventory AS act on act.srmNumber = RIGHT(ssd.callID,7))AS t1
在哪里t1.SPPA ='已解决'或者t1.WCS ='已解决'或者t1.SAM ='已解决'订购SRM#DESC;