用postgres替换SELECT返回值'询问

时间:2014-03-25 20:45:49

标签: sql postgresql

我在postgres模型查询中遇到了一个问题。

下面的查询返回一个包含三列的表:“central”,“imsi”和“mapver”。 有时,查询将在“mapver”中返回空白值,但我不能在其中存在空格。 | 我怎样才能使这个查询用“ - ”或单词代替空格,比如“WrongBlank”? 它是一个varchar字段。

SELECT Test_Configs.central, Test_Configs.imsi, 
       Test_Configs.mapver      

FROM config_imsis_centrais AS Default_Configs              -- Valores padrão da central correta
    LEFT JOIN config_imsis_centrais AS Test_Configs        -- Valores das centrais a serem testadas
        ON Default_Configs.central = 'ZBLM04'
        AND Default_Configs.ts = (SELECT MAX(ts) FROM config_imsis_centrais)
        AND Default_Configs.imsi = Test_Configs.imsi
        AND Default_Configs.ts = Test_Configs.ts
        AND Test_Configs.central <> Default_Configs.central
WHERE (                                                    -- Análise:
            COALESCE(Default_Configs.mapver, 'null') <> COALESCE(Test_Configs.mapver, 'null') AND
            Test_Configs.central <> ''
       )

一个更简单的例子...... 我经常得到:

central  |   imsi   |   mapver
--------------------------------
ZSPO03   |   74402  |   
ZSPO03   |   74401  |   
ZSPO03   |   72434  |   
ZSPO03   |   72415  |

但我想:

central  |   imsi   |   mapver
--------------------------------
ZSPO03   |   74402  |     -
ZSPO03   |   74401  |     -
ZSPO03   |   72434  |     -
ZSPO03   |   72415  |     -

非常感谢!

1 个答案:

答案 0 :(得分:1)

http://www.postgresql.org/docs/current/static/functions-conditional.html#FUNCTIONS-COALESCE-NVL-IFNULL

COALESCE函数返回其非空的第一个参数。仅当所有参数都为null时才返回Null。当检索数据以供显示时,它通常用于将默认值替换为空值,例如:

SELECT COALESCE(description, short_description, '(none)')

文本常量永远不能为空。因此,如果前面的内容为null,则返回文本字符串。

SELECT Test_Configs.central, Test_Configs.imsi, 
       COALESCE(Test_Configs.mapver, 'whatever string you want')      

FROM config_imsis_centrais AS Default_Configs              -- Valores padrão da central correta
    LEFT JOIN config_imsis_centrais AS Test_Configs        -- Valores das centrais a serem testadas
        ON Default_Configs.central = 'ZBLM04'
        AND Default_Configs.ts = (SELECT MAX(ts) FROM config_imsis_centrais)
        AND Default_Configs.imsi = Test_Configs.imsi
        AND Default_Configs.ts = Test_Configs.ts
        AND Test_Configs.central <> Default_Configs.central
WHERE (                                                    -- Análise:
            COALESCE(Default_Configs.mapver, 'null') <> COALESCE(Test_Configs.mapver, 'null') AND
            Test_Configs.central <> ''
       )

编辑,有空字符串,而不是空值

参考:http://www.postgresql.org/docs/9.3/static/functions-conditional.html#FUNCTIONS-CASE

SELECT Test_Configs.central, Test_Configs.imsi, 
       CASE Test_Configs.mapver WHEN '' THEN '-'
           ELSE COALESCE(Test_Configs.mapver, '-') 
       END AS mapver  

FROM config_imsis_centrais AS Default_Configs              -- Valores padrão da central correta
    LEFT JOIN config_imsis_centrais AS Test_Configs        -- Valores das centrais a serem testadas
        ON Default_Configs.central = 'ZBLM04'
        AND Default_Configs.ts = (SELECT MAX(ts) FROM config_imsis_centrais)
        AND Default_Configs.imsi = Test_Configs.imsi
        AND Default_Configs.ts = Test_Configs.ts
        AND Test_Configs.central <> Default_Configs.central
WHERE (                                                    -- Análise:
            COALESCE(Default_Configs.mapver, 'null') <> COALESCE(Test_Configs.mapver, 'null') AND
            Test_Configs.central <> ''
       )