创建一个查询以显示员工总数,以及总数中的员工数量 在2005年,2006年,2007年和2008年聘用。创建适当的列标题。
到目前为止,我已设法做到这一点:
SELECT COUNT(DISTINCT employee_id) AS "Total",
(CASE WHEN hire_date BETWEEN '01-01-2005' AND '31-12-2005' THEN '2005'
WHEN hire_date BETWEEN '01-01-2006' AND '31-12-2006' THEN '2006'
WHEN hire_date BETWEEN '01-01-2007' AND '31-12-2007' THEN '2007'
WHEN hire_date BETWEEN '01-01-2008' AND '31-12-2008' THEN '2008'
ELSE 'Out of Range'
END ) AS "Years"
FROM employees
WHERE hire_date BETWEEN '31-12-2005' AND '01-01-2008'
GROUP BY hire_date;
但输出并不是应有的。由于我的声誉很低,无法上传图片,以确切了解我的意思:)。无论如何,有什么建议吗?
答案 0 :(得分:1)
您应该使用这样的格式yyyy-MM-dd
SELECT COUNT(DISTINCT employee_id) AS "Total",
SUM( CASE WHEN hire_date BETWEEN '2005-01-01' AND '2005-12-31' THEN 1 else 0 end) as
'2005',
SUM( CASE WHEN hire_date BETWEEN '2006-01-01' AND '2006-12-31' THEN 1 else 0 end) as
'2006',
SUM( CASE WHEN hire_date BETWEEN '2007-01-01' AND '2007-12-31' THEN 1 else 0 end) as
'2007',
SUM( CASE WHEN hire_date BETWEEN '2008-01-01' AND '2008-12-31' THEN 1 else 0 end) as
'2008',
SUM( CASE WHEN hire_date < '2005-01-01' OR hire_date > '31-12-2008'
THEN 1 else 0 end) as
"Out of Range"
FROM employees
答案 1 :(得分:0)
您可以将查询更改为
SELECT COUNT(employee_id) AS "Total",
SUM(case when hire_date BETWEEN '01-01-2005' AND '31-12-2005' THEN 1 else 0 end) as '2005',
SUM(case WHEN hire_date BETWEEN '01-01-2006' AND '31-12-2006' THEN 1 else 0 end) as '2006',
SUM(case WHEN hire_date BETWEEN '01-01-2008' AND '31-12-2008' THEN 1 else 0 end) as '2008',
SUM(case WHEN hire_date BETWEEN '01-01-2007' AND '31-12-2007' THEN 1 else 0 end) as '2007'
FROM employees
GROUP BY hire_date;