From 5cd365a1135c912fab07c334136b1246049c72d2 Mon Sep 17 00:00:00 2001 From: Basyrov Rustam Date: Wed, 18 Dec 2024 21:49:56 +0300 Subject: [PATCH] finish?! --- egeland/sql/Script.sql | 96 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 85 insertions(+), 11 deletions(-) diff --git a/egeland/sql/Script.sql b/egeland/sql/Script.sql index 8a14c26..41c4a55 100644 --- a/egeland/sql/Script.sql +++ b/egeland/sql/Script.sql @@ -90,10 +90,34 @@ select from chat_messages cm where - entity_id = 37529075 + entity_id = 37548545 order by created_at offset 1; +with tmp as +(select + created_by, + created_at, + created_by = lag(created_by) over(order by created_at) as test +from + chat_messages cm +where + entity_id = 37548545 +order by created_at) +select + * +from tmp +where tmp.test = false or tmp.test is null + + +select + entity_id, + created_at, + created_by, + created_by = lag(created_by) over(partition by entity_id order by created_at) as tmp +from + chat_messages cm +order by created_at -- result with tmp as @@ -117,6 +141,18 @@ group by tmp.created_by +select + created_by, + extract(day from cast(to_timestamp(created_at) as date)) as time_casted, + avg(cast(to_timestamp(created_at) as time)) over() as test +from + chat_messages cm + + +select cast(to_timestamp(1729931470) as time) + +select greatest(cast(to_timestamp(1729931470) as time), + '12:00:00') as test @@ -124,14 +160,52 @@ group by tmp.created_by - - - - - - - - - - +with tmp_table2 as +(with tmp_table as +(select + *, + cast(to_timestamp(created_at) as time) as dtime, + coalesce(created_by <> lag(created_by) over(partition by entity_id order by created_at), true) as not_dupl, + row_number() over(partition by entity_id order by created_at) as num +from + chat_messages cm +order by entity_id, created_at) +select + *, + dtime, + case when (dtime < '09:30:00'::time and created_by <> 0) + then '24:00:00'::time - lag(dtime) over(partition by entity_id order by created_at) + when (dtime < '09:30:00'::time and created_by = 0) + then dtime -'09:30:00'::time + when (lag(dtime) over(partition by entity_id order by created_at) > dtime) + then greatest(dtime - lag(dtime) over(partition by entity_id order by created_at), + lag(dtime) over(partition by entity_id order by created_at) - dtime) + else dtime - lag(dtime) over(partition by entity_id order by created_at) + end as diff_time +from tmp_table +where tmp_table.not_dupl + and + not (tmp_table.num = 1 and tmp_table.created_by <> 0)) +select + r.rop_name, + m.name_mop, + cast(to_timestamp(created_at) as timestamp) as datetime, + diff_time +from + tmp_table2 as tt2 + join + managers as m on m.mop_id = tt2.created_by + join + rops as r on r.rop_id = m.rop_id::int4 +where + created_by <> 0 + +select abs('09:35:00'::time - '09:36:00'::time) + + + + + + + \ No newline at end of file