简体   繁体   English

如果条件在Oracle SQL中

[英]If Condition in Oracle SQL

I am working on a legacy system, and many of the database structures are horrendous (key / value pairs). 我在旧系统上工作,许多数据库结构都很糟糕(键/值对)。

I have the following select statement: 我有以下选择语句:

(
    SELECT D.F_VALUE
        FROM T_WEB_QUOTES_DATA D
        WHERE
            D.F_QUOTE_ID = TO_CHAR(VR_RENTAL.QUOTEID)
        AND D.F_KEY = 'Secondary_Driver_Forename'
) AS "SECONDARY_DRIVER_FORENAME"

So as you can see it is looking for a record where the F_Key column has a value of Secondary_Driver_Forename . 如您所见,它正在寻找F_Key列的值为Secondary_Driver_Forename The problem is there is another F_Key that holds the same exact information and I need to check for both keys. 问题是还有另一个F_Key拥有相同的确切信息,我需要检查两个键。

So what I want to do is: 所以我想做的是:

If there are no records where F_Key = Secondary_Driver_Forename or of such a record exists, but the value is an empty string or null, then I would like to go and look for a record where the F_Key is 2ndary_Driver_FirstName and if that does not exist (or is null), I would like to return a string saying No Key 如果没有记录,其中F_Key = Secondary_Driver_Forename或存在这样的记录,但是该值是空字符串或null,那么我想去查找F_Key为2ndary_Driver_FirstName的记录,如果该记录不存在(或为null),我想返回一个字符串,说No Key

How can I achieve this in Oracle SQL? 如何在Oracle SQL中实现这一目标?

I am thinking of something like this: 我在想这样的事情:

(
    SELECT (case when max(case when D.F_KEY in 'Secondary_Driver_Forename' then 1 else 0 end) = 1
                 then max(case when D.F_KEY in 'Secondary_Driver_Forename' then D.F_VALUE end)
                 else max(D.F_Value)
            end)
        FROM T_WEB_QUOTES_DATA D
        WHERE
            D.F_QUOTE_ID = TO_CHAR(VR_RENTAL.QUOTEID)
        AND D.F_KEY in ('Secondary_Driver_Forename', '2ndary_Driver_FirstName')
) AS "SECONDARY_DRIVER_FORENAME";

That is, do a conditional aggregation of the values. 即,对值进行条件汇总。 If the primary value is present, then use it. 如果存在主值,则使用它。 Otherwise, just choose the value that is there (either NULL or the value from the second key). 否则,只需选择那里的值( NULL或第二个键的值)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM