объединяя две таблицы и сумму Postgresql - PullRequest
1 голос
/ 26 апреля 2019

У меня есть база данных компании, которая занимается компенсационными выплатами. Мне пришлось. Я работаю над тремя столами: Выводы, m_email и m_lead. В таблице «заключение» я, среди прочего: идентификатор и дата создания. В таблице m_email у меня есть: id, сколько было открыто, сколько кликов. В таблице m_lead: id, output_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 ;

1 Ответ

0 голосов
/ 26 апреля 2019

Ваше требование немного трудно понять для меня. Но для первой части вашей проблемы я думаю, что могу ответить:

Исходная ситуация

У вас есть таблица 'application (id int primary key, метка времени создания_даты)'

Что вам нужно (для моего понимания)

Номер заявки на триместр (с января по март и т. Д.)

Возможное решение

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;

Я думаю, что вы можете адаптироваться к вашим потребностям.

Надеюсь, это поможет;)

...