如何重新创建此子查询到视图

时间:2019-04-05 19:33:16

标签: view subquery mariadb

MariaDb不支持子查询,所以我试图找出采取此查询并将其转换为视图的最佳方法。我不确定如何做到这一点,因为我知道我需要创建多个视图才能做到这一点,但是我不确定如何分解。

已被要求从该数据集创建视图,并且能够使用(子查询)创建此查询,但是现在我需要将其转换为视图,但我没有碰壁。尝试将其分解为可分解的片段以创建多个视图以创建主视图时,知道该从哪里开始。

SELECT
`TestDelete`.`CustomerInputName` AS `CustomerInputName`,
`TestDelete`.`customerInputCustid` AS `customerInputCustid`,
( CASE WHEN ( `TestDelete`.`YQ` = '2017Q1') and `TestDelete`.`customerInputCustid` in (select `portfolio_review_ACC1_2017Q1_REUP`.`customerInputCustid` from portfolio_review_ACC1_2017Q1_REUP)
THEN `TestDelete`.`Quarter` 
WHEN ( `TestDelete`.`YQ` = '2017Q1' and `TestDelete`.`customerInputCustid` not in (select `portfolio_review_ACC1_2017Q1_REUP`.`customerInputCustid` from portfolio_review_ACC1_2017Q1_REUP)) or 
( `TestDelete`.`YQ` = '2017Q2' and `TestDelete`.`customerInputCustid` not in (select `portfolio_review_ACC1_2017Q1_REUP`.`customerInputCustid` from portfolio_review_ACC1_2017Q1_REUP)) or 
( `TestDelete`.`YQ` = '2017Q3' and `TestDelete`.`customerInputCustid` not in (select `portfolio_review_ACC1_2017Q1_REUP`.`customerInputCustid` from portfolio_review_ACC1_2017Q1_REUP)) or 
( `TestDelete`.`YQ` = '2017Q4' and `TestDelete`.`customerInputCustid` not in (select `portfolio_review_ACC1_2017Q1_REUP`.`customerInputCustid` from portfolio_review_ACC1_2017Q1_REUP)) or 
( `TestDelete`.`YQ` = '2018Q1' and `TestDelete`.`customerInputCustid` not in (select `portfolio_review_ACC1_2017Q1_REUP`.`customerInputCustid` from portfolio_review_ACC1_2017Q1_REUP)) or 
( `TestDelete`.`YQ` = '2018Q2' and `TestDelete`.`customerInputCustid` not in (select `portfolio_review_ACC1_2017Q1_REUP`.`customerInputCustid` from portfolio_review_ACC1_2017Q1_REUP)) or 
( `TestDelete`.`YQ` = '2018Q3' and `TestDelete`.`customerInputCustid` not in (select `portfolio_review_ACC1_2017Q1_REUP`.`customerInputCustid` from portfolio_review_ACC1_2017Q1_REUP)) or 
( `TestDelete`.`YQ` = '2018Q4' and `TestDelete`.`customerInputCustid` not in (select `portfolio_review_ACC1_2017Q1_REUP`.`customerInputCustid` from portfolio_review_ACC1_2017Q1_REUP))
then 'First'
END ) AS `2017Q1`

1 个答案:

答案 0 :(得分:0)

看起来像这样,但是更快:

`TestDelete`.`YQ` IN ('2017Q2', '2017Q3', ...)
     AND NOT EXISTS(
           SELECT 1 FROM portfolio_review_ACC1_2017Q1_REUP
               WHERE customerInputCustid = TestDelete.customerInputCustid
                   )

同时,您的SELECT似乎至少缺少FROM customerInputCustid