SQL查询:在同一个表上进行多次选择和求和

时间:2018-04-27 16:38:38

标签: sql sql-server tsql

我有一个具有以下结构的表格,并且我试图选择由经理分组的所有员工,其中包括员工小时类型和一个月的总小时数之和。

[ID] - Int    
[ActualHours] - nvarchar(MAX)  
[TimeType] - nvarchar(MAX)  
[Cost] - nvarchar(MAX)  
[EmployeeName] - nvarchar(MAX)  
[SupervisorName] - nvarchar(MAX)  
[SupervisorNumber] - nvarchar(MAX)  
[Period] - nvarchar(MAX)  
[TaskName] -nvarchar(MAX)

表数据样本:

+------+-------------+-------------+------------+------------------+------------+------------------+--------------+--------------+---------------+
|  ID  | ActualHours |  TimeType   |    Cost    |   EmployeeName   | EmployeeID |  SupervisorName  | SupervisorID |    Period    |   TaskName    |
+------+-------------+-------------+------------+------------------+------------+------------------+--------------+--------------+---------------+
| 8    |    7.5      |    Regular  |    18      |  Jones, Fred     |  42        |  Marks ,Thomas   |       54     |    Dec-17    |  Delivery     |
| 8    |    22.5     |    Regular  |    25      |  Smith, Bill     |  54        |  Doe ,John       |       12     |    Dec-17    |  Accounts     |
| 8    |    12.5     |    OT       |    22      |  Hinckly, Debra  |  66        |  Marks ,Thomas   |       54     |    Dec-17    |  Shipping     |
| 8    |    7.5      |    Regular  |    18      |  Jones, Fred     |  42        |  Marks ,Thomas   |       54     |    Dec-17    |  Returns      |
| 8    |    7.5      |    OT       |    27      |  Jones, Fred     |  42        |  Marks ,Thomas   |       54     |    Dec-17    |  Recievables  |
| 8    |    2.5      |    OT       |    27      |  Jones, Fred     |  42        |  Marks ,Thomas   |       54     |    Dec-17    |  Transfers    |
+------+-------------+-------------+------------+------------------+------------+------------------+--------------+--------------+---------------+

我想要实现的是让查询返回以下结果:

+----------------+----------------+-----------------+------------+------------+----------+--------+
| SupervisorName |  EmployeeName  | TimeTypeRegular | TimeTypeOT | TotalHours | TaskCost | Period |
+----------------+----------------+-----------------+------------+------------+----------+--------+
| Marks ,Thomas  | Jones, Fred    |              15 |        9.5 |       24.5 |      270 | Dec-17 |
| Doe ,John      | Smith, Bill    |            22.5 |          0 |       22.5 |    562.5 | Dec-17 |
| Marks ,Thomas  | Hinckly, Debra |               0 |       12.5 |       12.5 |      275 | Dec-17 |
+----------------+----------------+-----------------+------------+------------+----------+--------+

我尝试过以下操作,但我无法同时获得常规小时数和OT小时数以及总和数。

select SupervisorName, EmployeeName,  TimeType, SUM(convert(decimal(10,2),ActualHours)) As TotalHours, Period
from TableName
where TimeType = 'Regular' and Period = 'Dec-17'
group by EmployeeName, EmployeeID, SupervisorName

我正在使用MS SQL Server 2017.感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

您可以在聚合函数SUM中使用case语句来筛选要求和的行:

select SupervisorName, EmployeeName,  TimeType, 
    SUM( CASE TimeType WHEN 'Regular' THEN convert(decimal(10,2),ActualHours) ELSE NULL END ) AS TimeTypeRegular,
    SUM( CASE TimeType WHEN 'OT' THEN convert(decimal(10,2),ActualHours) ELSE NULL END ) AS TimeTypeOT,
    SUM(convert(decimal(10,2),ActualHours)) As TotalHours, Period
from TableName
where TimeType = 'Regular' and Period = 'Dec-17'
group by EmployeeName, EmployeeID, SupervisorName

答案 1 :(得分:0)

试试这个:

SELECT 
    SupervisorName, 
    EmployeeName,  
    SUM(CASE WHEN TimeType = 'Regular' THEN convert(decimal(10,2),ActualHours) ELSE 0 END) AS TimeTypeRegular, 
    SUM(CASE WHEN TimeType = 'OT' THEN convert(decimal(10,2),ActualHours) ELSE 0 END) AS TimeTypeRegular,
    SUM(convert(decimal(10,2),ActualHours)) totalHours,
    Period
FROM 
    TableName
GROUP BY 
    EmployeeName, EmployeeID, SupervisorName, Period

从where子句中删除了句点条件,并在group by子句中添加了相同的条件 - >为您提供所有时间段的所需结果。如果您只希望特定月份的结果,请在where子句

中添加句点过滤器