简体   繁体   English

将查询合并为一个查询

[英]Combine queries into one query

How do I combine these two queries into one query? 如何将这两个查询合并为一个查询? Initially, I was storing the results of the first query into a table and then using the second query to query that table, but I do not want to do that anymore. 最初,我将第一个查询的结果存储到一个表中,然后使用第二个查询来查询该表,但是我不想再这样做了。 Any suggestions are appreciated. 任何建议表示赞赏。

Query 1 查询1

with base (org1, ind1, org2, ind2, org3, ind3)
as (
    --Lot of sub queries and processing
   )
SELECT org
    , ind
    , sum(count_final) AS count
FROM (
SELECT org1 as org , ind2 as ind, count(*) as count_final from base
group by org1 , ind2 
union all
SELECT org1 as org , ind3 as ind, count(*) as count_final from base
group by org1 , ind3 
union all
SELECT org2 as org , ind1 as ind, count(*) as count_final from base
group by org2 , ind1
union all
SELECT org2 as org , ind3 as ind, count(*) as count_final from base
group by org2 , ind3
union all
SELECT org3 as org , ind1 as ind, count(*) as count_final from base
group by org3 , ind1
union all
SELECT org3 as org , ind2 as ind, count(*) as count_final from base
group by org3 , ind2
) x
GROUP BY org
    , ind

Query 2 查询2

with cte as (
  select 
    ind, 
    sum(count) as tot 
  from 
    Query1Above 
  group by 
    ind
), 
xyz as (
  select 
    org as npi, 
    sum(count) as cnt 
  from 
    Query1Above 
  group by 
    org
) 
select 
  px.ind, 
  px.org, 
  '' as rank, 
  case when (px.count / cte.tot)* 100 < 2 then '<2%' when (px.count / cte.tot)* 100 >= 2 
  and (px.count / cte.tot)* 100 < 10 then '2%-10%' px.count as clm_cnt, 
  xyz.cnt as org_total 
from 
  Query1Above px 
  join cte on cte.ind = px.ind 
  join xyz on xyz.npi = px.org 
order by 
  px.org, 
  px.count desc

Simply incorporate Query1Above in its own CTE since CTEs can be based on a previous CTE in order of definition. 只需将Query1Above合并到其自己的CTE中,因为CTE可以基于定义的顺序基于先前的CTE。

with base (org1, ind1, org2, ind2, org3, ind3) as (
    --Lot of sub queries and processing
   ),    
Query1Above as (
    SELECT org
        , ind
        , sum(count_final) AS count
    FROM (
        SELECT org1 as org , ind2 as ind, count(*) as count_final from base
        GROUP BY org1 , ind2 
        UNION ALL
        SELECT org1 as org , ind3 as ind, count(*) as count_final from base
        GROUP BY org1 , ind3 
        UNION ALL
        SELECT org2 as org , ind1 as ind, count(*) as count_final from base
        GROUP BY org2 , ind1
        UNION ALL
        SELECT org2 as org , ind3 as ind, count(*) as count_final from base
        GROUP BY org2 , ind3
        UNION ALL
        SELECT org3 as org , ind1 as ind, count(*) as count_final from base
        GROUP BY org3 , ind1
        UNION ALL
        SELECT org3 as org , ind2 as ind, count(*) as count_final from base
        GROUP BY org3 , ind2
        ) x
    GROUP BY org
        , ind
),    
cte as (
  select 
    ind, 
    sum(count) as tot 
  from 
    Query1Above 
  group by 
    ind
), 
xyz as (
  select 
    org as npi, 
    sum(count) as cnt 
  from 
    Query1Above 
  group by 
    org
) 

-- ...final main query...

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

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