Sql选择语句,同时获得一个值的总和

时间:2013-10-24 05:05:34

标签: sql sql-server tsql

您好我有以下选择声明

      select
      a.amount
      ,b.des
      ,a.id
      ,SUM(a.amount) as total
      ,b.id  
      from t_sales a
      left outer join t_location b on(b.id=a.orgId)
      where a.Id=@salesId   and
      a.sId=@supId
      GROUP BY a.amount, b.des, a.Id,b.id;

除了总计之外,一切都很好。我试图获得总计a.amount返回15个值,所以我希望总共有15个值。请让我知道如何解决它。 谢谢

3 个答案:

答案 0 :(得分:0)

你能试试吗

select            a.amount, b.des, a.id, b.id,
                  (select            sum(a.amount)
                  from              t_sales a
                  left outer join   t_location b on (b.id = a.orgId)
                  where             a.Id = @salesId   
                  and               a.sId = @supId
                  GROUP BY          b.des, a.Id, b.id) as total
from              t_sales a
left outer join   t_location b on (b.id = a.orgId)
where             a.Id = @salesId   
and               a.sId = @supId
GROUP BY          a.amount, b.des, a.Id, b.id;

答案 1 :(得分:0)

请尝试:

select
    a.amount
    ,b.des
    ,a.id
    ,SUM(a.amount) over(partition by 1) as total
    ,b.id  
from t_sales a
    left outer join t_location b on(b.id=a.orgId)
where 
    a.Id=@salesId   and
    a.sId=@supId;

答案 2 :(得分:0)

您应该从选择列表中删除a.amount列(选择列表中不应存在汇总列

 select b.des, a.id, a.amount  ,SUM(a.amount) over(partition by 1) as total, b.id from t_sales a left outer join t_location b on(b.id=a.orgId)
    where a.Id=@salesId and a.sId=@supId GROUP BY b.des, a.Id, b.id;