我有一个SQL
表格Details
,其中列名为ITEMS
,TotalOrder
,CancelOrder
我写这样的查询
select Items, SUM(TotalOrder) as [TotalOrder], SUM(CancelOrder)as [CancelOrder] ,Source from Details group by Items
,结果似乎是这样的
ITEMS TotalOrder CancelOrder
Laptop 102 5
Desktop 9 1
Tablet 17 0
Tshirt 119 12
TV 5 0
但我的列名Items
包含数据Laptop
,Desktop
,Tablet
与名称Laptop
相同,这就是我希望的原因获得像
ITEMS TotalOrder CancelOrder
Laptop 128 6
Tshirt 119 12
TV 5 0
答案 0 :(得分:0)
试试这个
select Items, SUM(TotalOrder) as [TotalOrder], SUM(CancelOrder)as [CancelOrder]
from Details
where Items NOT IN('Laptop','Desktop','Tablet') group by Items
UNION
select 'Laptop',SUM(TotalOrder) as [TotalOrder], SUM(CancelOrder)as [CancelOrder]
from Details
where Items IN('Laptop','Desktop','Tablet')