如何在不使用Union的情况下获取页脚部分的人数总和

时间:2017-05-05 06:37:13

标签: sql asp.net sql-server asp.net-mvc

如何在不使用Union的情况下获取页脚部分的人数总和。任何人都可以提供一些想法

SELECT DeptID, COUNT(EmpID) AS TotalHeadCount  FROM EmployeeDetails          
WHERE (datepart(yyyy,DOJ) between 2005 and 2017) and Status =0
group by DeptID
Union 
Select Count(EmpID) AS T1 From EmployeeDetails WHERE Status =0`

2 个答案:

答案 0 :(得分:0)

我认为您的查询存在一些语法错误。无论如何,如果总计必须具有相同的部分总数(年度范围),你可以使用: (我简化了创建date_year列的表,但是你可以替换你的条件)

SELECT DeptID, COUNT(EmpID) AS TotalHeadCount  
FROM EmployeeDetails 
WHERE DATE_YEAR between 2005 and 2017 
    AND STATUS =0 
GROUP BY DEPTID
WITH ROLLUP

示例数据:

CREATE TABLE EmployeeDetails (EmpID INT, DEPTID INT, DATE_YEAR INT, STATUS INT);
INSERT INTO EmployeeDetails VALUES (1,1,2004,0);
INSERT INTO EmployeeDetails VALUES (2,1,2005,0);
INSERT INTO EmployeeDetails VALUES (3,1,2005,0);
INSERT INTO EmployeeDetails VALUES (4,1,2006,0);
INSERT INTO EmployeeDetails VALUES (5,1,2007,0);
INSERT INTO EmployeeDetails VALUES (5,1,2008,0);
INSERT INTO EmployeeDetails VALUES (6,1,2009,0);

输出:

DeptID      TotalHeadCount
----------- --------------
1           6
NULL        6

答案 1 :(得分:0)

这是怎么回事,

SELECT DeptID, COUNT(EmpID) AS TotalHeadCount  FROM EmployeeDetails          
WHERE (datepart(yyyy,DOJ) between 2005 and 2017) and Status =0
group by DeptID WITH CUBE