【问题标题】:How can you write a query for Amazon redshift database such that the where clause of the correlated query has two conditions?如何为 Amazon redshift 数据库编写查询,使相关查询的 where 子句具有两个条件?
【发布时间】:2016-07-06 16:59:40
【问题描述】:

例子-

SELECT date, name, 
       (SELECT value
        FROM this_table
        WHERE col1= 'test1' and col2='test'
       ) AS num_sloths_bought_before
FROM source_table;

当我在 redshift 中执行查询时,我得到了这个错误 -

[Amazon](500310) Invalid operation: This type of correlated subquery pattern is not supported yet;

【问题讨论】:

    标签: sql postgresql amazon-redshift


    【解决方案1】:

    您通常可以使用联接重写相关标量子查询,对于您的查询,它是左联接:

    SELECT date, name, dt.value AS num_sloths_bought_before
    FROM source_table
    LEFT JOIN
     ( SELECT val, 
          value
       FROM this_table
       WHERE col2='test'
     ) this_table
    ON source_table.val= this_table.val;
    

    【讨论】:

    • 谢谢!我试过这个并且它有效。我现在面临的问题是我的 where 子句引用了另一个表 --SELECT date, name, dt.value AS num_sloths_bought_before FROM source_table, ( SELECT value FROM this_table WHERE source_table.val= this_table.val and col2='test' ) dt;
    • 当我在cmets中执行上述查询时,我得到错误-“[Amazon](500310) Invalid operation: FROM中的子查询可能不引用相同查询级别的其他关系;”跨度>
    • @Tisha:所以它实际上是相关的,我编辑了我的答案,现在它是一个左连接
    • 实际上我对此有另一个问题。它无法访问 this_table,因为我使用的相关查询类似于 - “....Select val from database.this_table x ....” 我在这里用我的原始查询发布了一个新问题更清楚 - stackoverflow.com/questions/38231015/…
    猜你喜欢
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 2016-08-09
    • 1970-01-01
    • 2019-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多