mysql - How to select count over 2 columns in sql -
can suggest me query combined count 2 columns. specific requirement following:
a b permission -------------------------- 1 2 accept 2 3 accept 3 4 accept 1 6 accept 1 4 accept 2 1 accept 3 1 accept 4 1 pending
i want count of 1 whether belong a
or b
, permission 'accept
'. above example need output 5
you can denormalizing first data using union all
, use count
achieve desired result:
with sampledata(a, b, permission) as( select 1, 2, 'accept' union select 2, 3, 'accept' union select 3, 4, 'accept' union select 1, 6, 'accept' union select 1, 4, 'accept' union select 2, 1, 'accept' union select 3, 1, 'accept' union select 4, 1, 'pending' ) select t.colvalue, valuecount = count(*) ( select col = 'a', colvalue = a, permission sampledata union select col = 'b', colvalue = b, permission sampledata ) t permission = 'accept' group t.colvalue
result
colvalue valuecount ----------- ----------- 1 5 2 3 3 3 4 2 6 1
Comments
Post a Comment