2013-10-24 32 views
-1

您好我有以下的select語句SQL SELECT語句同時獲得一個值的總

 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 [email protected] and 
     [email protected] 
     GROUP BY a.amount, b.des, a.Id,b.id; 

一切工作正常,但總。我正在嘗試獲得返回15個值的a.amount的總數,所以我希望總共有15個值。請讓我知道如何解決它。 感謝

+0

你的金額r查詢包含空值? –

+0

@AmirrezaKeshavarz不,它們不是null –

回答

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 [email protected] and [email protected] GROUP BY b.des, a.Id, b.id; 
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; 
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 
    [email protected] and 
    [email protected];