SQL - 根据 2 个限制计算行数
SQL - Count rows based on 2 restrictions
我有一个 employees
table 和一个 hire_date
列,我想做的查询是计算 1995 年和 1996 年雇用了多少员工。1995 年在哪里有自己的专栏,1996 有自己的专栏:
______________
| | |
| 1995 | 1996 |
|______|______|
| 2 | 3 |
|______|______|`
伪代码:
display COUNT of rows WHERE hire_date LIKE '%95', display COUNT of rows WHERE hire_date LIKE '%96'
谁能指导我如何实现这一目标?
SELECT (SELECT COUNT(*) FROM employees WHERE hire_date = '1995') "1995",
(SELECT COUNT(*) FROM employees WHERE hire_date = '1996') "1996"
FROM dual;
使用此查询,您只能访问员工一次 table
select
sum(case TO_CHAR(hire_date, 'YYYY') when '1995' then 1 end) as "1995",
sum(case TO_CHAR(hire_date, 'YYYY') when '1996' then 1 end) as "1996"
from employees
where hire_date between date '1995-01-01' and date'1996-12-31';
我有一个 employees
table 和一个 hire_date
列,我想做的查询是计算 1995 年和 1996 年雇用了多少员工。1995 年在哪里有自己的专栏,1996 有自己的专栏:
______________
| | |
| 1995 | 1996 |
|______|______|
| 2 | 3 |
|______|______|`
伪代码:
display COUNT of rows WHERE hire_date LIKE '%95', display COUNT of rows WHERE hire_date LIKE '%96'
谁能指导我如何实现这一目标?
SELECT (SELECT COUNT(*) FROM employees WHERE hire_date = '1995') "1995",
(SELECT COUNT(*) FROM employees WHERE hire_date = '1996') "1996"
FROM dual;
使用此查询,您只能访问员工一次 table
select
sum(case TO_CHAR(hire_date, 'YYYY') when '1995' then 1 end) as "1995",
sum(case TO_CHAR(hire_date, 'YYYY') when '1996' then 1 end) as "1996"
from employees
where hire_date between date '1995-01-01' and date'1996-12-31';