dw=> select t2.product_category, t2.product_name, sum(nq), sum(order_amount) dw-> from v_sales_order_fact t1, product_dim t2 dw-> where t1.product_sk = t2.product_sk dw-> group by rollup (t2.product_category, t2.product_name) dw-> order by t2.product_category, t2.product_name; product_category | product_name | sum | sum ------------------+-----------------+-----+----------- monitor | flat panel | | 49666.00 monitor | lcd panel | 11 | 3087.00 monitor | | 11 | 52753.00 peripheral | keyboard | 38 | 67387.00 peripheral | | 38 | 67387.00 storage | floppy drive | 52 | 348655.00 storage | hard disk drive | 80 | 375481.00 storage | | 132 | 724136.00 | | 181 | 844276.00 (9 rows)
dw=> select t2.product_category, t2.product_name, t3.state, t3.city, sum(nq), sum(order_amount) dw-> from v_sales_order_fact t1, product_dim t2, zip_code_dim t3 dw-> where t1.product_sk = t2.product_sk dw-> and t1.customer_zip_code_sk = t3.zip_code_sk dw-> group by rollup (t2.product_category, t2.product_name, t3.state, t3.city) dw-> order by t2.product_category, t2.product_name, t3.state, t3.city; product_category | product_name | state | city | sum | sum ------------------+-----------------+-------+---------------+-----+----------- monitor | flat panel | oh | cleveland | | 7431.00 monitor | flat panel | oh | | | 7431.00 monitor | flat panel | pa | mechanicsburg | | 10630.00 monitor | flat panel | pa | pittsburgh | | 31605.00 monitor | flat panel | pa | | | 42235.00 monitor | flat panel | | | | 49666.00 monitor | lcd panel | pa | pittsburgh | 11 | 3087.00 monitor | lcd panel | pa | | 11 | 3087.00 monitor | lcd panel | | | 11 | 3087.00 monitor | | | | 11 | 52753.00 peripheral | keyboard | oh | cleveland | 38 | 10875.00 peripheral | keyboard | oh | | 38 | 10875.00 peripheral | keyboard | pa | mechanicsburg | | 29629.00 peripheral | keyboard | pa | pittsburgh | | 26883.00 peripheral | keyboard | pa | | | 56512.00 peripheral | keyboard | | | 38 | 67387.00 peripheral | | | | 38 | 67387.00 storage | floppy drive | oh | cleveland | | 8229.00 storage | floppy drive | oh | | | 8229.00 storage | floppy drive | pa | mechanicsburg | | 140410.00 storage | floppy drive | pa | pittsburgh | 52 | 200016.00 storage | floppy drive | pa | | 52 | 340426.00 storage | floppy drive | | | 52 | 348655.00 storage | hard disk drive | oh | cleveland | | 8646.00 storage | hard disk drive | oh | | | 8646.00 storage | hard disk drive | pa | mechanicsburg | 80 | 194444.00 storage | hard disk drive | pa | pittsburgh | | 172391.00 storage | hard disk drive | pa | | 80 | 366835.00 storage | hard disk drive | | | 80 | 375481.00 storage | | | | 132 | 724136.00 | | | | 181 | 844276.00 (31 rows)
dw=> select t2.product_category, dw-> t1.year_month, dw-> sum(quantity1) quantity_cur, dw-> sum(quantity2) quantity_pre, dw-> round((sum(quantity1) - sum(quantity2)) / sum(quantity2),2) pct_quantity, dw-> sum(amount1) amount_cur, dw-> sum(amount2) amount_pre, dw-> round((sum(amount1) - sum(amount2)) / sum(amount2),2) pct_amount dw-> from (select t1.product_sk, dw(> t1.year_month, dw(> t1.month_order_quantity quantity1, dw(> t2.month_order_quantity quantity2, dw(> t1.month_order_amount amount1, dw(> t2.month_order_amount amount2 dw(> from v_month_end_sales_order_fact t1 dw(> join v_month_end_sales_order_fact t2 dw(> on t1.product_sk = t2.product_sk dw(> and t1.year_month/100 = t2.year_month/100 + 1 dw(> and t1.year_month - t1.year_month/100*100 = t2.year_month - t2.year_month/100*100) t1, dw-> product_dim t2 dw-> where t1.product_sk = t2.product_sk dw-> group by t2.product_category, t1.year_month dw-> order by t2.product_category, t1.year_month; product_category | year_month | quantity_cur | quantity_pre | pct_quantity | amount_cur | amount_pre | pct_amount ------------------+------------+--------------+--------------+--------------+------------+------------+------------ storage | 201705 | 943 | | | 142814.00 | 110172.00 | 0.30 storage | 201706 | 110 | | | 9132.00 | 116418.00 | -0.92 (2 rows)
dw=> select t2.state, dw-> t2.city, dw-> count(distinct customer_sk) sum_customer_num, dw-> sum(order_amount) sum_order_amount dw-> from v_sales_order_fact t1, zip_code_dim t2 dw-> where t1.customer_zip_code_sk = t2.zip_code_sk dw-> group by rollup (t2.state, t2.city) dw-> order by t2.state, t2.city; state | city | sum_customer_num | sum_order_amount -------+---------------+------------------+------------------ oh | cleveland | 4 | 35181.00 oh | | 4 | 35181.00 pa | mechanicsburg | 8 | 375113.00 pa | pittsburgh | 12 | 433982.00 pa | | 20 | 809095.00 | | 24 | 844276.00 (6 rows)
dw=> select sum_total, sum_late, round(cast(sum_late as numeric)/sum_total,4) late_pct dw-> from (select sum(case when status_date_sk < entry_date_sk then 1 dw(> else 0 dw(> end) sum_late, dw(> count(*) sum_total dw(> from sales_order_fact) t; sum_total | sum_late | late_pct -----------+----------+---------- 151 | 2 | 0.0132 (1 row)
dw=> select round(avg(sum_order_amount),2) avg_amount, dw-> round(sum(sum_order_amount)/count(customer_sk),2) avg_amount1, dw-> percentile_cont(0.5) within group (order by sum_order_amount) median_amount, dw-> median(sum_order_amount) median_amount1 dw-> from (select customer_sk,sum(order_amount) sum_order_amount dw(> from v_sales_order_fact dw(> group by customer_sk) t1; avg_amount | avg_amount1 | median_amount | median_amount1 ------------+-------------+---------------+---------------- 35178.17 | 35178.17 | 14277 | 14277 (1 row)
dw=> select percentile_cont(0.25) within group (order by sum_order_amount desc) max_amount_25, dw-> percentile_cont(0.50) within group (order by sum_order_amount desc) max_amount_50, dw-> percentile_cont(0.75) within group (order by sum_order_amount desc) max_amount_75 dw-> from (select customer_sk,sum(order_amount) sum_order_amount dw(> from v_sales_order_fact dw(> group by customer_sk) t1; max_amount_25 | max_amount_50 | max_amount_75 ---------------+---------------+--------------- 50536.5 | 14277 | 8342.25 (1 row)
dw=> select year1, dw-> bn, dw-> c_count, dw-> sum_band, dw-> sum_total, dw-> round(sum_band/sum_total,4) band_pct dw-> from (select count(a.customer_sk) c_count, dw(> sum(annual_order_amount) sum_band, dw(> a.year year1, dw(> band_name bn dw(> from annual_customer_segment_fact a, dw(> annual_order_segment_dim b, dw(> annual_sales_order_fact d dw(> where a.segment_sk = b.segment_sk dw(> and a.customer_sk = d.customer_sk dw(> and a.year = d.year dw(> and b.segment_name = 'grid' dw(> group by a.year, bn) t1, dw-> (select sum(annual_order_amount) sum_total dw(> from annual_sales_order_fact) t2 dw-> order by year1, bn; year1 | bn | c_count | sum_band | sum_total | band_pct -------+------+---------+-----------+-----------+---------- 2016 | high | 6 | 572190.00 | 572190.00 | 1.0000 (1 row)
dw=> select case when t1.rn =1 then t1.city end city, dw-> t2.product_name, dw-> t1.sum_order_amount, dw-> t1.rn dw-> from (select city, dw(> product_sk, dw(> sum_order_amount, dw(> row_number() over (partition by city order by sum_order_amount desc) rn dw(> from (select t2.state||':'||t2.city city, dw(> product_sk, dw(> sum(order_amount) sum_order_amount dw(> from v_sales_order_fact t1, zip_code_dim t2 dw(> where t1.customer_zip_code_sk = t2.zip_code_sk dw(> group by t2.state||':'||t2.city, product_sk) t) t1 dw-> inner join product_dim t2 on t1.product_sk = t2.product_sk dw-> where t1.rn <= 3 dw-> order by t1.city, t1.rn; city | product_name | sum_order_amount | rn ------------------+-----------------+------------------+---- oh:cleveland | keyboard | 10875.00 | 1 | hard disk drive | 8646.00 | 2 | floppy drive | 8229.00 | 3 pa:mechanicsburg | hard disk drive | 194444.00 | 1 | floppy drive | 140410.00 | 2 | keyboard | 29629.00 | 3 pa:pittsburgh | floppy drive | 200016.00 | 1 | hard disk drive | 172391.00 | 2 | flat panel | 31605.00 | 3 (9 rows)
dw=> select product_name, dw-> sum_order_amount, dw-> percent_rank() over (order by sum_order_amount desc) rank dw-> from (select product_sk,sum(order_amount) sum_order_amount dw(> from v_sales_order_fact dw(> group by product_sk) t1, product_dim t2 dw-> where t1.product_sk = t2.product_sk dw-> order by rank; product_name | sum_order_amount | rank -----------------+------------------+------ hard disk drive | 375481.00 | 0 floppy drive | 348655.00 | 0.25 keyboard | 67387.00 | 0.5 flat panel | 49666.00 | 0.75 lcd panel | 3087.00 | 1 (5 rows)
%jdbc select t2.product_category, t2.product_name, t3.state, t3.city, sum(nq) sq, sum(order_amount) sa from v_sales_order_fact t1, product_dim t2, zip_code_dim t3 where t1.product_sk = t2.product_sk and t1.customer_zip_code_sk = t3.zip_code_sk group by t2.product_category, t2.product_name, t3.state, t3.city order by t2.product_category, t2.product_name, t3.state, t3.city;运行结果的表格、柱状图、饼图、堆叠图、线形图、散点图分别如图5-图10所示。
图6
图7
图8
图9
图10
一个note中可以独立执行多个查询语句。图形显示可以根据不同的“settings”联机分析不同的指标。报表有default、simple、report三种可选样式。例如,报表样式的饼图表示如图11所示。%jdbc select t2.product_category, t2.product_name, t3.state, t3.city, sum(nq) sq, sum(order_amount) sa from v_sales_order_fact t1, product_dim t2, zip_code_dim t3 where t1.product_sk = t2.product_sk and t1.customer_zip_code_sk = t3.zip_code_sk and t1.year_month/100 = ${year} group by t2.product_category, t2.product_name, t3.state, t3.city order by t2.product_category, t2.product_name, t3.state, t3.city;在运行查询时会在页面中出现一个输入框,填入适当的变量值运行查询,如图15所示。
%jdbc select ${checkbox:fields=t2.product_category, t2.product_category|t2.product_name},t3.state, t3.city, sum(nq) sq, sum(order_amount) sa from v_sales_order_fact t1, product_dim t2, zip_code_dim t3 where t1.product_sk = t2.product_sk and t1.customer_zip_code_sk = t3.zip_code_sk and t1.year_month/100 = ${year} group by ${checkbox:fields=t2.product_category, t2.product_category|t2.product_name}, t3.state, t3.city order by ${checkbox:fields=t2.product_category, t2.product_category|t2.product_name}, t3.state, t3.city;查询运行时出现字段复选框,如图16所示。