sql查询想要以不同的方式使用 [英] sql query want to use differently

查看:101
本文介绍了sql查询想要以不同的方式使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是查询,我们可以使用不同的方式..



  SELECT  
SELECT COUNT(id) FROM 员工 WHERE 年(hire_date)= 2011) as hirecnt,
SELECT COUNT(id) FROM 员工 WHERE 年(termination_date)= 2011) as termcnt







i尝试下面的查询...它工作不正常





选择计数(id)为cnt,Employment_status来自员工

where(YEAR (termination_date)= 2010和employment_status ='已终止')



(年(hire_date)= 2010和employment_status ='有效')

group by employment_status

解决方案

;  WITH  EMP_CTE  AS  
SELECT * 来自员工 WHERE ( YEAR(hire_date)= 2011) OR (YEAR(termination_date)= 2011))
SELECT SUM(hirecnt) as hirecnt,SUM(termcnt) as termcnt FROM

SELECT COUNT(*) as hirecnt , 0 as termcnt FROM EMP_CTE WHERE 年(hire_date)= 2011
UNION ALL
SELECT COUNT(*) as termcnt, 0 as hirecnt FROM EMP_CTE WHERE 年(termination_date)= 2011
as ÿ


Hi , is this below query can we use differently..

SELECT
(SELECT COUNT(id) FROM Employees WHERE YEAR(hire_date)=2011) as hirecnt,
(SELECT COUNT(id) FROM Employees WHERE YEAR(termination_date)=2011)as termcnt




i try below query like this ...its not working properly


select count(id) as cnt,employment_status from Employees
where ( YEAR(termination_date)=2010 and employment_status='Terminated')
or
( YEAR(hire_date)=2010 and employment_status='Active')
group by employment_status

解决方案

;WITH EMP_CTE AS
(SELECT *   from Employees WHERE (YEAR(hire_date)=2011) OR (YEAR(termination_date)=2011))
SELECT SUM(hirecnt) as hirecnt, SUM(termcnt) as termcnt FROM
(
SELECT COUNT(*)  as hirecnt, 0 as termcnt FROM EMP_CTE WHERE YEAR(hire_date)=2011
UNION ALL
SELECT COUNT(*) as termcnt , 0 as hirecnt FROM EMP_CTE WHERE YEAR(termination_date)=2011
) as y


这篇关于sql查询想要以不同的方式使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