不选择重复行

时间:2015-10-03 00:56:42

标签: sql sql-server

我的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

有可能吗?

1 个答案:

答案 0 :(得分:0)

评论太长了。

如果您仅在countryregion进行分组,则无法复制'UNKNOWN' / 'UNKNOWN'GROUPING SETS条件与此无关。他们生成的任何其他行在其中一列或两列中都会有NULL个值。

所以,我唯一的结论是'UNKNOWN'在不同的行中是不同的。你可能没有看到差异。一种可能性是尾随空格。另一个是看不见的角色。