我有一个返回两列的SQL查询 - “Title”和“Count”。当“Title”为NULL或为空('')时,我想将结果合并为一行。我怎么能这样做?
这是我到目前为止所做的:
SELECT [Title] WHEN '' THEN 'blank' ELSE ISNULL([Title],'blank') AS [Title],
COUNT([value]) AS [Count]
FROM ....
WHERE ....
GROUP BY [Title],[Count]
但由于Group By,它们分为两个不同的行:
答案 0 :(得分:5)
SELECT CASE WHEN COALESCE([Title],'') = '' THEN 'blank'
ELSE [Title]
END AS [Title],
COUNT([value]) AS [Count]
FROM ....
WHERE ....
GROUP BY CASE WHEN COALESCE([Title],'') = '' THEN 'blank'
ELSE [Title]
END
答案 1 :(得分:1)
Joe Stefanelli解决方案的替代方案:
Select Case
When Len( [Title] ) > 0 Then [Title]
Else 'blank'
End
From ...
Group By Case
When Len( [Title] ) > 0 Then [Title]
Else 'blank'
End
答案 2 :(得分:0)
在GROUP BY子句中使用[Count]列似乎是可疑的......如果你只按[Title]分组,它会起作用吗?
答案 3 :(得分:0)
<强> COALESCE 强>
选择title_id,count(*),COALESCE(type,pub_id)
来自标题
通过...分组
的title_id,类型,的pub_id