jeudi 5 mars 2015

SQLite - how to get all days in week (mo..su) with hour with higher count of rows for day of the week?

I have following table structure: enter image description here


And I would like to get result like this:



-------------------------------------------------------------------
DAY | HOUR | DIALS_CNT | APPT_CNT | CONVERS_CNT | CANNOT_REACH_CNT
-------------------------------------------------------------------
1 (note:means monday) | 13 | 20 | 10 | 5 |
-------------------------------------------------------------------
2 | 8 | 30 | 15 | 7 | 13
-------------------------------------------------------------------
3 | 8 | 30 | 15 | 7 | 13
-------------------------------------------------------------------
4 | 0 | 0 | 0 | 0 | 0
-------------------------------------------------------------------


etc..(zero means that in data is no available value for 4 day = thursday, (wednesday if 0 is Sunday) ).


I tried to do it by this way:



SELECT
DISTINCT strftime('%w', dc.date) as day,
strftime('%H', dc.date) as hour,
COUNT(*) AS DIALS_CNT,
SUM(CASE WHEN dc.call_result = 'APPT' THEN 1 ELSE 0 END) AS 'APPT_CNT',
SUM(CASE WHEN dc.call_result = 'CONV_NO_APPT' THEN 1 ELSE 0 END) AS 'CONVERS_CNT' ,
SUM(CASE WHEN dc.call_result = 'CANNOT_REACH' THEN 1 ELSE 0 END) AS 'CANNOT_REACH_CNT'
FROM dialed_calls dc
GROUP BY
strftime('%w', dc.date),
strftime('%H', dc.date)


But without luck.


Is anybody who know how to solve it in one query please?


Many thanks for any advice.


Aucun commentaire:

Enregistrer un commentaire