使用ORACLE SQL。
我有一个表'Employees‘,其中一个属性是'hire_date’。我的任务(书本练习)是写一个SELECT,告诉我1995年、1996年、1997年和1998年雇佣了多少员工。
类似于:
TOTAL     1995     1996     1997     1998
-----------------------------------------
20        4        5        29       2单独计算每年的雇员人数很容易,例如:
SELECT
  COUNT(*),
FROM
  employees e
WHERE
  e.hire_date like '%95'但是,当我必须以所需的格式‘聚合’数据时,我遇到了困难。有什么建议吗?
发布于 2010-11-17 21:06:06
语法不直观。这利用了剪切粘贴代码:
SQL> select
  2      sum(case when to_char(hiredate, 'YYYY') = '1980' then 1 else 0 end) as "1980"
  3      , sum(case when to_char(hiredate, 'YYYY') = '1981' then 1 else 0 end) as "1981"
  4      , sum(case when to_char(hiredate, 'YYYY') = '1982' then 1 else 0 end) as "1982"
  5      , sum(case when to_char(hiredate, 'YYYY') = '1983' then 1 else 0 end) as "1983"
  6      , sum(case when to_char(hiredate, 'YYYY') = '1987' then 1 else 0 end) as "1987"
  7      , count(*) as total
  8  from emp
  9  /
      1980       1981       1982       1983       1987      TOTAL
---------- ---------- ---------- ---------- ---------- ----------
         1         10          1          0          2         20
Elapsed: 00:00:00.00
SQL>https://stackoverflow.com/questions/4204492
复制相似问题