SQL 最大值和 null

发布于 2024-08-05 22:13:37 字数 1265 浏览 6 评论 0原文

我正在尝试获取 USAGE_START_DATE 列中的 null 值。

到目前为止,我得到的是 USAGE START DATE 列中具有值的唯一最大记录,并且在 nulls 列中找不到任何具有 nulls 的记录>使用开始日期

我已经尝试过 ISNULLCOALESCENVLNZ

我有 2 张桌子:通过 Reservation_id 链接。

ReservationID   Usage Start Date
1                01/01/2001 00:00:00
1                02/01/2001 00:00:00
1                03/03/2001 00:00:00
2                NULL
2                NULL

到目前为止,我当前的代码: 这是我得到的:

ReservationID    Usage Start DAte
1                03/03/2001 00:00:00

这是我想要的:

ReservationID     Usage Start Date
1                 03/03/2001 00:00:00
2

谢谢!!!

为了简化我的代码:

SELECT distinct RENTAL_DETAILS_VW.RESERVATION_ID,  
RENTAL_DETAILS_VW.USAGE_START_DATE,
FROM BYNXFLEET_BI.RENTAL_DETAILS_VW, WILLOW2K.RESERVATIONS  
WHERE RENTAL_DETAILS_VW.USAGE_START_DATE = (
select max(case when ors2.USAGE_START_DATE is null 
    then {ts ' 2009-01-01 00:00:00 ' } 
    else ors2.USAGE_START_DATE END) 
FROM RENTAL_DETAILS_VW ors2 
where ors2.RESERVATION_ID=RESERVATIONS.RESERVATION_ID)

I am trying to get the null value in the USAGE_START_DATE column.

So far, what is i got is the unique max records of those with value in the USAGE START DATE column, and could not find any the records with nulls in the USAGE START DATE.

I already tried ISNULL, COALESCE, NVL, NZ.

I got 2 tables: linked by Reservation_id.

ReservationID   Usage Start Date
1                01/01/2001 00:00:00
1                02/01/2001 00:00:00
1                03/03/2001 00:00:00
2                NULL
2                NULL

So far with my current code: Here is what I got:

ReservationID    Usage Start DAte
1                03/03/2001 00:00:00

Here is what I want:

ReservationID     Usage Start Date
1                 03/03/2001 00:00:00
2

Thank you!!!

To simplify my code:

SELECT distinct RENTAL_DETAILS_VW.RESERVATION_ID,  
RENTAL_DETAILS_VW.USAGE_START_DATE,
FROM BYNXFLEET_BI.RENTAL_DETAILS_VW, WILLOW2K.RESERVATIONS  
WHERE RENTAL_DETAILS_VW.USAGE_START_DATE = (
select max(case when ors2.USAGE_START_DATE is null 
    then {ts ' 2009-01-01 00:00:00 ' } 
    else ors2.USAGE_START_DATE END) 
FROM RENTAL_DETAILS_VW ors2 
where ors2.RESERVATION_ID=RESERVATIONS.RESERVATION_ID)

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

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

发布评论

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

评论(1

憧憬巴黎街头的黎明 2024-08-12 22:13:38

您不会获得 USAGE_START_DATE 为 NULL 的任何行,因为您将它们过滤掉 - 最后的 WHERE 子句发现仅包含具有匹配 RESERVATION_ID 的最大日期的行。您是否尝试包含 NULL USAGE_START_DATE 行,即使它们没有匹配的预订?

更新:您不需要 DISTINCT,它可能会对您的结果产生负面影响。另外,由于您想要“空白”而不是 NULL,因此您需要使用 ISNULL。您将需要一个LEFT JOIN,并且我还添加了一些表别名以使其更易于阅读。这就是我认为你想要的:

SELECT rd.RESERVATION_ID, 
       ISNULL(MAX(rd.USAGE_START_DATE), '') as START_DATE,
  FROM BYNXFLEET_BI.RENTAL_DETAILS_VW rd
  LEFT
  JOIN WILLOW2K.RESERVATIONS r
    ON rd.RESERVATION_ID = r.RESERVATION_ID

You're not going to get any rows where USAGE_START_DATE is NULL because you're filtering them out - the WHERE clause at the end sees that only rows that have the max date for a matching RESERVATION_ID are included. Are you trying to include NULL USAGE_START_DATE rows, even if they don't have a matching reservation?

UPDATE: you don't need the DISTINCT, and it may negatively affect your results. Also, since you want a "Blank" instead of NULL, you'll need to use ISNULL. You'll need a LEFT JOIN, and I've also added some table aliases to make it a little easier to read. Here's what I think you want:

SELECT rd.RESERVATION_ID, 
       ISNULL(MAX(rd.USAGE_START_DATE), '') as START_DATE,
  FROM BYNXFLEET_BI.RENTAL_DETAILS_VW rd
  LEFT
  JOIN WILLOW2K.RESERVATIONS r
    ON rd.RESERVATION_ID = r.RESERVATION_ID
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文