mysql - Combining Selects with two different Group Bys in same query -
i have 1 table 4 columns (1 primary key- not used here)
col1: year
col2: cust_id
col3: units
i want combine these 2 queries generate 1 output, different group by's required.
select cust_id,year,sum(units) table group cust_id;
select cust_id,year,units table group cust_id,year;
independently both work.
when try combine them , group cust_id,year sum(units) gets sum of specific year (which correct non-sum query), whereas need sum of years cust_id same.
you need define want achieve.
if want cust_id , sum of units years: select cust_id,sum(units) table group cust_id
if want cust_id , year plus sum of years customer:
select t1.cust_id,t1.year,sum(t1.units) unitsforyear,(select sum(t2.units) table t2 t1.cust_id = t2.cust_id) unitsforallyears table t1 group cust_id,year
Comments
Post a Comment