在连接 2 个表时需要帮助

发布于 2025-01-10 04:32:28 字数 6717 浏览 0 评论 0原文

我需要连接 2 个表并分配 0(HoursBilled 列),并且每个唯一 AuthId 的所有月份都出现在 BilledHours 表中。

第一个表 (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

第二个表 (BilledHours)(已按 ClientId、AuthId、月份和年份分组)包含每个 ClientId 的数据以及每个 AuthId 的计费小时数和月份。

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 表中的每个 AuthId 分配 0 HoursBilled,但如果 TODAY Date 不在 AuthStartDate 和 AuthEndDate 日期范围之间,请将其保留为 NULL。另外,需要为 BilledHours 表中没有的每个 AuthId 添加出现在 BilledHours 表中的月份和年份。

我的加入,但这是错误的(显然)。

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)

不正确的输出:

AuthIdClientIdAuthStartDateAuthEndDateAllowedHoursPerMonthHoursBilled
123552021-12-192022-03-17一月20224326.33
123552021-12-192022-03-17二月20224332.5
109552021-12-192022-03-17年1月202294.25
109552021-12-192022-03-172月2022年94.25
218552021-12-192022-03-17NULLNULL6NULL
619552021-12-192022-03-17NULLNULL< /strong>43NULL
777552021-12-192022-03-171 月2022 年432.5
777552021-12-192022-03-172月2022年431.5
345552022-03-182022-07-2840
346552022-03-182022-07-2812
395552022-03-182022-07-28NULLNULL10NULL
487552022-03-182022-07-28NULLNULL45NULL
198552022-03-182022-07-28NULLNULL37NULL

输出 I需要:

AuthIdClientIdAuthStartDateAuthEndDate允许小时数每月 小时数
123552021-12-192022-03-171 月2022 年4326.33
123552021-12-192022-03-172 月2022 年4332.5
109552021-12-192022-03-171月2022年94.25
109552021-12-192022-03-172月2022年94.25
218552021-12-192022-03-171月2022年60
218552021-12-192022-03-172月2022年< /strong>60
619552021-12-192022-03-171月2022年430
619552021-12-192022-03-172月2022年< /strong>430
777552021-12-192022-03-17一月2022432.5
777552021-12-192022-03-172 月2022 年431.5
345552022-03-182022-07-28NULLNULL40
346552022-03-182022-07-28NULLNULL12NULL
395552022-03-182022-07-28NULLNULL10NULL
487552022-03-182022-07-28NULLNULL45NULL
198552022-03-182022-07-28NULLNULL37NULL

I need to join 2 tables and assign 0 (HoursBilled column) and all Months appeared in the BilledHours table for each unique AuthId.

First table (AuthHours) has data of each ClientId and their allowed hours (per month) for different date ranges per unique 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

Second table (BilledHours) (already grouped by ClientId, AuthId, Month and Year) has data of each ClientId and their already billed hours per AuthId and Month.

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

I need to assign 0 HoursBilled for each AuthId that not in the BilledHours table, but if TODAY Date not between AuthStartDate and AuthEndDate date ranges, keep it NULL. Also, Month and Year appeared in the BilledHours table need to be added for each AuthId that not in the BilledHours table.

My Join, but it's wrong (obviously).

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)

Incorrect output:

AuthIdClientIdAuthStartDateAuthEndDateMonthYearAllowedHoursPerMonthHoursBilled
123552021-12-192022-03-17January20224326.33
123552021-12-192022-03-17February20224332.5
109552021-12-192022-03-17January202294.25
109552021-12-192022-03-17February202294.25
218552021-12-192022-03-17NULLNULL6NULL
619552021-12-192022-03-17NULLNULL43NULL
777552021-12-192022-03-17January2022432.5
777552021-12-192022-03-17February2022431.5
345552022-03-182022-07-28NULLNULL40NULL
346552022-03-182022-07-28NULLNULL12NULL
395552022-03-182022-07-28NULLNULL10NULL
487552022-03-182022-07-28NULLNULL45NULL
198552022-03-182022-07-28NULLNULL37NULL

Output I need:

AuthIdClientIdAuthStartDateAuthEndDateMonthYearAllowedHoursPerMonthHoursBilled
123552021-12-192022-03-17January20224326.33
123552021-12-192022-03-17February20224332.5
109552021-12-192022-03-17January202294.25
109552021-12-192022-03-17February202294.25
218552021-12-192022-03-17January202260
218552021-12-192022-03-17February202260
619552021-12-192022-03-17January2022430
619552021-12-192022-03-17February2022430
777552021-12-192022-03-17January2022432.5
777552021-12-192022-03-17February2022431.5
345552022-03-182022-07-28NULLNULL40NULL
346552022-03-182022-07-28NULLNULL12NULL
395552022-03-182022-07-28NULLNULL10NULL
487552022-03-182022-07-28NULLNULL45NULL
198552022-03-182022-07-28NULLNULL37NULL

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随心而道 2025-01-17 04:32:28

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

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 表的选项,但如果您的公司还没有 CAL_DM 表,我建议您投资 CAL_DM 表。

I changed the query to reflect more what you are looking for with the month/year request

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

This is an option without a CAL_DM table, but I would recommend investing in a CAL_DM table if your company does not have one already.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文