I'm looking for a way to both rename values in a case statement, then group those values together as their sums. It seems that all explanations focus either on creating the condition,setting criteria, or summing. I envision that the query should look something like this:
SELECT
X,
Y,
CASE Z
WHEN '1' THEN 'A'
WHEN '2' THEN 'B'
WHEN '3' THEN 'C'
WHEN '4' THEN 'D'
END +
SUM(CASE WHEN Z='1' OR Z='2' THEN 'I' END) +
SUM(CASE WHEN Z='3' OR Z='4' THEN 'J' END) AS Letters
FROM FOO
I may be making this more difficult than necessary. Really all I need is for 1
and 2
from Z
to group into I
and 3
and 4
from Z
to group into J
such that I
will be the sum of all cases of 1
and 2
and J
will be the sum of all case of 3
and 4
, ideally in the same column. Any help would be greatly appreciated.
As requested by the community, the input data would look something like this:
INSERT INTO FOO
(Z, Values)
VALUES
(1,3),
(2,6),
(3,10),
(4,20),
(5,1),
(1,9),
(2,12),
(3,30)
(4,40)
(5,2);
The outcome should create create a new category that sums values under 1
and 2
together under I
and values under 3
and 4
together under J
. Thus:
Letters Values
I 30
J 100
Where 5
is an excluded condition.