Lädt...

🔧 Calculate based on the records within the group and fill the result into the first record:From SQL to SPL #4


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

The id and nbr of the MS SQL database table are grouping fields. After sorting the records within the group by time field dt, the order of 0 and 1 in the status field 'status' is different.

Image description
Now we need to generate three calculated columns according to certain rules based on the records within the group, and fill them into the first record of each group, while filling in 0 for all other records.

Calculated column Cont1Sta1: When there is at least one record in this group with status=1, assign a value of 1, otherwise fill in 0.

Calculated column DateLagInDays: When there is at least one record in this group with status=1, and there is at least one record with status=0, and the first record of the latter is earlier than the first record of the former, assign a value of the day interval between the two, otherwise fill in 0.

Calculated column Recurrence: When there is at least one record with status=1 in this group, and there is at least one record with status=0, and the first record of the former is earlier than the last record of the latter, assign a value of 1, otherwise fill in 0.

Image description
SQL solution:

with cte as (
    select id, nbr, dt, status
        , row_number() over (partition by id, nbr order by id, nbr, dt asc, status desc) rn
        , max(status) over (partition by id, nbr) partition_status
        , min(case when Status = 1 then dt else null end) over (partition by id, nbr) partition_dt1
        , max(case when Status = 1 then dt else null end) over (partition by id, nbr) partition_dt2
    from tb
)
select id, nbr, dt, status
    , case when rn = 1 then partition_status else 0 end Cont1Sta1
    , case when rn = 1 then datediff(day, dt, coalesce(partition_dt1, dt)) else 0 end DateLagInDays
    , case when rn = 1 and exists (select 1 from cte c2 where c2.id = c1.id and c2.nbr = c1.nbr and c2.dt > c1.partition_dt2) then 1 else 0 end Recurrence
from cte c1
order by id, nbr, dt asc, status desc;

After SQL grouping, it must aggregate immediately, and subsets cannot be kept for more complex multi-step calculations. In this case, multiple window functions can only be used for repeated calculations, which is cumbersome in code. SQL does not have natural sequence numbers, so we need to use window functions to generate sequence numbers first. The expression of the last item is not convenient either, and here we need to compare by associating dates.

SPL can retain and reuse grouped subsets, with natural sequence numbers, and can directly reference the first or last one.

Image description
A1: Load data through JDBC, with 3 calculated columns default to 0 and sorted by date.

A2: Group but not aggregate.

A3: Calculate the data of each group: calculate the record position p1F for the first status=1, the record position p0F for the first status=0, and the record position p0L for the last status=0; Calculate three calculated columns in turn according to business logic.

The pselect function returns the position of the record that meets the criteria, and defaults to returning the first one, @a represents returning all positions. The function m can retrieve values by position, where -1 represents the last one.

SPL is open source and free, welcome to try it, it will bring you different surprises.

Open source address

Free download

...

🔧 Calculate a Pair of Minimum Values that Meet the Criteria within the Group — From SQL to SPL #12


📈 56.25 Punkte
🔧 Programmierung

🔧 Calculate Monthly Account Balance and Fill in Missing Dates — From SQL to SPL #10


📈 56.2 Punkte
🔧 Programmierung

🔧 Calculate monthly account balance and fill in missing dates:From SQL to SPL


📈 56.2 Punkte
🔧 Programmierung

🔧 Pair and Transpose Adjacent Records within the Group - From SQL to SPL #13


📈 50.72 Punkte
🔧 Programmierung

🔧 Search for the closest matching record within the group:From SQL to SPL


📈 48.62 Punkte
🔧 Programmierung

🔧 Number Non-Null Values in Order within the Group — From SQL to SPL #17


📈 39.52 Punkte
🔧 Programmierung

🔧 Calculate the Hierarchy of Recursive References — From SQL to SPL #11


📈 38.24 Punkte
🔧 Programmierung

🔧 Calculate the hierarchy of recursive references :From SQL to SPL


📈 38.24 Punkte
🔧 Programmierung

🔧 Calculate all the year/months included in the year/month interval — From SQL to SPL #15


📈 38.24 Punkte
🔧 Programmierung

🔧 Add records that meet the criteria before each group after grouping :From SQL to SPL


📈 38.2 Punkte
🔧 Programmierung

🔧 Issue with spl-associated-token-account Version Conflict in anchor-spl v0.30.1


📈 35.89 Punkte
🔧 Programmierung

🔧 Do ordered grouping and aggregation within groups:SQL VS SPL #11


📈 34.05 Punkte
🔧 Programmierung

🔧 Do Ordered Grouping and Aggregation within Groups — From SQL to SPL #1


📈 34.05 Punkte
🔧 Programmierung

🔧 Get the Records after and before the Searched One — From SQL to SPL #18


📈 32.72 Punkte
🔧 Programmierung

🔧 Get the records after and before the searched one:SQL VS SPL #12


📈 32.72 Punkte
🔧 Programmierung

🔧 Getting Values from Multiple Format Strings to Multiple Records — From SQL to SPL #16


📈 31.36 Punkte
🔧 Programmierung

🔧 Find the Closest Date Match for Each Record from Two Tables — From SQL to SPL #20


📈 30.62 Punkte
🔧 Programmierung

🔧 Find the closest date match for each record from two tables:SQL VS SPL #14


📈 30.62 Punkte
🔧 Programmierung

🔧 Released Result::Simple - a dead simple perl-ish Result like F#, Rust, Go


📈 29.36 Punkte
🔧 Programmierung

🕵️ FetLife: Able to see highest poll result without voting or view result


📈 29.36 Punkte
🕵️ Sicherheitslücken

🔧 #78 — Fill Aggregation Value in The First Row of The Same Group of Data


📈 28.74 Punkte
🔧 Programmierung

🔧 Calculate prime number table within 10000 using screening method


📈 27.89 Punkte
🔧 Programmierung

🔧 Summarize Row Counts by Dynamic Columns into JSON — From SQL to SPL #22


📈 26.93 Punkte
🔧 Programmierung

🔧 Generate Calculated Columns Based on Continuous Values — From SQL to SPL #29


📈 26.19 Punkte
🔧 Programmierung

🔧 #74 - Filter by Maximum Or Minimum Value within A Group (Find out One for Each Group)


📈 24.84 Punkte
🔧 Programmierung

📰 Report: Facebook's Privacy Lapses May Result in Record Fine


📈 23.78 Punkte
📰 IT Security Nachrichten

🔧 Grok 3: AI Thông Minh Nhất Thế Giới


📈 23.59 Punkte
🔧 Programmierung