0
我需要動態更改用於分組的GROUP BY子句規則。 始終返回相同的列號和名稱。SQL動態組按不同列數
例如表:
CREATE TABLE employees
(
employee_id NOT NULL,
department_id integer,
status character varying(50), /*active/inactive*/
cost numeric,
income (numeric),
first_name character varying(50),
last_name character varying(50)
)
我需要查詢是這樣的:
select
count(employee_id) as contacts,
sum(cost) as cost,
sum(income)/count(employee_id) as average_salary
from
employees
group by
case
when status = 'active' then /*group by*/ department_id, employee_id
when status = 'inactive' then /*group by*/ employee_id
end;
我如何運行此查詢?
非常感謝,它幫了我很多 – peterko 2014-10-13 11:04:56