首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从CTE中的CTE进行分组

从CTE中的CTE进行分组
EN

Stack Overflow用户
提问于 2019-09-10 23:51:40
回答 1查看 36关注 0票数 0

我有一个像这样的CTE -

代码语言:javascript
复制
with cte (
    org1, org2, org3
    , ind1, ind2, ind3
  ) as (
    -- a bunch of queries
    ) 
  SELECT 
    org1 as org, 
    ind2 as ind, 
    count(*) as count_final 
  from 
    cte 
  group by 
    org1, 
    ind2 
  union all 
  SELECT 
    org1 as org, 
    ind3 as ind, 
    count(*) as count_final 
  from 
    cte 
  group by 
    org1, 
    ind3 
  union all 
  SELECT 
    org2 as org, 
    ind1 as ind, 
    count(*) as count_final 
  from 
    cte 
  group by 
    org2, 
    ind1 
  union all 
  SELECT 
    org2 as org, 
    ind3 as ind, 
    count(*) as count_final 
  from 
    cte 
  group by 
    org2, 
    ind3 
  union all 
  SELECT 
    org3 as org, 
    ind1 as ind, 
    count(*) as count_final 
  from 
    cte 
  group by 
    org3, 
    ind1 
  union all 
  SELECT 
    org3 as org, 
    ind2 as ind, 
    count(*) as count_final 
  from 
    cte 
  group by 
    org3, 
    ind2;

一旦我有了org,ind和count_final,我该怎么做-

代码语言:javascript
复制
select org, ind, sum(count_final) as count from cte_final
group by org, ind

我试图在这个cte之上创建另一个cte,这样我就可以运行这个查询了,但是我得到了一个错误,指出存在对org的不明确引用。这是我试过的-

代码语言:javascript
复制
with cte_final (org, ind)
as (
with cte (org1, org2, org3, ind1, ind2, ind3)
as (

--a bunch of queries

    )

SELECT org1 as org , ind2 as ind, count(*) as count_final from cte
group by org1 , ind2 
union all
SELECT org1 as org , ind3 as ind, count(*) as count_final from cte
group by org1 , ind3 
union all
SELECT org2 as org , ind1 as ind, count(*) as count_final from cte
group by org2 , ind1
union all
SELECT org2 as org , ind3 as ind, count(*) as count_final from cte
group by org2 , ind3
union all
SELECT org3 as org , ind1 as ind, count(*) as count_final from cte
group by org3 , ind1
union all
SELECT org3 as org , ind2 as ind, count(*) as count_final from cte
group by org3 , ind2
;
)
select org, ind, sum(count_final) as count from cte_final
group by org, ind;

输入-

代码语言:javascript
复制
Org             Ind             Count_Final
"1023043221"    "156832423986"  "17"
"1023043221"    "156832423986"  "1"
"14325495102"   "142722349564"  "1"

我想要这个结果-

代码语言:javascript
复制
Org             Ind             Count_Final
"1023043221"    "156832423986"  "18"
"14325495102"   "142722349564"  "1"
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-11 00:00:57

我想你只需要一个cte。尝试:

代码语言:javascript
复制
WITH cte
AS (

--bunch of queries

    )
SELECT org
    , ind
    , sum(count_final) AS count
FROM (
    SELECT org1 AS org
        , ind2 AS ind
        , count(*) AS count_final
    FROM cte
    GROUP BY org1
        , ind2

    UNION ALL

    SELECT org1 AS org
        , ind3 AS ind
        , count(*) AS count_final
    FROM cte
    GROUP BY org1
        , ind3

    UNION ALL

    SELECT org2 AS org
        , ind1 AS ind
        , count(*) AS count_final
    FROM cte
    GROUP BY org2
        , ind1

    UNION ALL

    SELECT org2 AS org
        , ind3 AS ind
        , count(*) AS count_final
    FROM cte
    GROUP BY org2
        , ind3

    UNION ALL

    SELECT org3 AS org
        , ind1 AS ind
        , count(*) AS count_final
    FROM cte
    GROUP BY org3
        , ind1

    UNION ALL

    SELECT org3 AS org
        , ind2 AS ind
        , count(*) AS count_final
    FROM cte
    GROUP BY org3
        , ind2
    ) x
GROUP BY org
    , ind;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57874332

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档