I am a little bit new to SQL. So hope you can help me with this question as I didnt find answer yet. I have joined tables from other sources and I need to create new columns based on unique values of one column with summ of another col. values.
For example, this is my table:
And this is what I need to get (Summ of A for each unique value in B added as a column):
For now I can do it manually like this:
SELECT
EM.[id]
,SUM(CASE WHEN AM.[B]='ZA' THEN EM.[A] ELSE 0 END) "ZA_sum_A"
,SUM(CASE WHEN AM.[B]='ZB' THEN EM.[A] ELSE 0 END) "ZB_sum_A"
FROM [xxx].x.x AS AM
INNER JOIN [yyy].[y].[y] AS EM ON (AM.ELEMENT = EM.ELEMENT)
WHERE ...
GROUP BY EM.[id]
But issue is that I can have hundreds of unique values in B.
So, question how to do it correctly..

