1

我有一个表,其中 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

现在 coumn 是类型

name        VARCHAR2(30)
updateddate TIMESTAMP(6)

现在 rite now 查询返回结果,但在这之间有一些列,其中 name 值为 null,updateddate 值为 null。

我的查询是请告知一个查询,以便如果名称为空且更新日期为空,那么它的默认值应该类似于“AFGT”并且更新日期默认值应该是 '05.03.13 05:29:34'、'MM.DD .YY HH:MI'

如果名称和更新日期为空,请建议查询添加这些默认值

4

2 回答 2

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-value whenname is null 默认的 updateddate-value when updateddate is null。你的问题在这里有点不清楚。

因此,例如上面的语句也会在 name 为 null 且 updateddate 不为 null 时返回 default-name。只要相应字段为空,它就会返回默认值——无论其他字段是否为空。

于 2013-08-01T11:15:40.540 回答
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;
于 2013-08-01T10:27:44.810 回答