前一段时间完成公司的薪资系统的开发工作,用了层层SQL做生成类似EXCEL的数据透视表,没想到用一个SQL就能实现,实在有些打击,和大家分享一下。
比如说:我要得到类似下面格式的数据。
CHANNEL_DESC CO SALES$
-------------------- -- --------------
833,224
FR 70,799
US 762,425
Internet 133,821
Internet FR 9,597
Internet US 124,224
Direct Sales 699,403
Direct Sales FR 61,202
Direct Sales US 638,201
我开始的做法需要JDBC执行3层group by,但是现在发现只需要下面一个SQL语句,这是ORACLE为DW提供的GROUP BY CUBE ,它可以轻松实现数据透视表。
SELECT channels.channel_desc, countries.country_iso_code,
TO_CHAR(SUM(amount_sold), '9,999,999,999') SALES$
FROM sales, customers, times, channels, countries
WHERE sales.time_id=times.time_id AND sales.cust_id=customers.cust_id AND
sales.channel_id= channels.channel_id AND channels.channel_desc IN
('Direct Sales', 'Internet') AND times.calendar_month_desc='2000-09'
AND customers.country_id=countries.country_id
AND countries.country_iso_code IN ('US','FR')
GROUP BY CUBE(channels.channel_desc, countries.country_iso_code);
注意:上面的SQL,之所以要查sales, customers, times, channels, countries这么多表,是因为这是数据仓库的星型模型,如果是数据都在一张表里也一样,和GROUP BY CUBE没关系。
下面的GROUP BY ROLLUP更加方便使用。
SELECT channels.channel_desc, calendar_month_desc,
countries.country_iso_code,
TO_CHAR(SUM(amount_sold), '9,999,999,999') SALES$
FROM sales, customers, times, channels, countries
WHERE sales.time_id=times.time_id
AND sales.cust_id=customers.cust_id
AND customers.country_id = countries.country_id
AND sales.channel_id = channels.channel_id
AND channels.channel_desc IN ('Direct Sales', 'Internet')
AND times.calendar_month_desc IN ('2000-09', '2000-10')
AND countries.country_iso_code IN ('GB', 'US')
GROUP BY
ROLLUP(channels.channel_desc, calendar_month_desc, countries.country_iso_code);
CHANNEL_DESC CALENDAR CO SALES$
-------------------- -------- -- --------------
Internet 2000-09 GB 16,569
Internet 2000-09 US 124,224
Internet 2000-09 140,793
Internet 2000-10 GB 14,539
Internet 2000-10 US 137,054
Internet 2000-10 151,593
Internet 292,387
Direct Sales 2000-09 GB 85,223
Direct Sales 2000-09 US 638,201
Direct Sales 2000-09 723,424
Direct Sales 2000-10 GB 91,925
Direct Sales 2000-10 US 682,297
Direct Sales 2000-10 774,222
Direct Sales 1,497,646
1,790,032
注意:group by cube 的汇总数据是在明细数据之前显示,group by rollup 的汇总数据是在明细数据之后显示,并且最后有整个的汇总数据。
阅读(8865) | 评论(0) | 转发(0) |