想从文件创建之日起36个月内以间隔的间隔获得付款总和 - Postgres
我有两个桌子。
- 在第一个表“客户”中,我有3个Colums,第一列是“ ID”(每个客户唯一),在第二列中为“ date_created”(创建客户文件的日期)。从2007年到日期,日期的格式为“ yyyy-mm-dd”。第三列是“ client_name”(客户所属的客户端的名称)
- 第二个表是带有3个Colums的“付款”表,“ ID”(每个客户唯一),第二列是“付款金额”,第三列是“ date_payment”。
我想实现的是接下来。从第一表中,我想选择在日期范围内创建的文件(例如,从2018-06-01到2018年12月31日),并在创建一个月后从第二个表中获得付款总和。每个特定主张都附加了间隔,因此每个单独的主张都不同。这样做的目的是从创建月获得成功率,从创建到第36个月2个月...
结果是:
interval client_name sum
1 Boden 100
2 Boden 220
etc... till 36
我在下一个查询中获得的近距离结果非常耗时,所以我需要一些更快的解决方案将为每个索赔返回36个月间隔的结果。
select customers.client_name, sum(payments.amount) as sum_amount
from customers
left join payments on customers.id=payments.id
where customers.date_created >= '2018-06-01' and customersdate_created <= '2018-12-31'
and date_payment <= date(date_created + interval '1 month)
and client_name ilike '%Boden%'
group by customers.client_name
有人可以帮忙吗?
更新
没有人回答,但我自己想出了。下面的解决方案:
select customers.client_name, sum(payments.amount) as sum_amount, 1 as sortorder
from customers
left join payments on customers.id=payments.id
where customers.date_created >= '2018-06-01' and customersdate_created <= '2018-12-31'
and date_payment <= date(date_created + interval '1 month)
and client_name ilike '%Boden%'
group by customers.client_name
UNION
select customers.client_name, sum(payments.amount) as sum_amount, 2 as sortorder
from customers
left join payments on customers.id=payments.id
where customers.date_created >= '2018-06-01' and customersdate_created <= '2018-12-31'
and date_payment <= date(date_created + interval '2 month)
and client_name ilike '%Boden%'
group by customers.client_name
I have two tables.
- In first table "customers" I have 3 colums, first column is "id" (unique for every customer) and in second column is "date_created" (the date when the customer file was created). Dates are ranging in format 'yyyy-mm-dd' from 2007 till date. The third column is "client_name" (the name of client to which customer belongs)
- the second table is "payments" table with 3 colums, "id" (unique for every customer), in second column is "amount" of payment and in third column is "date_payment".
What I would like to achieve is next. From the first table I would like to choose files, created within a date range (for example from 2018-06-01 till 31.12.2018) and get sum of payment from second table after one month of creation, etc.. till month 36. Interval is attached for every particular claim, so it is different for every separate claim. The purpose of this is to get success rates from on month of creation, 2 month from creation, till month 36...
The result would be:
interval client_name sum
1 Boden 100
2 Boden 220
etc... till 36
Close results i get with next query but it is very time consuming, so I would need some quicker solution that would return results for 36 month intervals for every each claim.
select customers.client_name, sum(payments.amount) as sum_amount
from customers
left join payments on customers.id=payments.id
where customers.date_created >= '2018-06-01' and customersdate_created <= '2018-12-31'
and date_payment <= date(date_created + interval '1 month)
and client_name ilike '%Boden%'
group by customers.client_name
Can anyone help, please?
UPDATE
Nobody answered but I kind of figure it out myself. Solution below:
select customers.client_name, sum(payments.amount) as sum_amount, 1 as sortorder
from customers
left join payments on customers.id=payments.id
where customers.date_created >= '2018-06-01' and customersdate_created <= '2018-12-31'
and date_payment <= date(date_created + interval '1 month)
and client_name ilike '%Boden%'
group by customers.client_name
UNION
select customers.client_name, sum(payments.amount) as sum_amount, 2 as sortorder
from customers
left join payments on customers.id=payments.id
where customers.date_created >= '2018-06-01' and customersdate_created <= '2018-12-31'
and date_payment <= date(date_created + interval '2 month)
and client_name ilike '%Boden%'
group by customers.client_name
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论