You don't need crosstab()
for that. Conditional counts do the job:
SELECT callnumber1, callnumber2
, count(calltype = 'CALL-IN' OR NULL) AS call_in
, count(calltype = 'CALL-OUT' OR NULL) AS call_out
, count(calltype = 'SMS-IN' OR NULL) AS sms_in
, count(calltype = 'SMS-OUT' OR NULL) AS sms_out
, min(calldate) AS first_calldate
, max(calldate) AS last_calldate
, count(*) AS total_calls
FROM table1
WHERE calltype in ('CALL-IN','CALL-OUT','SMS-IN','SMS-OUT')
GROUP BY 1,2
ORDER BY 1,2
Use
count(*)
instead ofcount(callnumber1||callnumber2)
, assuming both columns are defined NOT NULL.How does
count(calltype = 'CALL-IN' OR NULL)
work?
Compute percents from SUM() in the same SELECT sql query