我从这个SQL查询得到重复,我认为它应该只是添加它们

时间:2016-04-20 02:36:34

标签: sql sql-server

这是我的代码

select 
    name, sum(Value) as [Total Donated]
from 
    CHARITY c 
join 
    DONATION d on c.CharityID = d.CharityID
join 
    ITEM_DONATION id on d.DonationID = id.DonationID
group by 
    name, DonationDate
having 
    DonationDate like '%13%'
order by 
    name;

和输出

name                 Total Donated
-------------------- -------------
Boy Scouts           250.00
Boy Scouts             5.00
Focus Hope             5.00
Focus Hope            10.00
Fresh Start Charity   12.00
Helping Hands          2.99
Helping Hands         12.50
Helping Hands        110.50
St. John Hospital    112.00

我以为我已将它设置在童子军应该是一个项目加入到一起并且同样具有焦点希望等等......但是我有点偏离任何人都可以帮助并让我知道我在做什么这里错了吗?

1 个答案:

答案 0 :(得分:3)

您需要从DonationDate子句中删除GROUP BY并将条件从HAVING移至WHERE子句:

select 
    name
    , sum(Value) as [Total Donated]
from CHARITY c 
join DONATION d
    on c.CharityID = d.CharityID
join ITEM_DONATION id
    on d.DonationID = id.DonationID
where
    DonationDate like '%13%'
group by name
order by name;