我的列为
FIRST_NAME
LAST_NAME
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
ADDRESS
AGE
我想从MONDAY
列扫描到Friday
列,然后仅从它们中找出最后一个数据加载列,例如:
如果我在MONDAY
,TUESDAY
,WEDNESDAY
中有数据,那么我从查询中得到的结果应该是'WEDNESDAY'
。
如果我在Monday
,Tuesday
,Wednesday
,Thursday
中有数据,则查询结果应为Thursday
。
如果仅在Monday
中有数据,则查询结果应为Monday
答案 0 :(得分:2)
如果我了解您的要求,则需要以下查询:
SELECT *,
CASE WHEN NOT Friday IS NULL THEN 'FRIDAY'
WHEN NOT Thursday IS NULL THEN 'THURSDAY'
WHEN NOT Wednesday IS NULL THEN 'WEDNESDAY'
WHEN NOT Tuesday IS NULL THEN 'TUESDAY'
WHEN NOT Monday IS NULL THEN 'MONDAY'
ELSE 'ALL NULLS' END AS Result
FROM YourTable
答案 1 :(得分:1)
COALESCE最适合您的用例。
SELECT COALESCE(FRIDAY, THURSDAY, WEDNESDAY, TUESDAY, MONDAY) AS SELECTEDDAY
FROM TABLE_NAME