组合两个表并总结 Postgresql

挑战者

我有一个处理赔偿金的公司的数据库。我必须。我正在处理三个表:结论、m_email 和 m_lead。在“结论”表中,我有:id 和创建日期。在 m_email 表中,我有:id,打开了多少,点击了多少。在 m_lead 表中:id、conclusion_id、creation_date。我需要检查以下内容: 1. 将一年分为四个部分,并检查从 1 月到 4 月、从 4 月到 7 月等有多少申请。 2. 接下来我需要附上一张表格以获得类似的信息。例如:

ID = 1      
number_of_aplications = 5000     
number_of_leads = 7000

下面我放了我的代码。我有两个问题: 1. 我不知道如何从三个月中添加几个月才能有一个结果。2.我不知道如何将潜在客户的数量连接到此代码

select date_part('month', creating_date) as "1-3", count(id) as 
"Number of applications"
from applications
where date_part('month', creating_date) between  '01' and '03' AND
date_part('year', creating_date) between '2017' and '2018'
group by  date_part('month', creating_date)
order by count(id) DESC ;
杰苏斯

你的要求对我来说有点难以理解。但是对于您问题的第一部分,我想我可以回答:

初始情况

您有一个表“应用程序(id int 主键,creation_date 时间戳)”

你需要什么(我的理解)

有每三个月的申请次数(一月到三月等)

一个可能的解决方案

select a.delta, sum(applicationByMonth) as applicationByMonth, sum(leadByMonth) as leadByMonth
from 
(
    select '1 trimester' as delta, cast(date_part('month', creation_date) as int) as month, count(id) as applicationByMonth
    from applications
    where cast(date_part('month', creation_date) as int) <= 3
    group by cast(date_part('month', creation_date) as int)
    union
    select '2 trimester' as delta, cast(date_part('month', creation_date) as int) as month, count(id) as applicationByMonth
    from applications
    where cast(date_part('month', creation_date) as int) between 4 and 6
    group by cast(date_part('month', creation_date) as int)
    union
    select '3 trimester' as delta, cast(date_part('month', creation_date) as int) as month, count(id) as applicationByMonth
    from applications
    where cast(date_part('month', creation_date) as int) between 7 and 9
    group by cast(date_part('month', creation_date) as int)
    union
    select '4 trimester' as delta, cast(date_part('month', creation_date) as int) as month, count(id) as applicationByMonth
    from applications
    where cast(date_part('month', creation_date) as int) between 10 and 12
    group by cast(date_part('month', creation_date) as int)
) as a
inner join 
(
    select '1 trimester' as delta, cast(date_part('month', creation_date) as int) as month, count(id) as leadByMonth
    from m_lead
    where cast(date_part('month', creation_date) as int) <= 3
    group by cast(date_part('month', creation_date) as int)
    union
    select '2 trimester' as delta, cast(date_part('month', creation_date) as int) as month, count(id) as leadByMonth
    from m_lead
    where cast(date_part('month', creation_date) as int) between 4 and 6
    group by cast(date_part('month', creation_date) as int)
    union
    select '3 trimester' as delta, cast(date_part('month', creation_date) as int) as month, count(id) as leadByMonth
    from m_lead
    where cast(date_part('month', creation_date) as int) between 7 and 9
    group by cast(date_part('month', creation_date) as int)
    union
    select '4 trimester' as delta, cast(date_part('month', creation_date) as int) as month, count(id) as leadByMonth
    from m_lead
    where cast(date_part('month', creation_date) as int) between 10 and 12
    group by cast(date_part('month', creation_date) as int)
) as l 
    on a.delta = l.delta
group by a.delta
order by 1;

我认为您可以从那里进行调整以满足您的需求。

希望这可以帮助 ;)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

(代码完成)组合两个任务列表(表)以显示总结果

使用PostgreSQL减去两个日期

PostgreSQL:基于一个数组(外键)列联接两个表

PostgreSQL:追加两个具有不同列的表

在PostgreSQL和GROUP BY中联接两个表

如何链接两个表,但仅从PostgreSQL中的一个表获取MAX值?

通过各种属性比较两个大表-PostgreSQL

PostgreSQL:结合这两个查询

如何在Postgresql中将两个选择查询水平组合在同一张表上?

如何在PostgreSQL中联接两个表来更新一个

两个表之间的PostgreSQL滞后函数

PostgreSQL在每行上连接两个表

带有两个表和两个字段的两个日期之间的PostgreSQL日期差异

PostgreSQL合并表并过滤两个表

使用PostgreSQL合并两个表

PostgreSQL:如何使用两个日期之间的时间联接两个表?

PostgreSQL:如果两个值都不在表a中,则在表b中插入两个值

通过另一个表 postgresql 连接两个表中的数据

如何在 PostgreSQL 函数中组合两个选择?

两个表之间的 Postgresql 中的 Sumif

PostgreSQL 连接两个表的语法

PostgreSQL - 帮助将两个表与三个特定列进行比较

在两个 PostgreSQL 表之间创建关系

Postgresql - 复合键和两个表,还是只有 1 个宽表?

Postgresql如何对同一个表中的两个选择使用联合?

Postgresql 如何计算两个日期取决于另一个表

postgresql - 两个节点之间的距离

如何在 PostgreSQL 中连接两个表?

在 PostgreSQL 中连接具有两个条件或两个共同列的表