Hi! Could we please enable some services and cookies to improve your experience and our website?

SQLize | PHPize | SQLtest

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

A A A
Login    Share code      Blog   FAQ
Copy Format Clear
with balance_with_prev as ( select bal_date::date , code , coalesce(value,0) as current_balance_value , lag( coalesce(value,0), 1) over(partition by code order by bal_date asc) as prev_balance_value from benefit_balances ) , daily_points_transactions as ( select code , created_at::date as trans_date , coalesce(sum(case when direction=1 then cust_sum else 0 end),0) as points_add , coalesce(sum(case when direction=0 then cust_sum else 0 end),0) as points_spent from benefit_points group by 1,2 ) Select bwp.bal_date , bwp.code , bwp.current_balance_value , bwp.prev_balance_value , dpt.points_add , dpt.points_spent , case when bwp.prev_balance_value is null and bwp.current_balance_value = 0 then true --пСрвая запись when bwp.prev_balance_value is null and bwp.current_balance_value != 0 then false -- ошибка ΠΏΠ΅Ρ€Π²ΠΎΠΉ записи, Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€ Π½Π΅ почистили тСстовыС Π΄Π°Π½Π½Ρ‹Π΅ when bwp.current_balance_value=bwp.prev_balance_value+dpt.points_add-dpt.points_spent then true else false end as is_match from balance_with_prev bwp left join daily_points_transactions dpt on dpt.code=bwp.code AND dpt.trans_date = DATEADD('day', -1, bwp.bal_date) -- where bwp.prev_balance_value is not null and bwp.current_balance_value != 0 -- Π² зависимости ΠΎΡ‚ Ρ‚Ρ€Π΅Π±ΠΎΠ²Π°Π½ΠΈΠΉ ΠΊ ΠΈΡ‚ΠΎΠ³ΠΎΠ²ΠΎΠΉ Ρ‚Π°Π±Π»ΠΈΡ†Π΅, ΠΌΠΎΠΆΠ΅ΠΌ Ρ…ΠΎΡ‚Π΅Ρ‚ΡŒ ΠΈΠ·Π±Π°Π²ΠΈΡ‚ΡŒΡΡ, ΠΎΡ‚ Π½ΡƒΠ»Π΅Π²Ρ‹Ρ… записСй ΠΈ Π½Π΅ ΠΏΠ΅Ρ€Π΅ΡΡ‡ΠΈΡ‚Ρ‹Π²Π°Ρ‚ΡŒ ΠΈΡ… order by 2,1

Stuck with a problem? Got Error? Ask AI support!

Copy Clear