如何返回0'如果日期使用max时不存在
我正在尝试在设备上实现的每项检查中的列中找到最新日期值。但是有一些设备从未进行过任何检查。我仍然需要获得一些回报。
例如:
device number date inspected
-------------------------------
01 2015-01-01,
01 2021-03-05,
02
03 2022-01-01
使用max的预期输出:
01 2021-03-05
02 0
03 2022-01-01
这是我的SQL代码:
SELECT
dbo.DEVICE.NUMBER AS device number,
MAX(dbo.DEVICE.DATE) AS Date
我似乎找不到正确执行的操作
I'm trying to find the most recent date value in a column for every inspection realized on a device. But there is some devices that never had any inspections. I still need need to get some return.
For example:
device number date inspected
-------------------------------
01 2015-01-01,
01 2021-03-05,
02
03 2022-01-01
Expected output using max:
01 2021-03-05
02 0
03 2022-01-01
This is my SQL code:
SELECT
dbo.DEVICE.NUMBER AS device number,
MAX(dbo.DEVICE.DATE) AS Date
I can't seem to find how to do it properly
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用此函数将任何空变成所需的默认值:
0
将是有效的日期值,因此'0'0'
可能可能工作更好。
我已经看到人们使用前哨值,例如
'1000-01-01'
甚至毫无意义的字符串,例如
'0000-00-00'
应付此类类型的详细信息。
如果1970年1月1日适用于您的申请
只需将其用作阶段开始即可。
You can turn any NULL into a desired default value with this function:
It's not obvious that
0
will be a valid date value, so'0'
mightwork better.
I have seen folks use sentinel values like
'1000-01-01'
or even nonsensical strings like
'0000-00-00'
to cope with such type details.
If January 1st 1970 works for your application,
just use that as start-of-epoch.