TSQL组生成重复行

时间:2017-06-27 18:54:27

标签: sql-server tsql stored-procedures

我正在尝试按ID列中的日期范围(不一定是相同日期)提取所有价格和税金。 因为我需要将其他列分组,因为T-SQL需要:

  

列'...'在选择列表中无效,因为       它不包含在聚合函数或GROUP BY子句中。

我有时会有重复的用户/ ID。 (顺便提一下,不知道为什么。)

我有这个SQL:

WITH myQuery AS 
(
  Select
      c.name, c.id,
      CASE 
      WHEN g.dateCreated BETWEEN CAST ('2016-06-01 00:00:00.000' AS DATETIME) 
           AND CAST ('2017-05-31 23:59:59.000' AS DATETIME) 
      THEN SUM(CAST(g.price AS decimal(20,2) ))
      ELSE 0
      END AS TOTAL_PRICE,
      CASE 
      WHEN g.dateCreated BETWEEN CAST ('2016-01-01 00:00:00.000' AS DATETIME) 
           AND CAST ('2016-12-31 23:59:59.000' AS DATETIME) 
      THEN SUM(CAST(g.tax AS decimal(20,2) ))
      ELSE 0
      END AS TOTAL_TAX
  FROM customers c 
  inner join goodies g
      ON c.id = g.customer_id
  GROUP BY  c.name, c.id, g.dateCreated
)
SELECT count(*) FROM  myQuery

我有5203行。我只有5031个用户。

当我分析我的数据时,我有一些重复的数据。

示例:

  Alex, 12,   0.00,  0.00
  Alex, 12, 100.00, 14.55
 Nancy,  4,   0.00,  0.00
Arthur, 97,  48.14, 09.17

我试图仅通过id进行分组,但似乎我不能这样做。

为什么我有重复的数据以及如何防止这种情况,并确保即使他们不买好吃的东西我也有1行?

1 个答案:

答案 0 :(得分:2)

更正您的条件汇总并从dateCreated

中删除group by
with myQuery as (
select
    c.name
  , c.id
  , total_price = sum(case
      when g.dateCreated >= '20160601' and g.dateCreated < '20170601'
        then cast(g.price as decimal(20,2))
      else 0
      end)
  , total_tax = sum(case
      when g.dateCreated >= '20160101' and g.dateCreated < '20170101'
        then cast(g.tax as decimal(20,2))
      else 0
      end)
from customers c
  left join goodies g
    on c.id = g.customer_id
group by
    c.name
  , c.id
--, g.dateCreated
)
select count(*) from  myQuery;

inner join更改为left join即使在customers中没有相应的行,也会返回goodies

我还更改了您的日期范围代码,以更明确地了解所包含的内容。

参考: