Update
Realised I was doing it correctly. The reason why I had the issue was because I didn't realise my data for Col1 wasn't as expected, having some Col1 that associates with multiple Col0 (It was supposed to be Col1:Col0 1:1 relationship. That's why the confusion of it's not working as intended.
Original Question
I'm using SQL query to sum a column for total revenue of distinct values in one of the columns, and return a table with combining with other attributes.
Here's my table:
Col 0    Col1   Col2(unique)  Revenue
X        1      A             10
X        1      B             20
X        1      C             0
X        2      D             5
X        2      E             8
Y        3      F             3
Y        3      G             0
Y        3      H             50
Desired output:
Col0   Col1   Revenue
X      1      30
X      2      13
Y      3      53
I tried:
WITH
  rev_calc AS (
    SELECT
      Col0,
      Col1,
      Col2, ##this is for further steps to combine other tables for mapping after this
      SUM(Revenue) AS total_revenue, ##total rev by Col1
    FROM table_input
    GROUP BY Col1, Col0, Col2 ##Have to group by Col0 and Col2 too as it raised error because of 'list expression'
    )
SELECT DISTINCT 
  table2.mappedOfCol0,
  rev_calc.Col1,
  rev_calc.Col2,
  rev_calc.total_revenue,
FROM another_table AS table2
LEFT JOIN rev_calc
  ON rev_calc.Col0 = table2.mappedOfCol0
But getting actual output with multiple rows of revenues under a specific Col1.
For example, when i filter by Col1 = 1 in the output table, I get a list of different revenue amount still:
Col1   total_revenue
1      10
1      20
1      0
I thought the GROUP BY should have sum up the revenue by distinctly under Col1. What did I miss out here? I also tried querying first FROM (SELECT DISTINCT Col1....) way but the sum(revenue) is producing a list of different revenue as well
Newbie to SQL here, appreciate if anyone can share any insights here. Thanks.
 
     
    