我想在sql中使用查询结果,但它有一些重复值,我想要它的不同值

时间:2018-02-03 18:49:37

标签: sql sql-server

我的查询是

SELECT 
    [date], [Id], COUNT(*) AS cc
FROM 
    BestReviews
WHERE
    customer = 'ATVPDKIKX0DER'
GROUP BY
    [id], [date]
HAVING 
    COUNT(Id) > 50 
ORDER BY
    cc DESC

它会返回如下结果:

1999-3-22   534215  64
1999-3-22   220009  64
1998-11-18  139545  58
1998-11-18  314112  58
1998-11-18  427338  58
1999-11-16  75564   55
1999-11-16  279232  55

我想要这个查询只有3个不同的日期。 e.x. 1999-3-22和1998-11-18和1999-11-16。在有序的方式后代

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:2)

目前还不清楚为什么你会得到不同ID的相同数量。

但是,您的问题的答案似乎是从idselect移除group by

SELECT [date], count(*) as cc
FROM BestReviews
WHERE customer = 'ATVPDKIKX0DER'
GROPU BY [date]
HAVING count(*) > 50 
ORDER BY cc DESC;

答案 1 :(得分:0)

    SELECT  date ,
            id ,
            cc
    FROM    ( SELECT    * ,
                        ROW_NUMBER() OVER ( PARTITION BY date ORDER BY id ) ctr
              FROM      ( SELECT    [date] ,
                                    [Id] ,
                                    COUNT(*) AS cc
                          FROM      BestReviews
                          WHERE     customer = 'ATVPDKIKX0DER'
                          GROUP BY  [id] ,
                                    [date]
                          HAVING    COUNT(Id) > 50
                        ) Main
            ) T
    WHERE   ctr = 1
    ORDER BY cc DESC