SQL 最大值和 null
我正在尝试获取 USAGE_START_DATE
列中的 null
值。
到目前为止,我得到的是 USAGE START DATE
列中具有值的唯一最大记录,并且在
。nulls
列中找不到任何具有 nulls
的记录>使用开始日期
我已经尝试过 ISNULL
、COALESCE
、NVL
、NZ
。
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不会获得 USAGE_START_DATE 为 NULL 的任何行,因为您将它们过滤掉 - 最后的 WHERE 子句发现仅包含具有匹配 RESERVATION_ID 的最大日期的行。您是否尝试包含 NULL USAGE_START_DATE 行,即使它们没有匹配的预订?
更新:您不需要 DISTINCT,它可能会对您的结果产生负面影响。另外,由于您想要“空白”而不是 NULL,因此您需要使用
ISNULL
。您将需要一个LEFT JOIN
,并且我还添加了一些表别名以使其更易于阅读。这就是我认为你想要的: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 aLEFT JOIN
, and I've also added some table aliases to make it a little easier to read. Here's what I think you want: