在下面的查询中,对于o.EventSetName,o.EventSetDisplay,o.EventSetDescription - 任何时候结果中的3列都有重复的ROWS - 只应显示第一个这样的行,其余的重复行应为空白.....
这是sql:
Select distinct top 100000 o.EventSetName,
o.EventSetDisplay,
o.EventSetDescription,
o.ChildSetName,
ROW_NUMBER() Over (Order By f.ChildSetName) RN,
f.DocumentDispSequence,
f.SectionDispSequence,
o.ObsSetDispSequence,
null
From ##ObsSetLevel o,
##Final f
Where f.ChildSetName = o.EventSetName and
o.EventSetName = @variableName
Order By RN asc, f.DocumentDispSequence asc, f.SectionDispSequence asc, o.ObsSetDispSequence asc
我没有报告工具,所以现在需要在存储过程本身中完成很多报告逻辑......
所以而不是:
val 1 val2 val3 val7
val 1 val2 val3 val8
val 1 val2 val3 val 10
val 1 val2 val3 x
val 1 val2 val3 y
对于行2,3,4和5
的前3列,我应该获得空白的resullt答案 0 :(得分:2)
在CTE中包含现有查询,添加ROW_NUMBER OVER PARTITION BY
列,这将为每组值创建RN。在外部查询中,只需使用CASE选择GRP_RN = 1的值,否则选择空字符串。
WITH CTE AS
(
Select distinct top 100000
o.EventSetName,
o.EventSetDisplay,
o.EventSetDescription,
o.ChildSetName,
ROW_NUMBER() Over (Order By f.ChildSetName) RN,
f.DocumentDispSequence,
f.SectionDispSequence,
o.ObsSetDispSequence,
null as NullColumnNeedsName,
ROW_NUMBER() OVER (PARTITION BY o.EventSetName, o.EventSetDisplay,o.EventSetDescription ORDER BY f.ChildSetName) GRP_RN
From ##ObsSetLevel o,
INNER JOIN ##Final f ON f.ChildSetName = o.EventSetName and o.EventSetName = @variableName
)
SELECT
CASE WHEN GRP_RN = 1 THEN o.EventSetName ELSE '' AS EventSetName,
CASE WHEN GRP_RN = 1 THEN o.EventSetDisplay ELSE '' AS EventSetDisplay,
CASE WHEN GRP_RN = 1 THEN o.EventSetDescription ELSE '' AS EventSetDescription,
other columns
FROM CTE
Order By RN asc, DocumentDispSequence asc, SectionDispSequence asc, o.ObsSetDispSequence asc
PS:我还纠正了你对旧式连接的使用。这种用法在20多年前已经过时,引入了SQL-92标准。 You should avoid using them.