算DISTINCT员工

时间:2014-01-07 08:02:59

标签: sql count sum distinct

请帮我计算一下我的查询返回的行数,显示一些工作在特定月份的员工。

我的总时数,模式和时间员工姓名,但我如何返回当月工作的确切员工数量?

Count(DISTINCT LD.Employee)显示每位员工出现一次,但如何添加1来获得52作为我的最终答案?

SELECT LD.Period, LD.EmOrg, LD.EmType, LD.Employee, 
    Sum(LD.RegHrs) AS 'Total Hours', Count(LD.RegHrs) AS 'Mode', 
    Count(DISTINCT LD.Employee) AS 'Counted Employees'
FROM SSI.dbo.LD LD
GROUP BY LD.Employee, LD.Period, LD.EmOrg, LD.EmType
HAVING (LD.EmOrg Like '%T00') AND (LD.EmType='S')

数据:

Period  EmOrg   EmType  Total Hours Mode    Employee Counted Employees
201306  RSA/PZB/T00 S   180 66  GOVINP1  1
201306  RSA/PZB/T00 S   200 35  LANDCJ1  1
201306  RSA/PZB/T00 S   180 34  WOODRE   1
201306  RSA/JNB/T00 S   160 33  MOKOHM1  1
201306  RSA/PLZ/T00 S   160 32  KAPPPJ   1
201306  RSA/PZB/T00 S   187 31  CAHISJ   1
201306  RSA/PZB/T00 S   180 31  ZEMUN    1
201306  RSA/PZB/T00 S   190 28  SAULDD1  1
201306  RSA/DUR/T00 S   188 26  JEROP1   1
201306  RSA/PZB/T00 S   204 24  NGOBS1   1
201306  RSA/PZB/T00 S   201 23  DLAMMP   1
201306  RSA/PZB/T00 S   192 23  ZONDNS2  1
201306  RSA/PLZ/T00 S   160 22  MPHURK   1
201306  RSA/PZB/T00 S   186 21  DUBESE1  1
201306  RSA/PZB/T00 S   187 21  MABAMS   1
201306  RSA/PZB/T00 S   189 21  MBELBV   1
201306  RSA/PZB/T00 S   188 21  MNDAMB   1
201306  RSA/PZB/T00 S   186 21  ZONDIPB1 1
201306  RSA/JNB/T00 S   180 20  BOKAML   1
201306  RSA/PTA/T00 S   180 20  DAVEJA   1
201306  RSA/PZB/T00 S   180 20  DLAMVK   1
201306  RSA/JNB/T00 S   163.5   20  ERLAME   1
201306  RSA/PZB/T00 S   180 20  HLONLA   1
201306  RSA/CPT/T00 S   179 20  HOUGHJ   1
201306  RSA/PZB/T00 S   180 20  KAFFS    1
201306  RSA/PTA/T00 S   180 20  KEYTDJ   1
201306  RSA/JNB/T00 S   180 20  KHATEK   1
201306  RSA/DUR/T00 S   180 20  KHUZB1   1
201306  RSA/PTA/T00 S   180 20  LEHLTJ   1
201306  RSA/JNB/T00 S   180 20  LETSMM2  1
201306  RSA/PTA/T00 S   170 20  MAKGMJ   1
201306  RSA/BDV/T00 S   160 20  MALEVT1  1
201306  RSA/JNB/T00 S   180 20  MAPHV1   1
201306  RSA/JNB/T00 S   180 20  MNGOTX   1
201306  RSA/PZB/T00 S   180 20  MTHETN1  1
201306  RSA/BDV/T00 S   180 20  MULAG1   1
201306  RSA/DUR/T00 S   180 20  NDLOCT1  1
201306  RSA/PZB/T00 S   180 20  NDLOS    1
201306  RSA/BFN/T00 S   180 20  NELL     1
201306  RSA/PLZ/T00 S   180 20  OOSTLC   1
201306  RSA/CPT/T00 S   180 20  PAULDMR  1
201306  RSA/PZB/T00 S   180 20  PHIRSN1  1
201306  RSA/PZB/T00 S   180 20  RAMBD1   1
201306  RSA/PZB/T00 S   180 20  SANLLJ   1
201306  RSA/PZB/T00 S   180 20  SMITEP3  1
201306  RSA/CPT/T00 S   180 20  SOCEGR   1
201306  RSA/PZB/T00 S   180 20  SOKOV    1
201306  RSA/CPT/T00 S   180 20  TSHIJC   1

1 个答案:

答案 0 :(得分:0)

如果您需要员工总数,则需要删除GROUP BY LD.Employee子句。

这就是为什么你的COUNT( LD.Employee )永远是一个。请尝试删除GROUP