2

基本上我正在尝试做的是,有些用户在某个日期范围内(每月)拥有适用的服务

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

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

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

Scheduled 列是通过我的表SUM中的StartEnd字段计算的。Appointment

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

表之间的关系如下:

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

  • Appointment 将 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 计划,但有 a 的人在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(第二个总和是正确的)

4

2 回答 2

1

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

SELECT [stuff] FROM Table1 WHERE [A]
UNION ALL
SELECT [samestuff] FROM Table1 WHERE [B]
ORDER BY [somestuff]
于 2012-04-19T18:17:14.077 回答
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
于 2012-04-19T19:08:44.347 回答