pgsql query to return single row to find the total count of diff columns on the same date

Multi tool use


pgsql query to return single row to find the total count of diff columns on the same date
I have a table that contains discount codes coupon_code and its vendor name.
eg: coupon_code: abc vendor: newyork
coupon_code: def vendor: usa
Another table has order details which contains date,coupon_code,order_number
eg:
date: 2012-04-07 coupon_code:abc order_number:1
date: 2012-04-08 coupon_code:abc order_number:2
date: 2012-04-08 coupon_code:def order_number:3
date: 2012-04-09 coupon_code:abc order_number:4
How can i get the total number of codes used for each day?
expected result
eg: date newyork[abc] usa[def]
2012-04-07 1 0
2012-04-08 1 1
2012-04-09 1 0
1 Answer
1
Try this
Select date,
count(case when coupon_code = 'abc' then 1 else null end) as newyork,
count(case when coupon_code = 'def' then 1 else null end) as usa
From tbl
group by date
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.