按一个字段求​​和,但仅限于唯一ID

时间:2015-05-04 15:19:09

标签: sql sql-server distinct aggregate-functions

我需要按月和年汇总销售数量,但我的数据库有一些重复的条目。我知道这不是理想的,但它是一个政府数据库,所以我无法解决它。该数据库有大量记录(> 2亿),所以我不想按ID分组。

这基本上就是我想要做的事情:

select YEAR(begin_date) as yr, MONTH(begin_date) as mnth, SUM(quantity) as quant 
from Table
[where transactionID is unique]
group by YEAR(begin_date), MONTH(begin_date)
order by YEAR(begin_date), MONTH(begin_date)

结果数据应该如下所示,但不包括重复数据:

yr  mnth    quant
2009    10  91241
2009    11  23650
2009    12  37006
2010    1   19770
2010    2   19937
2010    3   14403

2 个答案:

答案 0 :(得分:0)

您可以使用内部选择来删除一年中重复的重复事务ID,如下所示:

SELECT [year], [month], SUM(quantity)
FROM (SELECT DISTINCT 
          YEAR(begin_date) as [year], MONTH(begin_date) as [month], MAX(quantity) as [quantity], transactionID
      FROM yourTable 
      GROUP BY
          YEAR(begin_date) as [year], MONTH(begin_date), transactionID ) DT
GROUP By [year], [month]
ORDER BY [year], [month]

答案 1 :(得分:0)

假设您的表具有唯一的主键,您可以使用CTE来确定重复项,只需选择一个即可使用。我还支持一个有一些设计问题的数据库,并使用这个技巧来过滤掉欺骗。

;with uniques AS (PK,Number) (
    SELECT
        PrimaryKey,
        ROW_NUMBER() OVER(PARTITION BY YEAR(begin_date),MONTH(begin_date) ORDER BY YEAR(begin_date)) as number
    FROM Table
)
select YEAR(begin_date) as yr, MONTH(begin_date) as mnth, SUM(quantity) as quant 
from Table t
INNER JOIN uniques u
    ON u.pk = t.PrimaryKey
    AND u.number = 1
group by YEAR(begin_date), MONTH(begin_date)
order by YEAR(begin_date), MONTH(begin_date)