按一个字段求​​和,但仅针对唯一 ID

Sum by one field, but only for unique ID

我需要按月和年对销售量求和,但我的数据库有一些重复的条目。我知道这并不理想,但它是一个政府数据库,所以我无法修复它。数据库有大量记录(> 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

您可以使用 inner-select 删除一年中某月重复的重复交易 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]

假设您的 table 有一个唯一的主键,您可以使用 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)