如何在SQL Server中计数?

时间:2017-03-27 11:55:26

标签: sql-server

当我用这个来计算在每个部门工作的员工数量时,它的工作正常:

-- Count
    totals.Nr_Employees as TotalEmployees

    LEFT JOIN
        (select p1.DepartmentName, count(distinct u.Id) Nr_Employees
        from Paychecks p1
        join Users u on u.Id = p1.UserId
        where u.customerid=214 and u.isdeleted=0
        group by p1.DepartmentName 
        )  totals on totals.DepartmentName = p.DepartmentName

但是,当我想总计数或总数时,我会做这样的事情,但它不起作用,我不知道它的正确做法与否!有人可以指点我正确的方向吗?

-- SUM
    SummerCount.TotalEmployees as sumEmployees
    LEFT JOIN
        (select p1.DepartmentName, sum(count(distinct u.Id)) TotalEmployees
        from Paychecks p1
        join Users u on u.Id = p1.UserId
        where u.customerid=214 and u.isdeleted=0
        group by p1.DepartmentName
        ) SummerCount on SummerCount.DepartmentName = p.DepartmentName

AND,将Querys放在一起:

-- Count & SUM
    totals.Nr_Employees as TotalEmployees,
    SummerCount.TotalEmployees as sumEmployees

    FROM

    LEFT JOIN
        (select p1.DepartmentName, count(distinct u.Id) Nr_Employees
        from Paychecks p1
        join Users u on u.Id = p1.UserId
        where u.customerid=214 and u.isdeleted=0
        group by p1.DepartmentName 
        )  totals on totals.DepartmentName = p.DepartmentName

    LEFT JOIN
        (select p1.DepartmentName, sum(count(distinct u.Id)) TotalEmployees
        from Paychecks p1
        join Users u on u.Id = p1.UserId
        where u.customerid=214 and u.isdeleted=0
        group by p1.DepartmentName
        ) SummerCount on SummerCount.DepartmentName = p.DepartmentName


截图(错误的总和)enter image description here

2 个答案:

答案 0 :(得分:0)

使用公用表表达式来替换第一个查询,并从该cte中选择sum来替换第二个查询:

;WITH EmployeesCount AS
(
    select p1.DepartmentName, count(distinct u.Id) Nr_Employees
    from Paychecks p1
    join Users u on u.Id = p1.UserId
    where u.customerid=214 and u.isdeleted=0
    group by p1.DepartmentName 
) 

然后,在您的主查询中:

...
LEFT JOIN
EmployeesCount As totals on totals.DepartmentName = p.DepartmentName
LEFT JOIN
(
    SELECT SUM(Nr_Employees) 
    FROM EmployeesCount
) As SummerCount on SummerCount.DepartmentName = p.DepartmentName

更新

;WITH EmployeesCount AS
(
    select p1.DepartmentName, count(distinct u.Id) Nr_Employees
    from Paychecks p1
    join Users u on u.Id = p1.UserId
    where u.customerid=214 and u.isdeleted=0
    group by p1.DepartmentName 
)

SELECT  totals.Nr_Employees as TotalEmployees,
        SummerCount.Nr_Employees as sumEmployees
FROM 
dbo.Paychecks AS p INNER JOIN dbo.Users AS u ON u.Id=p.UserId 
INNER JOIN dbo.EmploymentStatuses AS E ON E.Id = u.EmploymentStatusId
LEFT OUTER JOIN dbo.EmployeeTerminationQueue AS etq on etq.UserId = u.Id
LEFT OUTER JOIN dbo.TerminationReasons AS t on t.Id = etq.TerminationReasonid    
LEFT JOIN EmployeesCount As totals on totals.DepartmentName = p.DepartmentName
CROSS JOIN
(
    SELECT SUM(Nr_Employees) As Nr_Employees
    FROM EmployeesCount
) As SummerCount 

完整查询

;WITH EmployeesCount AS
(
    select p1.DepartmentName, count(distinct u.Id) Nr_Employees
    from Paychecks p1
    join Users u on u.Id = p1.UserId
    where u.customerid=214 and u.isdeleted=0
    group by p1.DepartmentName 
)


