— TODO: This query will return a table with the revenue by month and year. It
— will have different columns: month_no, with the month numbers going from 01
— to 12; month, with the 3 first letters of each month (e.g. Jan, Feb);
— Year2016, with the revenue per month of 2016 (0.00 if it doesn’t exist);
— Year2017, with the revenue per month of 2017 (0.00 if it doesn’t exist) and
— Year2018, with the revenue per month of 2018 (0.00 if it doesn’t exist).
I write this code: SELECT
strftime(‘%m’, olist_orders.order_purchase_timestamp) AS month_no,
CASE
WHEN strftime(‘%m’, olist_orders.order_purchase_timestamp) = ’01’ THEN ‘Jan’
WHEN strftime(‘%m’, olist_orders.order_purchase_timestamp) = ’02’ THEN ‘Feb’
WHEN strftime(‘%m’, olist_orders.order_purchase_timestamp) = ’03’ THEN ‘Mar’
WHEN strftime(‘%m’, olist_orders.order_purchase_timestamp) = ’04’ THEN ‘Apr’
WHEN strftime(‘%m’, olist_orders.order_purchase_timestamp) = ’05’ THEN ‘May’
WHEN strftime(‘%m’, olist_orders.order_purchase_timestamp) = ’06’ THEN ‘Jun’
WHEN strftime(‘%m’, olist_orders.order_purchase_timestamp) = ’07’ THEN ‘Jul’
WHEN strftime(‘%m’, olist_orders.order_purchase_timestamp) = ’08’ THEN ‘Aug’
WHEN strftime(‘%m’, olist_orders.order_purchase_timestamp) = ’09’ THEN ‘Sep’
WHEN strftime(‘%m’, olist_orders.order_purchase_timestamp) = ’10’ THEN ‘Oct’
WHEN strftime(‘%m’, olist_orders.order_purchase_timestamp) = ’11’ THEN ‘Nov’
WHEN strftime(‘%m’, olist_orders.order_purchase_timestamp) = ’12’ THEN ‘Dec’
ELSE ‘Unknown’
END AS month,
SUM(CASE WHEN strftime(‘%Y’, olist_orders.order_purchase_timestamp) = ‘2016’ THEN olist_order_payments.payment_value ELSE 0.00 END) AS Year2016,
SUM(CASE WHEN strftime(‘%Y’, olist_orders.order_purchase_timestamp) = ‘2017’ THEN olist_order_payments.payment_value ELSE 0.00 END) AS Year2017,
SUM(CASE WHEN strftime(‘%Y’, olist_orders.order_purchase_timestamp) = ‘2018’ THEN olist_order_payments.payment_value ELSE 0.00 END) AS Year2018
FROM olist_order_payments
INNER JOIN olist_orders ON olist_order_payments.order_id = olist_orders.order_id
WHERE
olist_orders.order_status = ‘delivered’
AND olist_orders.order_delivered_customer_date IS NOT NULL
GROUP BY
strftime(‘%m’, olist_orders.order_purchase_timestamp)
ORDER BY
CAST(strftime(‘%m’, olist_orders.order_purchase_timestamp) AS INTEGER);
Results are:
month_no month Year2016 Year2017 Year2018
0 01 Jan 0.00 127545.67 1078606.86
1 02 Feb 0.00 271298.65 966510.88
2 03 Mar 0.00 414369.39 1120678.00
3 04 Apr 0.00 390952.18 1132933.95
4 05 May 0.00 566872.73 1128836.69
5 06 Jun 0.00 490225.60 1011561.35
6 07 Jul 0.00 566403.93 1027383.10
7 08 Aug 0.00 646000.61 985414.28
8 09 Sep 0.00 701169.99 0.00
9 10 Oct 46566.71 751140.27 0.00
10 11 Nov 0.00 1153393.22 0.00
11 12 Dec 19.62 843199.17 0.00
but the corrects are:
month_no month Year2016 Year2017 Year2018
0 01 Jan 0.00 37632.57 969967.80
1 02 Feb 0.00 222270.75 853616.82
2 03 Mar 0.00 376833.72 1024851.95
3 04 Apr 0.00 299798.45 1274742.18
4 05 May 0.00 579280.43 1150528.93
5 06 Jun 0.00 489463.42 1141543.85
6 07 Jul 0.00 518115.19 925958.79
7 08 Aug 0.00 609180.34 1319737.66
8 09 Sep 0.00 652576.48 12875.18
9 10 Oct 34116.28 740570.40 347.95
10 11 Nov 10734.64 733047.33 0.00
11 12 Dec 960.85 1082600.69 0.0
help me debbuging my code
Gilberto Perpinan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.