sql - How to get multiple counts based on different criterias -
i need making query found site first time , hoping here knows how it.
lets database has 3 columns (name,type,and decision). in name field there 100 records , there can duplicates. there 2 different types (online , offline), , 2 kinds of decisions (match or mismatch).
what need count of matches , mismatches each type, grouped name.
the table columns below:
name|online match count|online mismatch count|offline match count|offline mismatch count|
also if of fields have count of 0, want display 0 well.
does know how this? appreciate it.
this common technique , called pivot query.
select name, sum(case when type='online' , decision='match' 1 else 0 end) "online match count", sum(case when type='online' , decision='mismatch' 1 else 0 end) "online mismatch count", sum(case when type='offline' , decision='match' 1 else 0) "offline match count", sum(case when type='offline' , decision='mismatch' 1 else 0 end) "offline mismatch count" tablename group name
Comments
Post a Comment