This is a query I came up with when I wanted to calculate the usage rate of a new payment feature on an EC site.
It happened to combine several aggregate functions into a single query, so I put it together here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
SELECT DATE_FORMAT(create_date, '%Y-%m-%d') AS 日付 ,COUNT(order_id) AS 全件数 ,FORMAT(SUM(payment_total),0) AS "全支払い合計(円)" ,COUNT(payment_id =12orNULL) AS "新決済機能の購入件数" ,COUNT(customer_id =0orNULL) AS "ゲスト購入数" ,FORMAT(SUM(CASEWHEN payment_id =12THEN payment_total else0END),0) AS "新決済機能の購入支払い合計(円)" ,count(payment_id =12orNULL)/count(order_id) *100AS "新決済機能の購入比率(%)" ,SUM(CASEWHEN payment_id =12THEN payment_total else0END)/SUM(payment_total) *100AS "新決済機能の購入支払い合計比率(%)" ,COUNT(customer_id =0orNULL)/COUNT(order_id) *100AS "ゲスト購入比率" FROM dtb_order WHERE1=1 AND site_id =1 AND create_date >'2016-09-21 10:00:00' GROUPBY DATE_FORMAT(create_date, '%Y%m%d') ;
Package = EC-CUBE 2.11.5
New payment ID = 12
Result
Being able to write a CASE statement casually makes me feel a little more grown up. My one regret is that it would be cooler if the duplicated parts in the ratio calculations could be consolidated.