的時候我就喜歡做這樣的事情使用子查詢,而不使用子查詢有總:選擇總不分割
select
category,
date,
sum(qty)/(select sum(qty) from table where t.date= table.date) as share_of_total
from table t
group by category,date;
表看起來是這樣的:
category date qty
1 2016-11-01 3
1 2016-12-01 4
2 2016-12-01 6
2 2016-11-01 7
預期結果如下:
category date share_of_total
1 2016-11-01 30%
2 2016-11-01 70%
1 2016-12-01 40%
2 2016-12-01 60%
請給出表格定義和數據,以幫助您完成 –
!希望現在更清楚。 –