Lädt...

🔧 Track Production Operations Outcome Progression with Conditional NULLs :SQL VS SPL


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

A SQL Server database table describes the execution progress of multiple operations in a production process. When the Outcome of an OP is 0, it indicates execution failure and should continue until the Outcome is 1. This indicates successful execution and the next OP should be executed in sequence.

Image description
Now we need to track the execution progress of operations: each operation occupies a field, and the field order represents the execution progress of the operations; When an operation fails to execute, it should enter the next tracking stage, which is to generate a new record; Copy the progress 1 (if any) of the previous successful operations before continuing with this operation; If this operation is successful this time, the next operation will continue in the current tracking phase.

Image description

SQL

  SELECT  CASE WHEN max(OP) > 1 THEN 1 ELSE MAX(CASE WHEN OP = 1 THEN Outcome END) END AS OP1
,   CASE WHEN max(OP) > 2 THEN 1 ELSE MAX(CASE WHEN OP = 2 THEN Outcome END) END AS OP2
,   CASE WHEN max(OP) > 3 THEN 1 ELSE MAX(CASE WHEN OP = 3 THEN Outcome END) END AS OP3
,   CASE WHEN max(OP) > 4 THEN 1 ELSE MAX(CASE WHEN OP = 4 THEN Outcome END) END AS OP4
,   CASE WHEN max(OP) > 5 THEN 1 ELSE MAX(CASE WHEN OP = 5 THEN Outcome END) END AS OP5
FROM    (
    SELECT  *
    ,   sum(flag) OVER(ORDER BY OP, Outcome rows BETWEEN unbounded preceding AND CURRENT row) AS counter

    FROM    (
        SELECT  *
        ,   CASE WHEN (lag(outcome) OVER(ORDER BY op,outcome) = outcome AND outcome = 1) OR (lag(outcome) OVER(ORDER BY op,outcome) <> outcome AND outcome = 0)
                THEN 0
                ELSE 1
            END AS flag
        FROM    Operations
        ) x
    ) x
GROUP BY x.counter

SQL does not have a mechanism for orderly grouping based on conditions, and instead divides the original records into multiple tracking stages, requiring the use of nested subqueries and multiple window functions for marking. Ordinary SQL also lacks the syntax for dynamically generating fields, and can only rigidly write a case when for each field; Dynamically generating fields requires the use of dynamic SQL.

SPL code is simple and easy to understand. try.DEMO

Image description

A1: Load data.

A2: Dynamically generate a two-dimensional table.

A3: Grouping by conditions, when the last execution result failed, a new group was formed, where [-1] represents the previous one. Note that there is no immediate aggregation here.

A4: Loop the data of each group and add one record to the two-dimensional table at a time according to the rules. Fill in some 1s at the beginning and the sequence of the Outcome of this group at the end.

SPL is open source free, open source address
Free download

...

🔧 Track Production Operations Outcome Progression with Conditional NULLs :SQL VS SPL


📈 125.66 Punkte
🔧 Programmierung

🔧 Track Production Operations Outcome Progression with Conditional NULLs - From SQL to SPL #2


📈 125.66 Punkte
🔧 Programmierung

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


📈 35.21 Punkte
🔧 Programmierung

📰 Conditional Variational Autoencoders with Learnable Conditional Embeddings


📈 29.66 Punkte
🔧 AI Nachrichten

🔧 How do SQL interprets nulls?


📈 28.06 Punkte
🔧 Programmierung

🔧 Handling Nulls in C# A Comprehensive Guide


📈 24.87 Punkte
🔧 Programmierung

🔧 Fix Nulls in 2 Mins: Cleaner JSON Data with Jolt Code


📈 24.87 Punkte
🔧 Programmierung

🔧 CodeSOD: Terminated Nulls


📈 24.87 Punkte
🔧 Programmierung

🔧 The most painful reason NULLs are evil


📈 24.87 Punkte
🔧 Programmierung

🔧 Handling NULLs in PostgreSQL: Alternatives to ISNULL


📈 24.87 Punkte
🔧 Programmierung

🔧 CodeSOD: Accessed Nulls


📈 24.87 Punkte
🔧 Programmierung

🔧 CodeSOD: Terminated By Nulls


📈 24.87 Punkte
🔧 Programmierung

🔧 Error'd: (Almost) Nought but Nulls


📈 24.87 Punkte
🔧 Programmierung

🔧 NULLs Are Not The Same – A Guide


📈 24.87 Punkte
🔧 Programmierung

🔧 Optional in Java: A Swiss Army Knife for Handling Nulls and Improving Code Quality


📈 24.87 Punkte
🔧 Programmierung

🔧 CodeSOD: Terning Nulls into Values


📈 24.87 Punkte
🔧 Programmierung

🔧 CodeSOD: Comments, Documentation, and Nulls


📈 24.87 Punkte
🔧 Programmierung

🔧 PostgreSQL: Are All NULLs the Same?


📈 24.87 Punkte
🔧 Programmierung

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


📈 20.79 Punkte
🔧 Programmierung

🔧 Convert cross cell to row header, row header to column:From SQL to SPL


📈 20.79 Punkte
🔧 Programmierung

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


📈 20.79 Punkte
🔧 Programmierung

🔧 Convert cross cell to row header, row header to column:From SQL to SPL


📈 20.79 Punkte
🔧 Programmierung

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


📈 20.79 Punkte
🔧 Programmierung

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


📈 20.79 Punkte
🔧 Programmierung

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


📈 20.79 Punkte
🔧 Programmierung

🔧 Why Developers Are Ditching SQL Pain Points with Open-Source SPL


📈 20.79 Punkte
🔧 Programmierung

🔧 Find the superset from the relationship table:SQL VS SPL#13


📈 20.79 Punkte
🔧 Programmierung

🔧 Flexible interval aggregation:From SQL to SPL


📈 20.79 Punkte
🔧 Programmierung

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


📈 20.79 Punkte
🔧 Programmierung

🔧 Data Analysis Showdown: Comparing SQL, Python, and esProc SPL


📈 20.79 Punkte
🔧 Programmierung

🔧 How to execute SQL on CSV files with esProc SPL


📈 20.79 Punkte
🔧 Programmierung

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


📈 20.79 Punkte
🔧 Programmierung

🔧 Uncertain number but regular column to row conversion:From SQL to SPL#5


📈 20.79 Punkte
🔧 Programmierung

matomo