两个表列的SUM基于另一个列值和SUBTRACT和连接表

时间:2015-10-14 10:30:14

标签: sql-server

我有下表

表名称EmployeeInformation

EmployeeID EmployeeFirstName EmployeeLastName
    1             |John      |Baker
    2             |Carl      |Lennon
    3             |Marion    |Herbert

表名称PeriodInformation

PeriodID PeriodStart PeriodEnd
    1   |1/1/14      |12/30/14
    2   |1/1/15      |12/30/15

表名称LeaveType

LeaveTypeID LeaveTypeName
     1      |Annual Leave
     2      |Sick Leave

表名LeaveEntitlment

SNO LeaveTypeID PeriodID EmployeeID LeaveEntitlementDaysNumber
 1   |     1   |    1    |     1   |          10
 2   |     1   |    1    |     1   |          15
 3   |     2   |    1    |     1   |          15
 4   |     2   |    2    |     1   |           5
 5   |     1   |    2    |     2   |          10    
 6   |     1   |    2    |     2   |          10
 7   |     1   |    2    |     2   |          15
 8   |     2   |    2    |     2   |          15
 9   |     2   |    2    |     2   |           5
 10  |     1   |    1    |     2   |          10  

表名AssigneLeave

SNO LeaveTypeID PeriodID EmployeeID AssignedLeaveDaysNumber
 1   |     1   |    1    |     1    |          10
 2   |     1   |    1    |     1    |          10
 3   |     2   |    1    |     1    |           5
 4   |     2   |    2    |     1    |           5
 5   |     1   |    2    |     2    |           2    
 6   |     1   |    2    |     2    |           3
 7   |     1   |    2    |     2    |           4
 8   |     2   |    2    |     2    |           8
 9   |     2   |    2    |     2    |           5
 10  |     1   |    1    |     2    |           9

我想要一个查询根据EmployeeIDPeriodID和{}加入所有这些表 LeaveTypeID

基于LeaveEntitlement.LeaveEntitlementDaysNumber AS LeaveTypeID和AS EntitleAnnaul的{​​{1}}总和 并根据EntitleSick AS AssignedLeave.AssignedLeaveDaysNumber和AS LeaveTypeID求和AssignedAnnaul 并根据AssignedSick AS EntitleAnnaulAssignedAnnual中减去LeaveTypeID 并根据AnnualBalance AS EntitleSick

AssignedSick中减去LeaveTypeID

查询应显示以下列:

SickBalance

1 个答案:

答案 0 :(得分:0)

尝试以下查询(在子查询中使用group by聚合然后在外部查询中进行计算),假设所有表中都存在数据,则使用普通连接,否则则必须修改查询以使用左/右外连接

select
    SubQry.EmployeeID, 
    SubQry.EmployeeFirstName, 
    SubQry.EmployeeLastName, 
    SubQry.PeriodID, 
    SubQry.PeriodStart, 
    SubQry.PeriodEnd, 
    SubQry.EntitleAnnual, 
    SubQry.AssignedAnnual, 
    (SubQry.EntitleAnnual-SubQry.AssignedAnnual) as AnnualBalance, 
    SubQry.EntitleSick, 
    SubQry.AssignedSick, 
    (SubQry.EntitleSick-SubQry.AssignedSick) as SickBalance
From 
(
Select
    ei.EmployeeID,
    ei.EmployeeFirstName ,
    ei.EmployeeLastName,
    pi.PeriodID, 
    pi.PeriodStart, 
    pi.PeriodEnd, 
    Sum( Case when lt.LeaveTypeID = 1 Then le.LeaveEntitlementDaysNumber Else 0 End ) as EntitleAnnual, 
    Sum( Case when lt.LeaveTypeID = 1 Then al.AssignedLeaveDaysNumber Else 0 End ) as AssignedAnnual, 
    Sum( Case when lt.LeaveTypeID = 2 Then le.LeaveEntitlementDaysNumber Else 0 End ) as EntitleSick, 
    Sum( Case when lt.LeaveTypeID = 2 Then al.AssignedLeaveDaysNumber Else 0 End ) as AssignedSick
From ( select EmployeeID,LeaveTypeID,PeriodID,sum(LeaveEntitlementDaysNumber) as LeaveEntitlementDaysNumber from LeaveEntitlment group by EmployeeID,LeaveTypeID,PeriodID ) le 
join ( select EmployeeID,LeaveTypeID,PeriodID,sum(AssignedLeaveDaysNumber) as AssignedLeaveDaysNumber from AssigneLeave group by EmployeeID,LeaveTypeID,PeriodID )  al on le.EmployeeID = al.EmployeeID and le.LeaveTypeID = al.LeaveTypeID and le.PeriodID = al.PeriodID
join EmployeeInformation ei on ei.EmployeeID = le.EmployeeID
join PeriodInformation pi on pi.PeriodID = le.PeriodID 
join LeaveType lt on lt.LeaveTypeID = le.LeaveTypeID
group by ei.EmployeeID,ei.EmployeeFirstName,ei.EmployeeLastName,pi.PeriodID,pi.PeriodStart,pi.PeriodEnd
) SubQry