不返回null数据的情况

时间:2019-07-22 20:00:42

标签: postgresql

我有一个case语句,该语句不返回空值。我缺少什么来使它正常工作。

Select CASE 
                WHEN transfer_sources.input_lot_type = 'Dried' THEN Sum(transfer_sources.weight)
                WHEN transfer_sources.input_lot_type = 'Fresh' THEN NULL
            END
        from transfer_sources join bulk_lots on transfer_sources.source_id = bulk_lots.id
        where transfer_sources.input_lot_type = 'Dried' and bulk_lots.name = 'BS190208-010'
        group by transfer_sources.input_lot_type
        LIMIT 1

我想在新列中显示一个空值,因为我只想计算干燥的

1 个答案:

答案 0 :(得分:1)

您需要删除WHERE条条件transfer_sources.input_lot_type = 'Dried'

Select CASE 
            WHEN transfer_sources.input_lot_type = 'Dried' THEN Sum(transfer_sources.weight)
            WHEN transfer_sources.input_lot_type = 'Fresh' THEN NULL
        END
    from transfer_sources join bulk_lots on transfer_sources.source_id = bulk_lots.id
    where  bulk_lots.name = 'BS190208-010'
    group by transfer_sources.input_lot_type