pandas-sql小技巧
問題描述
統計不同產品在不同時間段的銷量,即,A產品統計1-5號,B產品統計3-7號的銷量….
分析
對於這種簡單問題,首先想到的是,用SQL去做,只要提取對應的時間區間就好了。
SQL語法如下:
先取出指定日期區間的數據
-- 先取出指定日期區間的數據
select a.id as 產品ID, a.saledate as 銷售日期, a.salecount as 銷量
from a(銷量表) , b(產品時間表) where a.id=b.id
and a.saledate>=b.startdate(統計開始時間)
and a.saledate<=b.enddate(統計結束時間)
然後再彙總銷量
select id, sum(salecount) as 總銷量 from tb group by id