内部查询案例表达式中的外部查询列

时间:2017-11-19 01:13:58

标签: mysql sql database vertica

我正在尝试在vertica中写下面的查询

`SELECT a.* 
FROM   a 
WHERE  a.country="India" 
AND    a.language ="Hindi" 
AND   ( CASE WHEN (a.spoken = true 
             AND exist ( select 1 
                         FROM   b 
                         WHERE  b.country=a.country 
                          AND    b.language=a.language 
                          AND   ( CASE WHEN (a.population <b.population
                                    AND    a.statsyear > b.statsyear)) 
                                      THEN true //pick recent stats
                                    WHEN (a.population > b.population) 
                                      THEN true 
                                          ELSE false 
                                   END)) THEN true 
                     WHEN (a.written = true ) THEN
                       true   
                     ELSE false 
              END)`

它不起作用,因为我们无法引用&#34; a.population&#34;内部查询表达式中的外部查询字段。我尝试重写它,或者假设Vertica不允许它。

我该如何重写

我在MySQL本地框中创建了以下表格

 Example of Tables and Results
    CREATE TABLE tableA
(
   id          INT,
   country     VARCHAR(20),
   language    VARCHAR(20),
   spoken      INT,
   written     INT,
   population  INT,
   stats       INT
)
insert into tableA values(1,'India','Hindi',1,0,9,2010)
insert into tableA values(2,'India','Hindi',1,0,11,2011)
insert into tableA values(3,'India','Hindi',1,0,10,2012)
insert into tableA values(4,'India','Hindi',0,1,10,2013)
insert into tableA values(5,'India','Hindi',1,1,10,2012)
insert into tableA values(6,'India','English',1,1,10,2012)
CREATE TABLE tableB
(
   id          INT,
   country     VARCHAR(20),
   language    VARCHAR(20),
   population  INT,
   stats       INT
)
insert into TableB values(1,'India','Hindi',10,2009)
insert into TableB values(2,'India','Hindi',10,2011)
insert into TableB values(3,'India','Hindi',10,2012)

Rewrote the query slightly in different way

select distinct a.id
from (
SELECT a.* 
FROM   TableA a 
WHERE  a.country="India" 
AND    a.language ="Hindi" ) a, TableB   b
WHere ( CASE WHEN a.written=1 THEN
               TRUE
            WHEN ( (a.spoken = 1) AND (a.country=b.country) AND (a.language=b.language)) THEN
                 (case WHEN ((a.population < b.population) AND (a.stats > b.stats)) THEN
                        TRUE
                  WHEN (a.population > b.population) THEN
                       TRUE
                  ELSE
                     FALSE              
                END)
             ELSE
               FALSE
             END)
 got below results
 1,2,4,5

这就是我需要的,现在请你帮我写一些更有效率的方式

1 个答案:

答案 0 :(得分:0)

等效的布尔逻辑:

SELECT DISTINCT a.* 
FROM   TableA a 
left join TableB b on a.country=b.country AND a.language=b.language
WHERE  a.country='India' 
AND    a.language ='Hindi'
AND (
        a.written=1
    OR
       (a.spoken = 1 AND a.population < b.population AND a.stats > b.stats)
    OR
       a.population > b.population
    )
;

<强>结果:

+----+---------+----------+--------+---------+------------+-------+
| id | country | language | spoken | written | population | stats |
+----+---------+----------+--------+---------+------------+-------+
|  1 | India   | Hindi    |      1 |       0 |          9 |  2010 |
|  2 | India   | Hindi    |      1 |       0 |         11 |  2011 |
|  4 | India   | Hindi    |      0 |       1 |         10 |  2013 |
|  5 | India   | Hindi    |      1 |       1 |         10 |  2012 |
+----+---------+----------+--------+---------+------------+-------+

Demo