2015-06-14 72 views
0

我想,當我運行此查詢我將數據從SQL Server中的格式如下取2000count行一個條件在SQL SERVER 2000

Department | TotalPresent | Employee | Officer 
XYZ    12   6   6 

我運行此查詢

SELECT  a.department, 
      Count(emp_id) AS totalpresent, 
      CASE 
         WHEN a.type = 'Employee' THEN Count(a.type) 
         ELSE 0 
      END AS employee, 
      CASE 
         WHEN a.type = 'Officer' THEN Count(a.type) 
         ELSE 0 
      END AS officer 
FROM  attendancelog m 
INNER JOIN employeedata a 
ON   m.emp_id = a.emp_id groupby a.type, 
      a.department 

得到這種不正確的數據

Department | TotalPresent | Employee | Officer 
    XYZ    12   6   0 
    XYZ    12   0   6 

請糾正我什麼似乎是問題爲什麼它返回m Ë相同的記錄兩次,但滿足下述條件

所有我想是指望條件的行類型是否爲僱員或官員

回答

1

取下group bytype和移動casesum()內:

Select a.department, count(emp_id) as TotalPresent, 
     sum(case when a.Type = 'Employee' THEN 1 ELSE 0 END) as Employee, 
     sum(case when a.Type = 'Officer' THEN 1 ELSE 0 END) as Officer 
from AttendanceLog m INNER JOIN 
    EmployeeData a 
    ON m.emp_id = a.emp_id 
GroupBy a.Department; 

此外,升級您的SQL Server。多年來,SQL Server 2000一直是不受支持的。您應該嘗試使用支持的軟件。

+0

thanx一噸,正在犯一個愚蠢的錯誤。那麼我們從sql server 2000升級需要時間,很少有應用程序依賴它 –