使用 CUBE 彙總資料

2021-10-03 04:41:18 字數 1698 閱讀 2296

cube 運算子生成的結果集是多維資料集。多維資料集是事實資料(即記錄個別事件的資料)的擴充套件。擴充套件是基於使用者要分析的列建立的。這些列稱為維度。多維資料集是結果集,其中包含各維度的所有可能組合的交叉**。

cube 運算子在 select 語句的 group by 子句中指定。該語句的選擇列表包含維度列和聚合函式表示式。group by 指定了維度列和關鍵字 with cube。結果集包含維度列中各值的所有可能組合,以及與這些維度值組合相匹配的基礎行中的聚合值。

create table inventory

( item char(10) null,

color char(10) null,

quantity int null

)insert into inventory

values

('table','blue','124'),

('table','red','223'),

('chair','blue','101'),

('chair','red','210')

select item,color,sum(quantity) as qtysum

from inventory

group by item,color with cube

select item,color,sum(quantity) as qtysum

from inventory

group by cube(item,color)

select case when (grouping(item) = 1) then 'all'

else isnull(item, 'unknown')

end as item,

case when (grouping(color) = 1) then 'all'

else isnull(color, 'unknown')

end as color,

sum(quantity) as qtysum

from inventory

group by item, color with cube

select case when (grouping(item) = 1) then 'all'

else isnull(item, 'unknown')

end as item,

sum(quantity) as qtysum

from inventory

group by item with cube

gocreate view invcube as

select case when (grouping(item) = 1) then 'all'

else isnull(item, 'unknown')

end as item,

case when (grouping(color) = 1) then 'all'

else isnull(color, 'unknown')

end as color,

sum(quantity) as qtysum

from inventory

group by item, color with cube

select *

from invcube

where item = 'chair'

and color = 'all'

使用CUBE和ROLLUP對資料進行彙總

it專家網獨家 想要找乙個既快捷又有效的方法來對您儲存在資料庫裡的資料進行彙總分析嗎?sql語言中的rollup和cube命令提供了乙個非常有用的工具,可以讓您快速深入地獲取資料的各種內在性質。rollup和cube是sql的擴充套件命令,可以在sql server 6.5 及以上版本 和oracl...

排序演算法彙總(資料結構)

1.氣泡排序 氣泡排序 int a 20 int n for int i 0 i1 i 而對於氣泡排序有乙個優化的方法,優化在於當一次排序沒有進行過交換的時候,也就是之後的順序都是有序的時候,相當於整個序列都是有序的了。氣泡排序優化 int a 20 int n int falg 做乙個標記位來判斷...

tableau彙總資料排序(正常不能排序)

發現access資料庫裡,期間 這個表有問題,有它和 簡易工資表 做聯接時,不但資料庫反應超慢,而且出現問題,統計資料出現錯誤,一定要注意這個問題 如 烏海龐大興銳汽車銷售服務 只在2015年11月份工資時,能統計出2個人,而實際情況是 這個單位只在當年1月 2月各有一人。暫行解決辦法 一 錯誤找到...