關系數據庫系統和混合/云數據管理解決方案的用戶都可以使用SQL靈活地訪問業務數據,并以創新的方式進行轉換或顯示。
對業務數據庫結構的透徹了解,對上游數據進行轉換和聚合的巧妙解決方案sql 查前 條平均值,對于高效,完善的ETL至關重要。這是我在構建復雜的管道時學到的一些技巧,這些技巧使我的工作輕松而有趣。
一、計算滾動平均
使用時間序列數據時,為觀察值計算滾動平均值或附加歷史值可能會有所幫助。假設我想獲取一家公司每天售出的小部件數量。我可能想包括7天移動平均線,或附上上周出售的工作日小部件,以查看業務與上周相比的表現。我可以通過將數據集連接到自身上,并使用日期列上的操作來選擇單個值或觀察范圍來做到這一點。
首先,讓我們開始使用Db2庫中名為的表中的以下代碼sql 查前 條平均值,按日銷售小部件:
select t1.date
, sum(t1.widgets_sold) as total_widgets_sold
from db2.widget_order_history t1
where t1.date between ‘2021–04–05’ and ‘2021–05–01’
group by t1.date
如果我們想在表的另一個變量(例如特定存儲)上添加任何過濾條件,則可以添加一個簡單的WHERE語句:
select t1.date
, sum(t1.widgets_sold) as total_widgets_sold
from db2.widget_order_history t1
where (t1.date between ‘2021–04–05’ and ‘2021–05–01’)
and t1.store = 1234
group by t1.date
位于其他表上的其他條件(即)將需要附加的INNER JOIN:
select t1.date
, sum(t1.widgets_sold) as total_widgets_sold
from db2.widget_order_history t1

inner join (
select store
from db2.store_data
where state = ‘NY’
) t2
on t1.store = t2.store
where t1.date between ‘2021–04–05’ and ‘2021–05–01’
group by t1.date
從提供的代碼生成的示例時間序列數據:
二、自連接附加歷史數據
現在,如果我想附加4/25 / 21–5 / 1/21這一周的7天滾動平均值,可以通過將表連接到自身上并利用在SUM()函數。
當您只想滿足表中的特定條件時,可以使用此技術來使用分組功能(即SUM(),COUNT(),MAX())。它只會對滿足WHEN子句中包含的規則的值求和。
在下面的示例中,如果表B的值在表A上當前觀察日期的前7天之內,我們可以將這些銷售量相加并除以7,以獲得表A的每一行的每周滾動平均值:
select a.date
, a.total_widgets_sold
, sum(
case when (b.date between a.date-7 and a.date-1)
then b.total_widgets_sold
else 0
end)/7 as seven_day_avg

from (
select date
, sum(widgets_sold) as total_widgets_sold
from db2.widget_order_history
where date between ‘2021–04–25’ and ‘2021–05–01’
group by date
) a
left join (
select date
, sum(widgets_sold) as total_widgets_sold
from db2.widget_order_history
where date between ‘2021–04–05’ and ‘2021–05–01’
group by date
) b
on a.date = b.date
group by a.date
, a.total_widgets_sold
order by a.date
2021日歷年第17周的小部件銷售,其7天平均值處于滾動狀態:
如果要將歷史值附加到每個觀察值,則可以避免聚合,而只需根據指定間隔時間的日期加入表即可。
下面的示例將表B聯接到表A上,以將日期回溯7天以獲取前一個工作日的小部件銷售:
select a.date
, a.total_widgets_sold
, b.total_widgets_sold as prev_wkday_sales
from (
select date
, sum(widgets_sold) as total_widgets_sold
from db2.widget_order_history
where date between ‘2021–04–25’ and ‘2021–05–01’
group by date
) a
left join (
select date
, sum(widgets_sold) as total_widgets_sold
from db2.widget_order_history
where date between ‘2021–04–04’ and ‘2021–05–01’
group by date
) b
on a.date -7 = b.date
第20周第2021日歷年的窗口小部件銷售以及上周的工作日窗口小部件銷售:
將表聯接到自身上是一種非常靈活的方式,可以向數據集添加匯總列和計算列。
分組功能(例如SUM()和COUNT()與CASE()語句)的創造性使用為功能工程,分析報告和各種其他用例帶來了巨大的機會。
在實踐中,如果查詢通過子查詢加入自身,并且查詢量很大,則可以預期運行時間很長。解決此問題的一種方法是使用臨時表來保存具有特定問題標準的初步結果。
例如,在SAS的WORK庫中為整個時間范圍創建一個小部件銷售表,并多次查詢該表。高效的代碼結構(例如使用索引)也可以提高效率。
三、使用CASE語句處理復雜的邏輯
CASE語句的語法與整個數據科學中其他常用編程語言的邏輯不同(請參閱: / R)。
通過使用偽代碼對邏輯規則進行周到的設計可以幫助避免由于不正確/不一致的規則而導致的錯誤。了解如何在SQL中編碼嵌套邏輯對于釋放數據中的潛力至關重要。
假設有一張購物者表,其中包含給定時間范圍內的年齡,家庭狀態和銷售情況等大量特征。有針對性的營銷活動正用于嘗試提高普通購物者的銷售額(已將平均購物者確定為消費在$ 100- $ 200之間的人)。
一旦被識別,Z世代/千禧一代購物者將獲得數字優惠券,所有其他購物者將被郵寄一張印刷優惠券,該打印優惠券將根據他們所居住的州而有所不同。為簡單起見,只有三個州的購物者居住。
這是在R和SQL中如何編碼此邏輯的方法:
## Example of Nested Logic in R
if(shoppers$sales<=0){ print("Error: Negative/No Sales")}
else if(shoppers&sales<=100){ print("Shopper has below-average sales.")}
else if(shoppers&sales<=200){
if(shopper$age<41){print("Shopper has average sales and is Gen Z/Millennial.")}
else{
if(shopper$state=='NJ'){print("Shopper has average sales, is Gen X/Boomer/Greatest Gen., and lives in New Jersey.")}
else if(shopper$state=='NY'){print("Shopper has average sales, is Gen X/Boomer/Greatest Gen., and lives in New York.")
else(shopper$state=='CT'){print("Shopper has average sales, is Gen X/Boomer/Greatest Gen., and lives in Connecticut.")}
}
}
else{print("Shopper has above-average sales.")}

*Example of nested logic in SQL. No need to actually nest statements!;
, case when sales < 0
then 'Error: Negative/No Sales.'
when sales <=100
then 'Shopper has below-average sales.'
when sales <=200 and age <41
then 'Shopper has average sales and is Gen Z/Millennial.'
when sales <=200 and state = 'NJ'
then 'Shopper has average sales, is Gen X/Boomer/Greatest Gen., and lives in New Jersey.'
when sales <=200 and state = 'NY'
then 'Shopper has average sales, is Gen X/Boomer/Greatest Gen., and lives in New York.'
when sales <=200 and state = 'CT'
then 'Shopper has average sales, is Gen X/Boomer/Greatest Gen., and lives in Connecticut.'
else 'Shopper has above-average sales.'
end as shopper_classification
周到地使用CASE語句將使您能夠構建復雜業務邏輯的任何組合。
但是,SQL邏輯與其他編程語言所需要的思維方式略有不同。
結合分組功能,這些工具可以為數據科學家提供競爭優勢,以獲取和轉換用于特征工程,商業智能,分析報告等的數據源!