如何对单独的数据集进行分组

时间:2016-03-23 19:10:47

标签: sql sql-server count group-by sum

目前我的查询看起来像这样

Select A, B,  count (C) as Quantity, sum (D) as Fee,
       (select ABC from Mappings where MT = A) as 'Dog',
       (select ACD from Mappings where MT = A) as 'Cat'
from @mt
where A in (select MT from Mappings where AED in (@Cli))
group by A, B

目前我的数据正在对A上的数据进行计数和求和,但我希望数据能够被Cat计算和驯服。

有没有办法将数据分组到另一列

我正在使用t-sql

感谢任何帮助

1 个答案:

答案 0 :(得分:0)

由于AB没有进行汇总计算,因此它们都需要保留在GROUP BY子句中。如果您在最终结果中不需要那些(或“狗”),您可以尝试以下方法:

Select (select ACD from Mappings where MT = A) as 'Cat'
count (C) as Quantity, 
sum (D) as Fee
from @mt
where A in (select MT from Mappings where AED in (@Cli))
group by (select ACD from Mappings where MT = A)