首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Snowflake-SQL -将行从一个查询添加到另一个查询

Snowflake-SQL -将行从一个查询添加到另一个查询
EN

Stack Overflow用户
提问于 2021-09-17 00:43:08
回答 1查看 45关注 0票数 0

我正在寻找组合查询结果的帮助。

我有一个查询,结果是:

代码语言:javascript
复制
YEAR    MONTH   T_DATE  TypeACount
2021    9   2021-09-01  13280
2021    8   2021-08-01  24508
2021    7   2021-07-01  21014
2021    6   2021-06-01  19666

查询:

代码语言:javascript
复制
select
    EXTRACT(YEAR FROM  f.value:paymentDate::date) as year,
    EXTRACT(MONTH FROM  f.value:paymentDate::date) as month,
    DATEFROMPARTS(year,month, 1) as t_date,
    count (case when v:payments[0].paymentMethod.source = 'SourceA' then 1 end) as "TypeACount"
from public.transactions t,
        lateral flatten(input => t.v, path => 'payments') f
where 
    f.value:paymentDate::date between DATEADD(dd, 1, last_day(DATEADD(mm, -4, GETDATE()))) and getdate()
    and
    f.value:paymentMethod.source = 'SourceA'
    and
    f.value:status = 'paid'
GROUP by
    month,year
ORDER by
    year DESC,
    month DESC

我还有另一个查询,结果是:

代码语言:javascript
复制
YEAR    MONTH   T_DATE  TypeBCount  TypeCCount  TypeDCount  TypeECount
2021    9   2021-09-01  3639        1340        6800        56401
2021    8   2021-08-01  6185        2482        11707       96122
2021    7   2021-07-01  5485        1680        10820       92394
2021    6   2021-06-01  5423        521         10643       97303

查询:

代码语言:javascript
复制
select
    EXTRACT(YEAR FROM v:created::date) as year,
    EXTRACT(MONTH FROM v:created::date) as month,
    DATEFROMPARTS(year,month, 1) as t_date,
    count (case when v:category[0].source = 'TypeB' then 1 end) as TypeBCount,
    count (case when v:category[0].source = 'TypeC' then 1 end) as TypeCCount,
    count (case when v:category[0].source = 'TypeD' then 1 end) as TypeDCount,
    count (case when v:category[0].source = 'TypeE' then 1 end) as TypeECount
from PUBLIC.TRANSACTIONS
where 
    v:created::date between DATEADD(dd, 1, last_day(DATEADD(mm, -4, GETDATE()))) and getdate()
GROUP by
    month,year
ORDER by
    year DESC,
    month DESC

有没有办法将TypeACount列添加到第二个查询中?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-17 01:07:45

您可以在CTE中的表表达式中运行这两个查询,然后将它们连接起来。

代码语言:javascript
复制
with A as
(
select
    EXTRACT(YEAR FROM  f.value:paymentDate::date) as year,
    EXTRACT(MONTH FROM  f.value:paymentDate::date) as month,
    DATEFROMPARTS(year,month, 1) as t_date,
    count (case when v:payments[0].paymentMethod.source = 'SourceA' then 1 end) as "TypeACount"
from public.transactions t,
        lateral flatten(input => t.v, path => 'payments') f
where 
    f.value:paymentDate::date between DATEADD(dd, 1, last_day(DATEADD(mm, -4, GETDATE()))) and getdate()
    and
    f.value:paymentMethod.source = 'SourceA'
    and
    f.value:status = 'paid'
GROUP by
    month,year
ORDER by
    year DESC,
    month DESC
),
B as
(
select
    EXTRACT(YEAR FROM v:created::date) as year,
    EXTRACT(MONTH FROM v:created::date) as month,
    DATEFROMPARTS(year,month, 1) as t_date,
    count (case when v:category[0].source = 'TypeB' then 1 end) as TypeBCount,
    count (case when v:category[0].source = 'TypeC' then 1 end) as TypeCCount,
    count (case when v:category[0].source = 'TypeD' then 1 end) as TypeDCount,
    count (case when v:category[0].source = 'TypeE' then 1 end) as TypeECount
from PUBLIC.TRANSACTIONS
where 
    v:created::date between DATEADD(dd, 1, last_day(DATEADD(mm, -4, GETDATE()))) and getdate()
GROUP by
    month,year
ORDER by
    year DESC,
    month DESC
)
select * from A
  left join B
    on A.YEAR = B.YEAR and A.MONTH = B.MONTH
;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69216568

复制
相关文章

相似问题

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