我下面這個系列文章,瞭解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