161 auth_session_ls

This commit is contained in:
msyu
2025-08-25 17:56:44 +03:00
parent e7628f9100
commit 1b007c5e4a
3 changed files with 110 additions and 37 deletions
+13 -8
View File
@@ -38,7 +38,7 @@ select dayscale.dt
,sum (o.cost/dayscale.monthdays*o.probability_pc/100) as sum_unbounded ,sum (o.cost/dayscale.monthdays*o.probability_pc/100) as sum_unbounded
,sum( ,sum(
case when (o.dt_to > dayscale.dt /*OR o.dt_to IS NULL*/) then case when (o.dt_to > dayscale.dt /*OR o.dt_to IS NULL*/) then
o.cost*COALESCE( o.cost*o.quantity*(100 - coalesce(o.discount,0)/100)*COALESCE(
case when o.agreement_is_actual then 100 else o.probability_pc end case when o.agreement_is_actual then 100 else o.probability_pc end
,0)/100/dayscale.monthdays ,0)/100/dayscale.monthdays
else 0 else 0
@@ -46,23 +46,23 @@ select dayscale.dt
) as sum_working ) as sum_working
,sum( ,sum(
case when (o.dt_to_base > dayscale.dt /*OR o.dt_to_base IS NULL*/) then 0 else case when (o.dt_to_base > dayscale.dt /*OR o.dt_to_base IS NULL*/) then 0 else
o.cost_base*(100-COALESCE( o.cost_base*o.quantity_base*(100 - coalesce(o.discount_base,0)/100)*(100 - COALESCE(
case when o.agreement_is_actual then 100 else o.probability_pc end case when o.agreement_is_actual then 100 else o.probability_pc end
,0))/100/dayscale.monthdays ,0))/100/dayscale.monthdays
end end
) as sum_base ) as sum_base
,sum( ,sum(
COALESCE(case when (o.dt_to > dayscale.dt /*OR o.dt_to IS NULL*/) then COALESCE(case when (o.dt_to > dayscale.dt /*OR o.dt_to IS NULL*/) then
o.cost*( o.cost*o.quantity*(100 - coalesce(o.discount,0)/100)*(
case when o.agreement_is_actual then 100 else o.probability_pc end case when o.agreement_is_actual then 100 else o.probability_pc end
)/100/dayscale.monthdays )/100/dayscale.monthdays
else 0 /*по окончании рабочей сделки она дает нулевой вклад*/ else 0 /*по окончании рабочей сделки она дает нулевой вклад*/
end,0) end,0)
+ +
COALESCE(case when (o.dt_to_base > dayscale.dt /*OR o.dt_to_base IS NULL*/) then COALESCE(case when (o.dt_to_base > dayscale.dt /*OR o.dt_to_base IS NULL*/) then
o.cost_base/dayscale.monthdays*(100- COALESCE( o.cost_base*o.quantity_base*(100 - coalesce(o.discount_base,0)/100)*(100 - COALESCE(
case when o.agreement_is_actual then 100 else o.probability_pc end case when o.agreement_is_actual then 100 else o.probability_pc end
,0))/100 ,0))/100/dayscale.monthdays
else 0 /*по окончании базовой сделки она дает нулевой вклад*/ else 0 /*по окончании базовой сделки она дает нулевой вклад*/
end,0) end,0)
) as sum_bounded ) as sum_bounded
@@ -90,10 +90,14 @@ LEFT OUTER JOIN (
) b on (t.line_key=b.line_key AND b.siv_uid=( ) b on (t.line_key=b.line_key AND b.siv_uid=(
select siv_uid from select siv_uid from
( -- базовая лесенка ( -- базовая лесенка
SELECT siv_uid, dt_from, agreement_version, line_version, SELECT siv_uid, dt_from, agreement_version, line_version, line_key,
MIN(dt_from) OVER (PARTITION BY line_key ORDER BY agreement_version desc, line_version desc) running_min_dt MIN(dt_from) OVER (PARTITION BY line_key ORDER BY agreement_version desc, line_version desc) running_min_dt
FROM line where siv_is_actual AND probability_pc=100 AND agreement_is_actual AND deal_status_id in (5,6) FROM line ii
) i where i.dt_from=i.running_min_dt where ii.siv_is_actual AND ii.probability_pc=100 AND ii.agreement_is_actual AND ii.deal_status_id in (5,6)
AND ((t.agreement_version > ii.agreement_version) OR (t.agreement_version = ii.agreement_version AND t.line_version > ii.line_version))
) i
where i.dt_from=i.running_min_dt
AND t.line_key=i.line_key
AND i.dt_from < t.dt_from AND i.dt_from < t.dt_from
ORDER BY /*dt_from desc,*/ agreement_version desc, line_version desc ORDER BY /*dt_from desc,*/ agreement_version desc, line_version desc
LIMIT 1 LIMIT 1
@@ -101,6 +105,7 @@ LEFT OUTER JOIN (
) )
where t.dt_from=t.running_min_dt -- это условие формирования лесенки, далко оторвано от ее селекта where t.dt_from=t.running_min_dt -- это условие формирования лесенки, далко оторвано от ее селекта
AND t.line_pricing_model_id in (2,3) AND t.line_pricing_model_id in (2,3)
AND t.probability_pc > 0 -- несколько искусственное условие, убирающее несущественные и неуспешные сделки
--AND t.line_key in ('0a61f9e4-520b-4f0a-aac0-c1d42c037e1b','0bde160d-52d0-4d55-b4e0-05f45cfd739e','08485a0c-a913-4332-bb36-e40dec27d12e','1809bb31-4d37-424b-ba2b-26b9a873af7f') --AND t.line_key in ('0a61f9e4-520b-4f0a-aac0-c1d42c037e1b','0bde160d-52d0-4d55-b4e0-05f45cfd739e','08485a0c-a913-4332-bb36-e40dec27d12e','1809bb31-4d37-424b-ba2b-26b9a873af7f')
) o ) o
+27 -18
View File
@@ -8,7 +8,7 @@ begin_of_scale (dt) as (values ('2025-01-01'::timestamp)), -- этот изыс
end_of_scale (dt) as (values ('2025-12-02'::timestamp)), -- этот изыск не дает видимого ускорения end_of_scale (dt) as (values ('2025-12-02'::timestamp)), -- этот изыск не дает видимого ускорения
dayscale as ( dayscale as (
SELECT dd::date as dt SELECT dd::date as dt
, DATE_PART('days', DATE_TRUNC('month', dd) + '1 MONTH'::INTERVAL - '1 DAY'::INTERVAL) monthdays --, DATE_PART('days', DATE_TRUNC('month', dd) + '1 MONTH'::INTERVAL - '1 DAY'::INTERVAL) monthdays
FROM begin_of_scale, end_of_scale, generate_series( FROM begin_of_scale, end_of_scale, generate_series(
begin_of_scale.dt begin_of_scale.dt
,end_of_scale.dt ,end_of_scale.dt
@@ -20,7 +20,7 @@ line as (
,siv.version::numeric as line_version ,siv.version::numeric as line_version
, GREATEST(siv.date_nop,d."_plannedDueDate")::date as dt_from , GREATEST(siv.date_nop,d."_plannedDueDate")::date as dt_from
, COALESCE(siv.date_end::date, end_of_scale.dt) as dt_to , COALESCE(siv.date_end::date, end_of_scale.dt) as dt_to
,siv.is_easy as is_simple, siv.cnt as quantity -- maybe should mutliply ,siv.is_easy as is_simple, siv.cnt as quantity
, a.__id as agreement_uid, a.contract_uid, a.is_actual as agreement_is_actual, a.deal_uid, a.index as agreement_version , a.__id as agreement_uid, a.contract_uid, a.is_actual as agreement_is_actual, a.deal_uid, a.index as agreement_version
,d."_plannedDueDate", d.__name as deal, d.__status_status as deal_status_id ,d."_plannedDueDate", d.__name as deal, d.__status_status as deal_status_id
,c.__name as contract ,c.__name as contract
@@ -32,40 +32,49 @@ line as (
join elma.aggrements c on (a.contract_uid = c.__id) join elma.aggrements c on (a.contract_uid = c.__id)
join status s on (d.__status_status=s.status_id) join status s on (d.__status_status=s.status_id)
cross join end_of_scale cross join end_of_scale
where siv.is_actual
) )
-- 20241011T092847849 очень странный пример строки ... может быть, вообще не нужно для инсталлов брать базовую сделку, а просто рабочую с ее вероятностью
-- 20241011T091519330 чрезвычайно странная строка. Похоже, для инсталла сплошь и рядом меняют артикул ... но при этом прошлый инсталл будет пропадать из истории? если его игнорировать
select dayscale.dt select dayscale.dt
,sum (o.cost/dayscale.monthdays*o.probability_pc/100) as sum_unbounded ,(o.cost_base*(100-COALESCE(o.probability_pc,0))) as sum_base
,(
COALESCE((o.cost*o.quantity*(100-coalesce(o.discount,0))/100*o.probability_pc/100),0)
+
COALESCE((o.cost_base*o.quantity_base*(100-coalesce(o.discount_base,0))/100*(100-COALESCE(o.probability_pc,0))/100),0)
) as sum_bounded
, cost, quantity, discount, cost_base, quantity_base, discount_base, *
/*
,sum (o.cost*o.probability_pc/100) as sum_unbounded
,sum(o.cost*o.probability_pc/100) as sum_working ,sum(o.cost*o.probability_pc/100) as sum_working
,sum(o.cost_base*(100-COALESCE(o.probability_pc,0))) as sum_base ,sum(o.cost_base*(100-COALESCE(o.probability_pc,0))) as sum_base
,( ,(
COALESCE(sum(o.cost*o.probability_pc/100),0) COALESCE(sum(o.cost*o.quantity*(100-coalesce(o.discount,0))/100*o.probability_pc/100),0)
+ +
COALESCE(sum(o.cost_base*(100-COALESCE(o.probability_pc,0))),0) COALESCE(sum(o.cost_base*o.quantity_base*(100-coalesce(o.discount_base,0))/100*(100-COALESCE(o.probability_pc,0))),0)
) as sum_bounded ) as sum_bounded
,max(o.dt_from) as max_dt_from, max(o.dt_from_base) as max_dt_from_base, max(o.probability_pc) as max_probability_pc ,max(o.dt_from) as max_dt_from, max(o.dt_from_base) as max_dt_from_base, max(o.probability_pc) as max_probability_pc
,count(*) ,count(*)
*/
from dayscale from dayscale
LEFT OUTER JOIN ( LEFT OUTER JOIN (
SELECT t.*, SELECT t.*,
b.discount as discount_base, b.quantity as quantity_base, b.dt_from as dt_from_base, b.dt_to as dt_to_base, b.cost as cost_base b.discount as discount_base, b.quantity as quantity_base, b.dt_from as dt_from_base, b.dt_to as dt_to_base, b.cost as cost_base
FROM line t FROM line t
LEFT OUTER JOIN line b on (t.line_key=b.line_key AND t.line_pricing_model_id=b.line_pricing_model_id AND b.siv_uid=(
LEFT OUTER JOIN line b on (t.line_key=b.line_key AND b.siv_uid=(
select siv_uid select siv_uid
FROM line where siv_is_actual AND probability_pc=100 AND agreement_is_actual AND deal_status_id in (5,6) FROM line i
AND ((t.agreement_version > b.agreement_version) OR (t.agreement_version = b.agreement_version AND t.line_version > b.line_version)) where t.line_key=i.line_key AND t.line_pricing_model_id=i.line_pricing_model_id AND i.siv_is_actual AND i.probability_pc=100 AND i.agreement_is_actual AND i.deal_status_id in (5,6)
ORDER BY agreement_version desc, line_version desc AND ((t.agreement_version > i.agreement_version) OR (t.agreement_version = i.agreement_version AND t.line_version > i.line_version))
ORDER BY i.agreement_version desc, i.line_version desc
LIMIT 1 LIMIT 1
) )
) )
where 1=1 where 1=1
AND t.line_pricing_model_id in (1) AND t.line_pricing_model_id in (1)
AND t.probability_pc > 0 -- несколько искусственное условие, убирающее несущественные и неуспешные сделки
--AND t.line_key in ('0a61f9e4-520b-4f0a-aac0-c1d42c037e1b','0bde160d-52d0-4d55-b4e0-05f45cfd739e','08485a0c-a913-4332-bb36-e40dec27d12e','1809bb31-4d37-424b-ba2b-26b9a873af7f')
) o ) o
on (o.dt_from = dayscale.dt) on (dayscale.dt=o.dt_from)
group by dayscale.dt --where o.contract_uid='01927a86-23c9-729b-a416-a93b71e0c91a'
order by dayscale.dt --group by dayscale.dt
order by dayscale.dt, agreement_uid
+59
View File
@@ -0,0 +1,59 @@
select
c.__name as contract, d.__name as deal, d.__status_status
,siv.hash ,siv.type, siv.discount, siv.user_description, siv.cnt, siv.date_nop, siv.date_end, siv.is_easy, siv.deal, siv.is_actual
,p.type as type_p, p.code, p.name, p.price, p.cnt, p.discount as discount_p, p.ed_izm, p.deal as deal_p
--siv.*
from elma.deals_services siv
join elma.service_parametrs p on (p.__id = ANY (siv.params))
join elma.deals d on (d.__id = ANY(siv.deal))
join elma.additional_agreements a on (a.__id = ANY(siv.additional_agreement))
join elma.aggrements c on (a.contract_uid = c.__id)
where siv.hash='20241011T091519330'
AND siv.is_actual -- убираем черновики расчетов
-- 20241101T110547177 пример странного оформления, лишние типоразмеры в параметрах
-- 20241011T092847849 странным образом меняется строка, цена остается
-- 20241011T091519330 - меняется даже тип 1-2 договор 2206202301
/*
select
siv.discount, siv.user_description, siv.cnt, siv.date_nop, siv.date_end, siv.is_easy, siv.deal, siv.is_actual, siv.hash
,p.type, p.code, p.name, p.price, p.cnt, p.discount as discount_p, p.ed_izm, p.deal as deal_p
--siv.*
from elma.deals_services siv
join elma.service_parametrs p on (p.__id = ANY (siv.params))
where p.cnt <> siv.cnt
*/
select hash from elma.deals_services
where is_actual
group by hash
having count(distinct type) >1
select
c.__name as contract, d.__name as deal, d.__status_status
,a.__name as additional_agreement
,siv.hash ,siv.type, siv.discount, siv.user_description, siv.cnt, siv.date_nop, siv.date_end, siv.is_easy, siv.deal, siv.is_actual
from elma.deals_services siv
join elma.deals d on (d.__id = ANY(siv.deal))
join elma.additional_agreements a on (a.__id = ANY(siv.additional_agreement))
join elma.aggrements c on (a.contract_uid = c.__id)
where siv.is_actual -- убираем черновики расчетов
AND a.is_actual -- оставляем только актуальные доп.соглашения
AND siv.hash in (select hash
from elma.deals_services siv
join elma.additional_agreements a on (a.__id = ANY(siv.additional_agreement))
where siv.is_actual AND a.is_actual
group by hash
having count(distinct type) >1
)
order by hash
select * from elma.deals_services siv where hash='20240424T170743034' AND is_actual
select * from elma.deals_services siv where hash='20241011T091519330' AND is_actual
select c.__name as contract, a.__name as additional_agreement, deal, hash, count(*)
from elma.deals_services siv
join elma.additional_agreements a on (a.__id = ANY(siv.additional_agreement))
join elma.aggrements c on (a.contract_uid = c.__id)
where siv.is_actual
--AND a.is_actual
group by siv.deal, c.__name, a.__name, siv.hash, siv.type having count(*)>1