我有一组数据(下面),我试图获得一个数据总和,并将列与所需结果聚合,如下所示。有一个简单的方法吗?
Location1 Location2 Product Mode Customer Quantity
61 151 A TL Bill 800
61 151 A TL Bill 800
61 501 B TL Nan 800
61 501 C TL Cas 800
61 901 B TL Cas 800
61 901 B TL Cas 800
61 111 C TL Bill 800
期望的结果:
Location1 Location2 Product Mode Customer Quantity**
61 151 A TL Bill 1600
61 501 B TL Nan 800
61 501 C TL Cas 800
61 501 C TL Cas 1600
61 111 C TL Bill 800
答案 0 :(得分:0)
因为我有点无聊,我会为你做你的工作。
Declare @data as Table(Location1 int, Location2 int, Product varchar(1), Mode varchar(2), Customer varchar(5), Quantity float)
Insert into @data(Location1, Location2, Product, Mode, Customer, Quantity)
Values
(61, 151, 'A', 'TL', 'Bill',800 )
, (61, 151, 'A', 'TL', 'Bill',800 )
, (61, 501, 'B', 'TL', 'Nan',800 )
, (61, 501, 'C', 'TL', 'Cas',800 )
, (61, 901, 'B', 'TL', 'Cas',800 )
, (61, 901, 'B', 'TL', 'Cas',800 )
, (61, 111, 'C', 'TL', 'Bill', 800)
SELECT
Location1, Location2, Product, Mode, Customer, Sum(Quantity) as TotalQty
FROM
@Data
GROUP BY
Location1, Location2, Product, Mode, Customer
ORDER BY
Product
结果:
(7 row(s) affected)
Location1 Location2 Product Mode Customer TotalQty
----------- ----------- ------- ---- -------- -----------
61 151 A TL Bill 1600
61 501 B TL Nan 800
61 901 B TL Cas 1600
61 111 C TL Bill 800
61 501 C TL Cas 800
(5 row(s) affected)