postgresql - Two different group by clauses in one query? -
first time posting here, newbie sql, , i'm not sure how word i'll try best.
i have query:
select report_month, employee_id, split_bonus,sum(salary) empsal report_month in('2010-12-01','2010-11-01','2010-07-01','2010-04-01','2010-09-01','2010-10-01','2010-08-01') , employee_id in('100','101','102','103','104','105','106','107') group report_month, employee_id, split_bonus;
now, result of query, want add new column split_bonus_cumulative equivalent adding sum(split_bonus) in select clause case, group buy should have report_month , employee_id.
can show me how single query? in advance.
assuming you're using postgres, might find window functions useful:
http://www.postgresql.org/docs/9.0/static/tutorial-window.html
unless i'm mistaking, want resembles following:
select report_month, employee_id, salary, split_bonus, sum(salary) on w sum_salary, sum(split_bonus) on w sum_bonus empsal ... window w (partition employee_id);
ctes convenient:
http://www.postgresql.org/docs/9.0/static/queries-with.html
with rows ( select foo.* foo ... ), report1 ( select aggregates rows ... ), report2 ( select aggregates rows ... ) select * report1, report2, ...
Comments
Post a Comment