SQL - 我应该在这个WHERE子句中添加IF或CASE吗?

时间:2016-11-02 05:26:25

标签: mysql sql if-statement case

我写了一个如下所示的查询:

SELECT
xx.DATE
xx.TRANSACTION_ID
xx.ID
xx.CODE

FROM TABLE_X xx

WHERE
xx.DATE >= TO_DATE('2015-01-01', 'YYYY-MM-DD')
AND xx.ID IN 
(SELECT
yy.id
FROM TABLE_Y yy
WHERE
yy.status = 'OPEN'
AND yy.type = 1
AND trunc(yy.date) = TO_DATE('{FREE_FORM}', 'YYYY-MM-DD')
)

ex.TABLE_X

DATE        TRANSACTION_ID  ID      CODE
1/1/2015    1020            CAT     RED
1/1/2015    1010            CAT     GREEN
1/2/2015    2020            DOG     RED
1/2/2015    2010            DOG     GREEN
1/3/2015    3030            RABBIT  BLUE
1/3/2015    3020            SPIDER  RED
1/3/2015    3010            RABBIT  GREEN
1/4/2015    4020            CAT     RED
1/4/2015    4010            SNAKE   GREEN
1/4/2015    4000            MONKEY  ORANGE
1/5/2015    5020            HORSE   RED
1/5/2015    5010            COW     GREEN

ex.TABLE_Y

date        id      status  type
1/1/2015    CAT     OPEN    1
1/2/2015    DOG     OPEN    1
1/3/2015    RABBIT  OPEN    1

使用这些示例表,我当前的输出是:

ex.output

DATE        TRANSACTION_ID  ID      CODE
1/1/2015    1020            CAT     RED
1/1/2015    1010            CAT     GREEN
1/2/2015    2020            DOG     RED
1/2/2015    2010            DOG     GREEN
1/3/2015    3030            RABBIT  BLUE
1/3/2015    3010            RABBIT  GREEN
1/4/2015    4020            CAT     RED

我想在WHERE子句中添加一些内容,例如:

if (ex.output.CODE = RED){
SELECT
xx.DATE
xx.TRANSACTION_ID
xx.ID
xx.CODE

FROM TABLE_X xx

WHERE xx.TRANSACTION_ID = ex.output.TRANSACTION_ID - 10
}
AND
if (ex.output.CODE = GREEN){
SELECT
xx.DATE
xx.TRANSACTION_ID
xx.ID
xx.CODE

FROM TABLE_X xx

WHERE xx.TRANSACTION_ID = ex.output.TRANSACTION_ID + 10
}

我的目标是最终输出:

DATE        TRANSACTION_ID  ID      CODE
1/1/2015    1020            CAT     RED
1/1/2015    1010            CAT     GREEN
1/2/2015    2020            DOG     RED
1/2/2015    2010            DOG     GREEN
1/3/2015    3030            RABBIT  BLUE
1/3/2015    3020            SPIDER  RED
1/3/2015    3010            RABBIT  GREEN
1/4/2015    4020            CAT     RED
1/4/2015    4010            SNAKE   GREEN

这可能吗?

2 个答案:

答案 0 :(得分:0)

尝试此查询(用MS T-SQL编写)

WITH    CTE
          AS ( SELECT   xx.DATE ,
                        xx.TRANSACTION_ID ,
                        xx.ID ,
                        xx.CODE
               FROM     TABLE_X xx
               WHERE    xx.DATE >= '2015-01-01'
                        AND xx.ID IN ( SELECT   yy.id
                                       FROM     TABLE_Y yy
                                       WHERE    yy.status = 'OPEN'
                                                AND yy.type = 1 )
             )
    SELECT  *
    FROM    CTE
    UNION
    SELECT  xx.*
    FROM    TABLE_X xx
            JOIN CTE ON xx.Transaction_ID = CTE.Transaction_ID - 10
                        AND CTE.Code = 'RED'
    UNION
    SELECT  xx.*
    FROM    TABLE_X xx
            JOIN CTE ON xx.Transaction_ID = CTE.Transaction_ID + 10
                        AND CTE.Code = 'GREEN'
    ORDER BY Date ,
            ID;

上述查询将提供

的输出

enter image description here

答案 1 :(得分:0)

如果我理解您的请求是正确的,那么ID='CAT'1/4/2015,4020,CAT,RED)的最后一行不应该在您的最终输出中。您无法在 ex.output 中的任何位置获得4020的计算TRANSACTION_ID

我会像这样构建查询:

SELECT 
xx.DATE,
xx.TRANSACTION_ID,
xx.ID,
xx.CODE 
FROM TABLE_X xx
WHERE TRANSACTION_ID IN 
(SELECT CASE eo.CODE WHEN 'RED' THEN TRANSACTION_ID-10 WHEN 'GREEN' THEN  TRANSACTION_ID+10 ELSE TRANSACTION_ID END  as trx_id
 FROM ex.output eo)