使用基于ROWCOUNT的SUM

时间:2012-04-19 17:37:10

标签: sql sql-server sum

基本上我要做的是,有些用户在日期范围内(每月)有适用的服务

我希望能够显示用户的可用服务时间(以小时为单位),以及他们根据包含约会数据的表安排了多少小时

所以我希望它基本上看起来像

user             service             available         scheduled
-----------------------------------------------------------------
User1            Service1            120               7
User1            Service2            40                0

计划列是通过获取SUM表格中StartEnd字段的Appointment来计算的。

现在我的问题是,如果没有安排约会,我不确定如何仍然显示我想要的信息。

表格之间的关系如下:

  • 每位用户1个NOA
  • 每个NOA的多项服务
  • 每个服务1个FormType

  • 约会将FormTypeID保存为FK

  • ClientAppointment包含AppointmentID和UserID(具有指定NOA的同一用户)

SELECT

 --NOA
 noa.noaID, noa.userID as 'ClientUserID', 

 --Service
 service.BillingCode + service.MOD1 + service.MOD2 + service.MOD3 + service.MOD4 AS     BillingCode, service.FormTypeID, service.StartDate, service.EndDate, service.CurrentUnits, service.Adjustment,

 --Scheduler
 scheduler.* 
 FROM LEL_NOA noa
 LEFT JOIN LEL_Service service ON service.noaID = noa.noaID
 LEFT OUTER JOIN 
 (
 SELECT ca.ClientID, SUM(DATEDIFF(second, a.Start, a.[End]) / 3600) as ScheduledUnits        FROM LEL_Scheduler_Appointment a
 LEFT JOIN LEL_Scheduler_ClientAppointment ca ON a.ApptID = ca.ApptID
 WHERE a.Start BETWEEN '1/1/2012' and '1/31/2012'
   AND a.[End] BETWEEN '1/1/2012' and '1/31/2012'
 GROUP BY ca.ClientID

 ) AS scheduler ON noa.UserID = scheduler.ClientID

WHERE noa.UserID = 4
AND service.StartDate BETWEEN '1/1/2012' and '1/31/2012'
AND service.EndDate BETWEEN '1/1/2012' and '1/31/2012'

ORDER BY 'ClientUserID', BillingCode, FormTypeID, StartDate

以上程序将撤回本月,21个可用,7个预定,但UserID = 4的人在该时间段内有另一个服务,我想显示0小时的信息作为我桌上的下一个项目。


(noaID, ClientUserID, BillingCode, FormTypeID, StartDate, EndDate, CurrentUnits,     Adjustment, ScheduledUnits) VALUES
(203, 25, 'x888', 6, '4/16/2012 4:24:19 PM', '4/16/2012 4:24:19 PM', 0, 0, 5.000000), 
(203, 25, 'x999', 1, '4/1/2012 12:00:00 AM', '4/30/2012 12:00:00 AM', 10, 0, 5.000000)

问题是第一个条目,没有安排任何约会,但它仍然显示5(第二个总和是正确的)

2 个答案:

答案 0 :(得分:1)

在ORDER BY子句上方,添加一个UNION ALL行,然后添加一个新的SELECT查询,该查询专门为没有计划的同一用户提取信息。只需确保新查询与顶部查询具有相同的列,即使您必须对NULL值进行硬编码。这是一般语法:

SELECT [stuff] FROM Table1 WHERE [A]
UNION ALL
SELECT [samestuff] FROM Table1 WHERE [B]
ORDER BY [somestuff]

答案 1 :(得分:1)

尝试重新处理查询以使用内联子查询。性能可能会受到影响,但获得正确的查询会更容易。

SELECT
--NOA
noa.noaID, noa.userID as ClientUserID, 
--Service
service.BillingCode + service.MOD1 + service.MOD2 + service.MOD3 + service.MOD4 AS BillingCode,
service.FormTypeID, service.StartDate, service.EndDate, service.CurrentUnits, service.Adjustment,
--Scheduler
(
    SELECT COALESCE(SUM(DATEDIFF(second, a.Start, a.[End]) / 3600), 0) as ScheduledUnits
    FROM LEL_Scheduler_Appointment a
        INNER JOIN LEL_Scheduler_ClientAppointment ca ON a.ApptID = ca.ApptID
    WHERE a.Start BETWEEN '1/1/2012' and '1/31/2012'
        AND a.[End] BETWEEN '1/1/2012' and '1/31/2012'
        AND ca.ClientID = noa.UserID
        AND a.ServiceID = FormTypeID
) as ScheduledUnits
FROM LEL_NOA noa
    LEFT JOIN LEL_Service service ON service.noaID = noa.noaID
WHERE noa.UserID = 4
    AND service.StartDate BETWEEN '1/1/2012' and '1/31/2012'
    AND service.EndDate BETWEEN '1/1/2012' and '1/31/2012'
ORDER BY ClientUserID, BillingCode, FormTypeID, StartDate