我下面这个系列文章,了解ROLLUP和CUBE相同的查询获取不同的结果
对于此查询:
select
case
when grouping(CustomerName) = 1 then 'All Customers'
else CustomerName
end as CustomerName,
case
when grouping(ItemName) = 1 then 'All Items'
else ItemName
end as ItemName,
sum(quantity*pricepercase) as Amount1
from orders
group by CustomerName, ItemName
with cube
笔者结果是这样的:
CustomerName ItemName Amount
-------------------- -------------------- ---------------------
Jacob Item 1 312.50
Jacob Item 2 480.00
Jacob All Items 792.50
Mike Item 1 75.00
Mike Item 2 44.00
Mike All Items 119.00
All Customers All Items 911.50
All Customers Item 1 387.50
All Customers Item 2 524.00
由多维数据集生成的两个额外的行是最后2行。我得到如下结果:
CustomerName ItemName Amount
-------------------- -------------------- ---------------------
Jacob Item 1 312.50
Mike Item 1 75.00
All Customers Item 1 387.50
Jacob Item 2 480.00
Mike Item 2 44.00
All Customers Item 2 524.00
All Customers All Items 911.50
Jacob All Items 792.50
Mike All Items 119.00
第一个结果集看起来合适。为什么运行它时会有什么不同?
编辑为包含查询。 – Animesh 2012-03-04 10:46:40
不要使用'WITH CUBE' http://msdn.microsoft.com/en-us/library/ms177673.aspx *对所有新工作使用符合ISO的语法。为了向后兼容,提供了非ISO兼容语法。* – 2012-03-04 10:58:00
哦!很高兴知道。 'CUBE(elem1,elem2)'而不是'WITH CUBE' – Animesh 2012-03-04 11:02:12