SELECT DISTINCT u.Id, u.FirstName + ' ' + u.LastName AS Medarbajder,
u.SSN AS CPRNR,
(CASE 
WHEN u.SSN IS NOT NULL THEN
DATEDIFF(YEAR,CONVERT(date,STUFF(LEFT(u.SSN, 4), 3, 0, '.') + '.' + CAST(DATEPART(YEAR, GETDATE()) / 100 - 1 as char(2)) + SUBSTRING(u.SSN, 5, 2), 104) , GETDATE()) 
ELSE
NULL
END) As Aldre ,

 (CASE WHEN right(rtrim(SSN), 1) IN ('1', '3', '5', '7', '9') THEN 'M'
             WHEN right(rtrim(SSN), 1) IN ('2', '4', '6', '8', '0') THEN 'K'
        END) as Køn,

convert(varchar(10), p.WorkStartDate, 105) AS StartDato ,

(CASE
WHEN u.ResignationDate IS NOT NULL THEN

CONVERT(varchar(4), DATEDIFF(dd, WorkStartDate, u.ResignationDate)/365) + ' år '+

CONVERT(varchar(4), DATEDIFF(MONTH, WorkStartDate, u.ResignationDate) % 12) + ' måneder '+

CAST(DATEDIFF(dd, DATEADD(mm, DATEDIFF(mm, DATEADD(yy, DATEDIFF(yy, WorkStartDate, u.ResignationDate), WorkStartDate), u.ResignationDate), 
DATEADD(yy, DATEDIFF(yy, WorkStartDate, u.ResignationDate), WorkStartDate)), u.ResignationDate) AS varchar(2)) +' dag ' 

ELSE
CONVERT(varchar(4), DATEDIFF(dd, WorkStartDate, GETDATE())/365) + ' år '+

CONVERT(varchar(4), DATEDIFF(MONTH, WorkStartDate, GETDATE()) % 12) + ' måneder '+

CAST(DATEDIFF(dd, DATEADD(mm, DATEDIFF(mm, DATEADD(yy, DATEDIFF(yy, WorkStartDate, GETDATE()), WorkStartDate), GETDATE()), 
DATEADD(yy, DATEDIFF(yy, WorkStartDate, GETDATE()), WorkStartDate)), GETDATE()) AS varchar(2)) +' dag ' end) as Ancinitet,

(CASE
WHEN u.ResignationDate IS NOT NULL THEN
DATEDIFF(day, WorkStartDate, u.ResignationDate) 
ELSE
DATEDIFF(day, WorkStartDate, GETDATE())
end) as AncinitetAntalDag,

p.DepartmentName AS Afdelinger,

E.Name AS Ansættelsesstatus,
u.ResignationDate AS Opsigelsesdato,
(CASE WHEN etq.Id IS NOT NULL THEN t.Name ELSE 'Aktiv' END) AS Terminationreason,

totals.Nr_Employees as TotalEmployees,
SummerCount.Nr_Employees as sumEmployees

FROM 
 dbo.Paychecks AS p INNER JOIN dbo.Users AS u ON u.Id=p.UserId 
 INNER JOIN dbo.EmploymentStatuses AS E ON E.Id = u.EmploymentStatusId
LEFT OUTER JOIN dbo.EmployeeTerminationQueue AS etq on etq.UserId = u.Id
LEFT OUTER JOIN dbo.TerminationReasons AS t on t.Id = etq.TerminationReasonid
LEFT JOIN EmployeesCount As totals on totals.DepartmentName = p.DepartmentName
CROSS JOIN
(
    SELECT SUM(Nr_Employees) As Nr_Employees
    FROM EmployeesCount
) As SummerCount 





 WHERE u.CustomerId=214 AND u.IsDeleted = 0 
AND etq.StartDate >= @EndDateFrom AND etq.StartDate <= @EndDateTo
AND 
((@Gender != 'B' AND
    @Gender =  (CASE WHEN right(rtrim(SSN), 1) IN ('1', '3', '5', '7', '9') THEN 'M'
            WHEN right(rtrim(SSN), 1) IN ('2', '4', '6', '8', '0') THEN 'K'
            END))
OR
(@Gender = 'B'))
AND
((@TerminationReason != 0 AND @TerminationReason = (CASE WHEN etq.Id IS NOT NULL THEN t.Id ELSE 0 END))
OR
(@TerminationReason = 0))
AND u.EmploymentStatusId = 2
order by p.DepartmentName

答案 1 :(得分:0)

不支持将聚合函数作为聚合函数的运算符。这就是为什么你的声明不起作用。

您可以使用WITH ROLLUP子句或将您的语句放入子选择中,并在外部语句中构建总和。