在加入 2 个表时需要帮助

Need assistance in Joining 2 tables

我需要加入 2 tables 并分配 0(HoursBilled 列)并且所有月份都出现在每个唯一 AuthId 的 BilledHours table 中。

First table (AuthHours) 具有每个 ClientId 的数据及其在每个唯一 AuthId 的不同日期范围内的允许小时数(每月)。

create table AuthHours
(AuthId INT, ClientId INT, AuthStartDate DATE, AuthEndDate DATE, AllowedHoursPerMonth Float);

INSERT INTO AuthHours
VALUES
(123, 55, '2021-12-19', '2022-03-17', 43.0),
(109, 55, '2021-12-19', '2022-03-17', 9.0),
(218, 55, '2021-12-19', '2022-03-17', 6.0),
(619, 55, '2021-12-19', '2022-03-17', 43.0),
(777, 55, '2021-12-19', '2022-03-17', 43.0),
(345, 55, '2022-03-18', '2022-07-28', 40.0),
(346, 55, '2022-03-18', '2022-07-28', 12.0),
(395, 55, '2022-03-18', '2022-07-28', 10.0),
(487, 55, '2022-03-18', '2022-07-28', 45.0),
(198, 55, '2022-03-18', '2022-07-28', 37.0)

SELECT * FROM AuthHours

第二个 table (BilledHours)(已按 ClientId、AuthId、月份和年份分组)具有每个的数据每个 AuthId 和每月的 ClientId 及其已经 计费小时数。

create table BilledHours
(ClientId INT, Month VARCHAR(10), Year INT, AuthId INT, HoursBilled Float);

INSERT INTO BilledHours
VALUES
(55, 'January', 2022, 123, 26.33),
(55, 'January', 2022, 109, 4.25),
(55, 'January', 2022, 777, 2.5),
(55, 'February', 2022, 123, 32.5),
(55, 'February', 2022, 109, 4.25),
(55, 'February', 2022, 777, 1.5)

SELECT * FROM BilledHours

我需要为每个不在 BilledHours table 中的 AuthId 分配 0 HoursBilled,但如果今天日期不在 AuthStartDate 和 AuthEndDate 日期范围之间,则将其保留为 NULL。此外,需要为每个不在 BilledHours table.

中的 AuthId 添加 BilledHours table 中出现的月份和年份

我的加入,但它是错误的(很明显)。

SELECT  AuthHours.AuthId,
        AuthHours.ClientId,
        AuthHours.AuthStartDate,
        AuthHours.AuthEndDate,
        BilledHours.Month,
        BilledHours.Year,
        AuthHours.AllowedHoursPerMonth,
        BilledHours.HoursBilled
        
FROM AuthHours
LEFT JOIN BilledHours
  ON (AuthHours.AuthId = BilledHours.AuthId) AND (AuthHours.ClientId = BilledHours.ClientId)

输出不正确:

AuthId ClientId AuthStartDate AuthEndDate Month Year AllowedHoursPerMonth HoursBilled
123 55 2021-12-19 2022-03-17 January 2022 43 26.33
123 55 2021-12-19 2022-03-17 February 2022 43 32.5
109 55 2021-12-19 2022-03-17 January 2022 9 4.25
109 55 2021-12-19 2022-03-17 February 2022 9 4.25
218 55 2021-12-19 2022-03-17 NULL NULL 6 NULL
619 55 2021-12-19 2022-03-17 NULL NULL 43 NULL
777 55 2021-12-19 2022-03-17 January 2022 43 2.5
777 55 2021-12-19 2022-03-17 February 2022 43 1.5
345 55 2022-03-18 2022-07-28 NULL NULL 40 NULL
346 55 2022-03-18 2022-07-28 NULL NULL 12 NULL
395 55 2022-03-18 2022-07-28 NULL NULL 10 NULL
487 55 2022-03-18 2022-07-28 NULL NULL 45 NULL
198 55 2022-03-18 2022-07-28 NULL NULL 37 NULL

我需要的输出:

AuthId ClientId AuthStartDate AuthEndDate Month Year AllowedHoursPerMonth HoursBilled
123 55 2021-12-19 2022-03-17 January 2022 43 26.33
123 55 2021-12-19 2022-03-17 February 2022 43 32.5
109 55 2021-12-19 2022-03-17 January 2022 9 4.25
109 55 2021-12-19 2022-03-17 February 2022 9 4.25
218 55 2021-12-19 2022-03-17 January 2022 6 0
218 55 2021-12-19 2022-03-17 February 2022 6 0
619 55 2021-12-19 2022-03-17 January 2022 43 0
619 55 2021-12-19 2022-03-17 February 2022 43 0
777 55 2021-12-19 2022-03-17 January 2022 43 2.5
777 55 2021-12-19 2022-03-17 February 2022 43 1.5
345 55 2022-03-18 2022-07-28 NULL NULL 40 NULL
346 55 2022-03-18 2022-07-28 NULL NULL 12 NULL
395 55 2022-03-18 2022-07-28 NULL NULL 10 NULL
487 55 2022-03-18 2022-07-28 NULL NULL 45 NULL
198 55 2022-03-18 2022-07-28 NULL NULL 37 NULL

我更改了查询以反映您在 month/year 请求中寻找的更多内容

select
    distinct
    a.authid,
    a.clientid,
    a.authstartdate,
    a.authenddate,
    case when a.hoursbilled is null then null else a.month end as month,
    case when a.hoursbilled is null then null else a.year end as year,
    a.allowedhourspermonth,
    a.hoursbilled
from 
    (
        select
        a.authid,
        a.clientid,
        a.authstartdate,
        a.authenddate,
        a.allowedhourspermonth,
        a.month,
        a.year,
        case 
            when a.hoursbilled is null and getdate() between cast(a.authstartdate as date) and cast(a.authenddate as date) then 0
            when a.hoursbilled is null and getdate() not between cast(a.authstartdate as date) and cast(a.authenddate as date) then null
            else a.hoursbilled 
        end as hoursbilled
    from 
        (
        SELECT 
            a.authid,
            a.clientid,
            a.authstartdate,
            a.authenddate,
            a.allowedhourspermonth,
            b.month,
            b.year,
            b.hoursbilled
        FROM 
            (
            select
                d.clientid,
                d.month,
                d.year,
                d.authid,
                b.hoursbilled
            from 
                (
                    select
                        distinct
                        a.clientid,
                        a.authid,
                        month,
                        year
                    from BilledHours b 
                        join AuthHours a on 1=1
                ) d 
                left join BilledHours b on b.authid = d.authid and d.clientid = b.clientid and d.month = b.month and b.year = d.year
            ) b 
                left join AuthHours a on a.clientid = b.clientid and a.authid = b.authid
        ) a
    ) a

这是一个没有 CAL_DM table 的选项,但如果您的公司还没有 CAL_DM table,我建议投资。