I am now using Microsoft SQL, my code is:
SELECT TOP 1
[avail]
FROM [table1]
where [name] = 'abc'
order by [datetime] desc
I hope when [avail] exists, return the value of [avail], if not exists, return 0 or "Not Found"
Thanks!
I am now using Microsoft SQL, my code is:
SELECT TOP 1
[avail]
FROM [table1]
where [name] = 'abc'
order by [datetime] desc
I hope when [avail] exists, return the value of [avail], if not exists, return 0 or "Not Found"
Thanks!
5条答案
按热度按时间hts6caw31#
You can use this
jfewjypa2#
You can do in two ways:
Use
IF EXISTS
way:or store the
avail
value into a variable and process, the only risk in this way is if thetop 1 avail
is returns empty value, then you will get the result as zero.5sxhfpxr3#
Try to use COALESCE. It selects the data from the first argument that has a nonnull value. If avail is not null, return [avale], otherwise return "Not Found"
You can read about COALESCE in https://learn.microsoft.com/en-us/sql/t-sql/language-elements/coalesce-transact-sql
pbpqsu0x4#
Or you can use sum() function, like this
It will return 0 if no rows are there
pxy2qtax5#
If its top 1 you want or remove top command