If I have a case expression is there a way it does not work as a waterfall?
For example if I have the following dataset:
group value
---------------
derek 5
cait 1
david 2
denver 3
Is there a way I can show the following results
CASE
WHEN group like 'd%' then d
WHEN group like 'de% then de
ELSE other
To show
group value
--------------
d 10
de 8
other 3
Right now it shows as
group value
--------------
d 10
other 2
You can use a left join
instead:
select coalesce(which, 'other'), sum(t.value)
from t left join
(values ('d%', 'd'), ('de%', 'de')
) v(pattern, which)
on t.group like v.pattern
group by which;
you can use UNION.
SELECT 'd' [group], SUM(Value)[Value] FROM <table_name> WHERE [GROUP] LIKE 'd%'
UNION
SELECT 'de' [group], SUM(Value)[Value] FROM <table_name> WHERE [GROUP] LIKE 'de%'
UNION
SELECT 'other' [group], SUM(Value)[Value] FROM <table_name> WHERE [GROUP] NOT LIKE 'd%' OR [GROUP] NOT LIKE 'de%'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.