分析函式例項

2021-06-17 15:55:28 字數 3724 閱讀 2915

具體的分析函式的語法和函式列表見:

/*資料準備:

*以oracle樣例的sh模式為實驗資料

*建立實驗資料表:sales_fact

*/create table sales_fact as

select country_name country,country_subregion region,prod_name product,

calendar_year year,calendar_week_number week,

sum(amount_sold) sale,

sum(amount_sold *

(case

when mod(rownum,10)=0 then 1.4

when mod(rownum,5)=0 then 0.6

when mod(rownum,2)=0 then 0.9

when mod(rownum,2)=1 then 1.2

else 1 end)

) receipts

from sales,times,customers,countries,products

where sales.time_id =times.time_id and

sales.prod_id =products.prod_id and

sales.cust_id =customers.cust_id and

customers.country_id=countries.country_id

group by country_name, country_subregion,prod_name ,calendar_year, calendar_week_number;

---實驗一:聚合函式

/*1、獲取到目前為止的累積消費額;

*2、獲取前後五周內的最大消費額;

*/----獲取到目前為止的累積消費額;

select year,week,sale,

sum(sale)over

(partition by year

order by week

rows between unbounded preceding and current row) cumulate_sum---計算區間為年初到當前

from sales_fact

where product ='xtend memory' and country ='australia' and week between 1 and 3 and year in (1998,1999)

order by year,week;

-----實驗結果

-year-week-sale-cumulate_sum

1998 1 58.15 58.15

1998 2 29.39 87.54

1998 3 29.49 117.03

1999 1 53.52 53.52

1999 3 94.6 148.12

---獲取前後五周內的最大消費額;

select year,week,sale,

sum(sale)over

(partition by year

order by week

rows between 2 preceding and 2 following) cumulate_sum---計算區間為當前行前兩周到當前行的後兩周

from sales_fact

where product ='xtend memory' and country ='australia' and week between 1 and 5 and year in (1998,1999)

order by year,week;

---實驗結果

-year-week-sale-cumulate_sum

1998 1 58.15 117.03

1998 2 29.39 146.52

1998 3 29.49 176.32

1998 4 29.49 118.17

1998 5 29.8 88.78

1999 1 53.52 188.62

1999 3 94.6 268.63

1999 4 40.5 268.63

1999 5 80.01 215.11

----實驗二:定位函式:lead(),lag(),first_value(),last_value(),nth_value()

/*實驗目的:

*1、獲取前一周的銷售量【使用lag()】;

*2、獲取下一周的銷售量【使用lead()】;

*3、獲取最大的銷售額和最小的銷售額【使用first_value()和last_value()】;

*4、獲取第二高的銷售量【使用nth_value()】;

***/

select year,week,sale,

lag(sale,1,sale)over

(partition by year

order by week) lag,----1、獲取前一周的銷售量;

lead(sale,1,sale)over

(partition by year

order by week) lead, ---2、獲取下一周的銷售量;

first_value(sale)over

(partition by year

order by sale desc

rows between unbounded preceding and unbounded following) max_sale ,---3、最大的銷售額

last_value(sale)over

(partition by year

order by sale desc

rows between unbounded preceding and unbounded following) min_sale,---3、最小的銷售額

nth_value(sale,2) over

(partition by year

order by sale desc

rows between unbounded preceding and unbounded following) second_sale---4、第二高的銷售量

from sales_fact

where product ='xtend memory' and country ='australia' and week between 1 and 3 and year in (1998,1999)

order by year,week;

---實驗結果:

-year-week-sale---lag---lead---max_sale-min_sale-second_sale

1998 1 58.15 58.15 29.39 58.15 29.39 29.49

1998 2 29.39 58.15 29.49 58.15 29.39 29.49

1998 3 29.49 29.39 29.49 58.15 29.39 29.49

1999 1 53.52 53.52 94.6 94.6 53.52 53.52

1999 3 94.6 53.52 94.6 94.6 53.52 53.52

unix linux fork函式例項分析

分析一下fork 函式。include include intmain else run in child process.run in parent process.pid 29214.fork函式有三種返回值。大於零表明執行成功 等於零表明建立子程序 小於零表明執行失敗 為什麼if else 會...

select函式及例項分析

select機制中提供了乙個資料結構 struct fd set 可以理解為乙個集合,實際上是乙個位圖,每乙個特定為來標誌相應大小檔案描述符,這個集合中存放的是檔案描述符 file descriptor 即檔案控制代碼 也就是點陣圖上的每一位都能與乙個開啟的檔案控制代碼 檔案描述符 建立聯絡,這個工...

excel統計函式例項分析

內容提要 本文對excel統計函式進行彙總介紹,通過幾個例項和相關的講座資料來理解excel統計函式的應用。excel統計函式的使用頻率很高。常見的excel統計函式有counta,countif,subtotal,sumif,sum,sumproduct函式等等。excel統計函式例項一 統計數值...