Last active
December 13, 2015 17:48
-
-
Save LuckOfWise/4950733 to your computer and use it in GitHub Desktop.
売上明細的なテーブルで、各ショップ、日ごとに対象の日から1年前までの集計値を求める。
これ合ってんのか??
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
相関クエリ | |
select T1.shop_id, | |
T1.sales_date, | |
( select sum(cash_amount) | |
from shop_sales_details T2 | |
where T2.shop_id = T1.shop_id | |
and T2.sales_date between T1.sales_date + cast('-1 year' as interval) and T1.sales_date | |
) as cash_amount, | |
( select sum(other0) | |
from shop_sales_details T2 | |
where T2.shop_id = T1.shop_id | |
and T2.sales_date between T1.sales_date + cast('-1 year' as interval) and T1.sales_date | |
) as other0, | |
( select sum(other1) | |
from shop_sales_details T2 | |
where T2.shop_id = T1.shop_id | |
and T2.sales_date between T1.sales_date + cast('-1 year' as interval) and T1.sales_date | |
) as other1, | |
( select sum(other2) | |
from shop_sales_details T2 | |
where T2.shop_id = T1.shop_id | |
and T2.sales_date between T1.sales_date + cast('-1 year' as interval) and T1.sales_date | |
) as other2 | |
from shop_sales_details T1 | |
不等値結合で無理矢理 | |
select T1.shop_id, | |
T1.sales_date, | |
sum(T2.cash_amount) as cash_amount, | |
sum(T2.other0) as other0, | |
sum(T2.other1) as other1, | |
sum(T2.other2) as other2 | |
from shop_sales_details T1 | |
left outer join shop_sales_details T2 | |
on T2.shop_id = T1.shop_id | |
and T2.sales_date between T1.sales_date + cast('-1 year' as interval) and T1.sales_date | |
group by | |
T1.shop_id, | |
T1.sales_date |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment