我必须生成如下报告
我必须按年龄分组,并且需要根据奖金,性别,is_permanent和部门的总和显示结果。这种报告的sql查询是什么。我应该创建视图来生成此报告吗?请提出建议。
所有数据应来自EMPLOYEE表,看起来像
empid| DOB | gender| annual_bonus| monthly_bonus| quarterly_bonus| Is_Permanent| Department|
-------------------------------------------------------------------------------------------
101 | 31122002| M | 100 | 300 | 600 | No | Admin|
102 | 31122002| f | 120 | 3020 | 600 | No | Admin|
103 | 31122001| M | 1230 | 300 | 6100 | No | Sales|
104 | 31122001| f | 1100 | 3200 | 1600 | No | Sales|
105 | 31122000| M | 1100 | 300 | 6010 | No | Marketing|
106 | 31122000| f | 1200 | 300 | 600 | No | Marketing|
107 | 31121999| M | 1100 | 300 | 600 | No | Finance|
108 | 31121999| f | 1200 | 300 | 600 | No | Finance|
109 | 31121998| M | 1200 | 300 | 600 | No | Purchase|
110 | 31121998| f | 1200 | 300 | 600 | No | Purchase|
111 | 31121997| M | 1200 | 300 | 600 | No | Transport|
112 | 31121997| f | 1200 | 300 | 600 | No | Transport|
您需要使用conditional aggregate
,如下所示:
Select age,
SUM (CASE WHEN Is_Permanent = 'Y' and gender = 'M' then anual_bonus end) as p_m_a_bonus,
SUM (CASE WHEN Is_Permanent = 'Y' and gender = 'M' then monthly_bonus end) as p_m_m_bonus,
SUM (CASE WHEN Is_Permanent = 'Y' and gender = 'M' then quarterly_bonus end) as p_m_q_bonus,
...
From employees
Where Department = 'Sales'
Group by empid
干杯!