我的SQL代码如下
SELECT
Country,
Region,
COUNT(Country) AS [Count],
CASE WHEN Region IS NULL THEN 0 ELSE 1 END AS Category
FROM
Games
GROUP BY
GROUPING SETS (
(Country, Region)
,(Country)
)
HAVING GROUPING(Region) = 0 OR COUNT(DISTINCT Region) > 0
ORDER BY
Country,
Category,
Region
结果如下
COUNTRY REGION Count Category
------- ------- --- -------
UNKNOWN UNKNOWN 3 0
UNKNOWN UNKNOWN 3 1
AUSTRIA NULL 1 0
AUSTRIA A 1 1
BELGIUM NULL 1 0
BELGIUM A 1 1
CZECH REPUBLIC NULL 3 0
CZECH REPUBLIC A 1 1
CZECH REPUBLIC B 1 1
CZECH REPUBLIC C 1 1
OPPS OPPS 1 0
OPPS OPPS 1 1
TURKEY NULL 6 0
TURKEY A 1 1
TURKEY B 1 1
TURKEY C 1 1
TURKEY D 1 1
TURKEY E 1 1
TURKEY H 1 1
我不想根据我的示例结果选择重复行,如下所示
UNKNOWN UNKNOWN 3 0
OPPS OPPS 1 0
有可能吗?
答案 0 :(得分:0)
评论太长了。
如果您仅在country
和region
进行分组,则无法复制'UNKNOWN'
/ 'UNKNOWN'
。 GROUPING SETS
条件与此无关。他们生成的任何其他行在其中一列或两列中都会有NULL
个值。
所以,我唯一的结论是'UNKNOWN'
在不同的行中是不同的。你可能没有看到差异。一种可能性是尾随空格。另一个是看不见的角色。