我有一个表boa_user,它将三个用户返回为..
SELECT DISTINCT u.name, u.updated_by, u.updateddate
FROM YTR_USER u, YTR_user_group ug, YTR_group g
WHERE u.id = ug.user_id
AND ug.group_id = g.id
AND u.ACTIVE_FLAG='Y'
AND g.id not in (13,11,15)
ORDER BY u.name
下面是格式..
NAME UPDATED_BY UPDATEDDATE
AAA edfef 03.03.03 14:29:34.000
BBB ferer 12.12.12 15:13:23.756
现在coulmn属于
类型name VARCHAR2(30)
updateddate TIMESTAMP(6)
现在rite现在查询返回结果,但在它们之间有一些列名称值为null且updateddate值为null。
我的查询是请告知查询,以便如果name为null并且更新日期为null,那么它的defalut值应该像“AFGT”并且updateddate默认值应该是'05 .03.13 05:29:34',' MM.DD.YY HH:MI'
如果名称和更新日期为空,请告知查询添加这些默认值
答案 0 :(得分:2)
在选择部分中使用NVL-Function。
类似的东西:
SELECT DISTINCT
nvl(u.name, <defaultname>),
u.updated_by,
nvl(u.updateddate, <deafultdate>)
FROM YTR_USER u, YTR_user_group ug, YTR_group g
WHERE u.id = ug.user_id
AND ug.group_id = g.id
AND u.ACTIVE_FLAG='Y'
AND g.id not in (13,11,15)
ORDER BY u.name
请注意,在name is null
或 updateddate is null
时,我希望您需要默认的名称值。{{1}}。你的问题在这里有点不清楚。
因此,当name为null且updateddate不为null时,上面的语句也将返回default-name。只要相应的字段为空,它就会返回默认值 - 无论其他字段是否为空。
答案 1 :(得分:0)
如果我理解正确,请在name IS NULL AND updateddate IS NULL
时使用:
name = 'AFGT'
updateddate = TO_TIMESTAMP('05.03.13 05:29:34', 'MM.DD.YY HH:MI')
如果这是正确的,那么您需要的只是一个CASE
。
但是,您是否希望DISTINCT
应用于默认行。如果您这样做,请使用:
SELECT DISTINCT x.corrected_name, x.corrected_updateddate
FROM (
SELECT
CASE WHEN u.name IS NULL AND u.updateddate IS NULL
THEN 'AFGT'
ELSE u.name
END AS corrected_name,
u.updated_by,
CASE WHEN u.name IS NULL AND u.updateddate IS NULL
THEN TO_TIMESTAMP('05.03.13 05:29:34', 'MM.DD.YY HH:MI')
ELSE u.updateddate
END AS corrected_updateddate
FROM YTR_USER u, YTR_user_group ug, YTR_group g
WHERE u.id = ug.user_id
AND ug.group_id = g.id
AND u.ACTIVE_FLAG='Y'
AND g.id not in (13,11,15)
ORDER BY u.name
) AS x;
如果您想在结果中显示所有默认行:
SELECT x.corrected_name, x.corrected_updateddate
FROM (
SELECT DISTINCT
CASE WHEN u.name IS NULL AND u.updateddate IS NULL
THEN 'AFGT'
ELSE u.name
END AS corrected_name,
u.updated_by,
CASE WHEN u.name IS NULL AND u.updateddate IS NULL
THEN TO_TIMESTAMP('05.03.13 05:29:34', 'MM.DD.YY HH:MI')
ELSE u.updateddate
END AS corrected_updateddate,
u.name,
u.updateddate
FROM YTR_USER u, YTR_user_group ug, YTR_group g
WHERE u.id = ug.user_id
AND ug.group_id = g.id
AND u.ACTIVE_FLAG='Y'
AND g.id not in (13,11,15)
ORDER BY u.name
) AS x;