从 Teradata 中的两个不同表获取最多两个日期 - 场景?

发布于 2024-08-15 05:17:01 字数 907 浏览 2 评论 0原文

我有两个表table1和table2。表 2 的行数少于表 1。在这两个表中,table1 中有两个日期列 caldate1 ,table2 中有两个日期列 caldate2 。所以现在我需要连接这两个表并获取两个日期列的最大值并将其保留在新表中。但是,如果我们对这两个表进行内连接,则表 1 中不存在于表 2 中的行将不会进入最终表。所以我们需要类似的东西,

table1 
left outer join
table2

但是有一种情况是两个日期有空值。那么我可以在以下场景中使用合并来获取正确的数据吗?

1. table1 中的行在 table2 中不存在 ->那么table1中的caldate1应该进入最终表。

2. table1中的行在table2中存在,并且table1的caldate1和table2的caldate2为空->那么 null 应该进入最终表的日期列

3. table1 中的行在 table2 中存在,并且 caldate1 不为 null,caldate2 为 null ->那么 caldate1 应该进入决赛桌。

4. table1 中的行在 table2 中存在,并且 caldate1 为 null,caldate2 不为 null ->那么 caldate2 应该进入决赛桌

5. table1 中的行存在于 table2 中并且 caldate1 大于 caldate2 -> caldate1 应该进入决赛桌

6. table1 中的行存在于 table2 中并且 caldate2 大于 caldate1 -> caldate2 应该进入决赛桌

我们不需要考虑 table2 中与 table1 不匹配的行。所以基本上,如果两个表中都存在特定行,我需要具有最新计算日期的所有 table1 行。提前致谢。我无法获得正确的功能来执行此操作。它是否合并?

I have two tables table1 and table2. Table2 is having less number of rows than table1. In these two tables there are two date columns caldate1 in table1 and caldate2 in table2. So now I need to join on these two tables and get the maximum of the two date columns and keep that in new table. But if we inner join on these two tables the table1 rows which are not there in table2 will not go into the final table. So we need some thing like

table1 
left outer join
table2

But there is a situation that the two dates are having nulls. So Can I use coalesce to get the correct data in the below scenarios..

1. row in table1 is not there in table2 -> then the caldate1 in table1 should go into final table.

2. row in table1 is there in table2 and the caldate1 of table1 and caldate2 of table2 are nulls -> then null should come into final table's date column

3. row in table1 is there in table2 and caldate1 is not null and caldate2 is null -> then caldate1 should come into final table.

4. row in table1 is there in table2 and caldate1 is null and caldate2 is not null -> then caldate2 should come into final table

5. row in table1 is there in table2 and caldate1 is greater than caldate2 -> caldate1 should come into final table

6. row in table1 is there in table2 and caldate2 is greater than caldate1 -> caldate2 should come into final table

We dont need to consider the rows in table2 which are not matching with table1. So basically i need all table1 rows with latest of the caldate if a particular row is there in both tables. Thanks in advance. I am not able to get correct function to do it.Is it coalesce?

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

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

发布评论

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

评论(2

岁月静好 2024-08-22 05:17:01

从上面的查询中,如果某些数字出现在table2中而不出现在table1中,那么这些记录将被删除,您可以在上面的查询中使用完全外连接。

或者请参阅下面的查询也将涵盖该场景。

sel number,max(date1) from (
  sel number,max(caldate1) as date1
    from table1
  union
  sel number,max(caldate2) as date1
    from table2
)tmp ;

From the above query, if some number present in table2 and not in table1, those records will be dropped, You can use full outer join in the above query.

OR See the below query will cover that scenario also.

sel number,max(date1) from (
  sel number,max(caldate1) as date1
    from table1
  union
  sel number,max(caldate2) as date1
    from table2
)tmp ;
つ可否回来 2024-08-22 05:17:01

我正在考虑做类似下面的事情来满足我的要求。

SELECT 
a.number,
CASE WHEN ZEROIFNULL(a.caldate1) > ZEROIFNULL(b.caldate2)
THEN a.caldate1  -- This is working
ELSE
b.caldate2
END AS caldate
/*COALESCE (a.caldate1,caldate2) AS caldate*/ -- This is not giving max of dates
FROM 
table1  a
LEFT OUTER JOIN
table2  b
ON
a.number = b.number

谢谢你的帮助。现在按照上面的方法就完成了。

I am thinking of doing something like below to satisfy my requirement.

SELECT 
a.number,
CASE WHEN ZEROIFNULL(a.caldate1) > ZEROIFNULL(b.caldate2)
THEN a.caldate1  -- This is working
ELSE
b.caldate2
END AS caldate
/*COALESCE (a.caldate1,caldate2) AS caldate*/ -- This is not giving max of dates
FROM 
table1  a
LEFT OUTER JOIN
table2  b
ON
a.number = b.number

Thanks for helping. Now its done by the above method.

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