私は売上テーブルを持ち、2つの異なる日付範囲を持っています。 つまり、合計販売額は(2016-12-21 - 2016-12-30) is 100
で、期間は(2016-12-11 - 2016-12-20) is 85
です。 私が欲しい結果は1つのクエリで異なる2つの日付範囲のレコードを取得します
100 (sales of 2016-12-21 - 2016-12-30), 85 (sales of 2016-12-11 - 2016-12-20), 15 (difference of both periods)
です。
私は何を考えていることは
select *, (a.sales - b.sales) as diff
from (select id, sum(sales) as sales from salestable where date >= '2016-12-21' and date <= '2016-12-30') a
join (select id, sum(sales) as sales from salestable where date >= '2016-12-11' and date <= '2016-12-20') b
on a.id = b.id;
でこれを行うには、他のより良い方法はありますか?