在连接 2 个表时需要帮助
我需要连接 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)
不正确的输出:
AuthId | ClientId | AuthStartDate | AuthEndDate | 月 | 年 | AllowedHoursPerMonth | HoursBilled |
---|---|---|---|---|---|---|---|
123 | 55 | 2021-12-19 | 2022-03-17 | 一月 | 2022 | 43 | 26.33 |
123 | 55 | 2021-12-19 | 2022-03-17 | 二月 | 2022 | 43 | 32.5 |
109 | 55 | 2021-12-19 | 2022-03-17 | 年1月 | 2022 | 9 | 4.25 |
109 | 55 | 2021-12-19 | 2022-03-17 | 2月 | 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< /strong> | 43 | NULL |
777 | 55 | 2021-12-19 | 2022-03-17 | 1 月 | 2022 年 | 43 | 2.5 |
777 | 55 | 2021-12-19 | 2022-03-17 | 2月 | 2022年 | 43 | 1.5 |
345 | 55 | 2022-03-18 | 2022-07-28 | 空 | 空 | 40 | 空 |
346 | 55 | 2022-03-18 | 2022-07-28 | 空 | 空 | 12 | 空 |
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 |
输出 I需要:
AuthId | ClientId | AuthStartDate | AuthEndDate | 月 | 年 | 允许 | 小时数每月 小时数 |
---|---|---|---|---|---|---|---|
123 | 55 | 2021-12-19 | 2022-03-17 | 1 月 | 2022 年 | 43 | 26.33 |
123 | 55 | 2021-12-19 | 2022-03-17 | 2 月 | 2022 年 | 43 | 32.5 |
109 | 55 | 2021-12-19 | 2022-03-17 | 1月 | 2022年 | 9 | 4.25 |
109 | 55 | 2021-12-19 | 2022-03-17 | 2月 | 2022年 | 9 | 4.25 |
218 | 55 | 2021-12-19 | 2022-03-17 | 1月 | 2022年 | 6 | 0 |
218 | 55 | 2021-12-19 | 2022-03-17 | 2月 | 2022年< /strong> | 6 | 0 |
619 | 55 | 2021-12-19 | 2022-03-17 | 1月 | 2022年 | 43 | 0 |
619 | 55 | 2021-12-19 | 2022-03-17 | 2月 | 2022年< /strong> | 43 | 0 |
777 | 55 | 2021-12-19 | 2022-03-17 | 一月 | 2022 | 43 | 2.5 |
777 | 55 | 2021-12-19 | 2022-03-17 | 2 月 | 2022 年 | 43 | 1.5 |
345 | 55 | 2022-03-18 | 2022-07-28 | NULL | NULL | 40 | 空 |
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 |
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:
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 |
Output I need:
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 |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我更改了查询以反映您在月/年请求中寻找的更多内容
。这是一个没有 CAL_DM 表的选项,但如果您的公司还没有 CAL_DM 表,我建议您投资 CAL_DM 表。
I changed the query to reflect more what you are looking for with the month/year request
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.