简体   繁体   English

AWS Redshift SQL使用查询结果执行另一个查询

[英]AWS Redshift SQL using the results of a query to execute another query

I have a table that has descriptions. 我有一张桌子,上面有描述。 I am using a query based on author IDs to find the relevant descriptions. 我正在使用基于作者ID的查询来查找相关描述。 I use this code: 我使用以下代码:

 SELECT
    count(*), h.book_desc
    FROM
    native.authbill p, native.chg h
    where p.book_chg_id = h.book_chg_id
    and 
    (p.aut_key in (
    select aut_key
    from native.authcodes p
    where p.auth_code in (74233, 23421) )
    or p.aut_key in (
    select aut_key
    from native.pubisbn_proc pat
    where isbn_code in ('373423','0256543','0257535')))
    group by h.book_desc

Then I have a another query that finds book descriptions based on a genre 然后我有另一个查询,该查询根据类型查找书名

SELECT
    count(*), h.book_desc
    FROM
    native.authbill p, native.chg h
    where p.book_chg_id = h.book_chg_id
    and p.genre_code in (
    SELECT distinct chg.genre_code
    FROM native.chgset chg
    where chg.genre_desc in ('Sci-fi', 'Action', 'Rom-com')
                        )

What I want to do is take all the results from the first query and narrow them down further by the second query. 我想做的是从第一个查询中获取所有结果,并在第二个查询中进一步缩小范围。 I get 150000 results from the first query and 250000 when i run the second query. 我从第一个查询获得150000个结果,而在运行第二个查询时获得250000个结果。 How can I narrow down the first query results by the second query. 如何缩小第二个查询的第一个查询结果。 So I want to use the results from the first query and then make sure those descriptions also are in the genre description array. 因此,我想使用第一个查询的结果,然后确保这些描述也都在体裁描述数组中。 This is in AWS Redshift SQL. 这在AWS Redshift SQL中。 Any help is appreciated. 任何帮助表示赞赏。

You could use a CTE, and a join to intersect the two sets: 您可以使用CTE和联接将两个集合相交:

with q1 as (
SELECT
    count(*) as cnt, h.book_desc
    FROM
    native.authbill p, native.chg h
    where p.book_chg_id = h.book_chg_id
    and 
    (p.aut_key in (
    select aut_key
    from native.authcodes p
    where p.auth_code in (74233, 23421) )
    or p.aut_key in (
    select aut_key
    from native.pubisbn_proc pat
    where isbn_code in ('373423','0256543','0257535')))
    group by h.book_desc
),
q2 as (
SELECT
    count(*) as cnt, h.book_desc
    FROM
    native.authbill p, native.chg h
    where p.book_chg_id = h.book_chg_id
    and p.genre_code in (
    SELECT distinct chg.genre_code
    FROM native.chgset chg
    where chg.genre_desc in ('Sci-fi', 'Action', 'Rom-com')
                        )
)
select book_desc, q1.cnt, q2.cnt 
from q1 join q2 using book_desc

I think you can just use an and condition in the where clause: 我认为您可以在where子句中使用and条件:

select count(*), h.book_desc
from native.authbill p join
     native.chg h
     on p.book_chg_id = h.book_chg_id
where (p.aut_key in (select aut_key
                     from native.authcodes p
                     where p.auth_code in (74233, 23421)
                    ) or
       p.aut_key in (select aut_key
                     from native.pubisbn_proc pat
                     where isbn_code in ('373423', '0256543', '0257535')
                    )
      ) and
      p.genre_code in (select chg.genre_code
                       from native.chgset chg
                       where chg.genre_desc in ('Sci-fi', 'Action', 'Rom-com')
                      ) 
group by h.book_desc;

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

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