我有一家处理赔偿金的公司的数据库。我必须。我正在处理三个表: 结论,m_email和m_lead。在“结论”表中,除其他外,我有:id和创建日期。在m_email表中,我有:id,打开了多少,单击了多少。在m_lead表中:id,结论ID,creation_date。 我需要检查以下内容: 1.将年份分为四个部分,检查一月至四月,四月至七月等的申请数量。 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 ;
答案 0 :(得分:0)
您的需求对我来说有点难以理解。但是对于您问题的第一部分,我想我可以回答:
初始情况
您有一个表'applications(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;
我认为您可以从那里开始适应自己的需求。
希望这会有所帮助;)