Lädt...

🔧 Align the existing data to the corresponding position and fill in any missing data with 0 — From SQL to SPL #6


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Problem description & analysis:

The MySQL database has a sampling table, where each ITEM and CITY is a sampling task. Each sampling task includes 1 to 5 records, indicating that data was collected within 5 weeks. START_Y and START_W are the year and week when sampling started, while FIRST_USE_Y and FIRST_USE_W are the year and week when data was actually collected. The calculation rule for week numbers: Starting from January 1st, every 7 days counts as a week and accumulates sequentially.

source table

Task: Now we need to expand each sampling task into 5 records (weeks), increasing sequentially from the year and week of sampling, including the weeks where data was actually collected and those where data was not collected. The former should be aligned to the corresponding position, while the latter has a VALUE of 0.

expected results

Code comparisons:

SQL

WITH ItemCity As (
    SELECT Item, City, MIN(  DATEADD(day, Start_W*7, DATEFROMPARTS(Start_Y, 1, 1)) ) As StartWeek
    FROM Data
    GROUP BY Item, City
), 
ItemCityWeeks As (
   SELECT Item,City, StartWeek
       ,Year(StartWeek) As Start_Y,datepart(week, StartWeek)-1 As Start_W
       ,YEAR(DATEADD(day, Weeks.num*7, StartWeek)) As First_Use_Y
       ,DATEPART(dayofyear, DATEADD(day, Weeks.num*7, StartWeek))/7 As First_Use_W
   FROM ItemCity
   CROSS JOIN ( VALUES (0), (1), (2), (3), (4)) Weeks(num)
)
SELECT icw.Item, icw.City
      , icw.Start_Y, icw.Start_W, icw.First_Use_Y, icw.First_Use_W
      , coalesce(d.value, 0) as Value
FROM ItemCityWeeks icw
LEFT JOIN Data d ON d.Item = icw.Item AND d.City = icw.City 
      and d.First_Use_Y = icw.First_Use_Y and d.First_Use_W = icw.First_Use_W
ORDER BY Item, City DESC

After SQL grouping, it must aggregate immediately. It cannot keep the grouped subsets and expand each subset into N records, and then simply align the VALUE using filtering methods. It can only solve it by taking a detour: First group and aggregate and then expand, and cross multiplication should be used for expansion. When aligning VALUE, the indirect implementation of multi field join is necessary, and the structure is very complex and the code is also verbose.

SPL:

SPL code is much simpler and easier to understand: try.DEMO

SPL code script

A1: Load data.

A2: Group by task, and it can retain the grouped subsets without aggregation.

A3: Expand each group of data directly into 5 records, calculate the week number according to the rules, and simply filter out the VALUE corresponding to the current week number. Function pdate@y returns the first day of the year in which the date is located, ifn returns the first non-null member, select@1 filters out the first record that meets the criteria.

Try esProc SPL today and streamline your workflow👉🏻: Open-Source Address

...

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


📈 45.6 Punkte
🔧 Programmierung

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


📈 45.6 Punkte
🔧 Programmierung

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


📈 34.72 Punkte
🔧 Programmierung

🎥 Jira Align Product Overview | Jira Align Product Demos | Atlassian


📈 34.07 Punkte
🎥 Video | Youtube

🔧 How to Position and Align Elements Using CSS


📈 31.15 Punkte
🔧 Programmierung

🔧 How to monitor events in data dimensions and obtain corresponding data?


📈 28.51 Punkte
🔧 Programmierung

🔧 #66 — Search for The Position of A Certain Value And Take Values by Position


📈 27.09 Punkte
🔧 Programmierung

🔧 How do I connect my existing Visual Studio 2022 project to an existing github repository?


📈 26.45 Punkte
🔧 Programmierung

📰 Data is the missing piece of the AI puzzle. Here's how to fill the gap


📈 25.66 Punkte
📰 IT Nachrichten

🔧 KISS Principle: Giữ Mọi Thứ Đơn Giản Nhất Có Thể


📈 25.46 Punkte
🔧 Programmierung

🔧 Có thể bạn chưa biết (Phần 1)


📈 25.46 Punkte
🔧 Programmierung

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


📈 25.46 Punkte
🔧 Programmierung

🕵️ Kèo Thẻ Phạt Vip66 Là Gì? 3 Lối Đánh Kèo Chậm Mà Chắc


📈 25.46 Punkte
🕵️ Reverse Engineering

🔧 In Excel, Align Every Two Columns to Present Them While Setting Missing Values as 0


📈 25.21 Punkte
🔧 Programmierung

🔧 Survey: C to Rust conversion and corresponding tools


📈 25.05 Punkte
🔧 Programmierung

🔧 Patterns that indicate DOM dynamic behavior and corresponding React code.


📈 25.05 Punkte
🔧 Programmierung

🔧 SOA vs Microservices – 8 key differences and corresponding use cases


📈 25.05 Punkte
🔧 Programmierung

📰 Ordr IoT Discovery Program uncovers shadow IoT devices and their corresponding risks


📈 25.05 Punkte
📰 IT Security Nachrichten

🪟 Adobe Premier Pro gets AI, can fill in missing scene elements in video


📈 23.93 Punkte
🪟 Windows Tipps

🔧 How to Calculate Median on Values That Have Corresponding Quantities


📈 23.92 Punkte
🔧 Programmierung

🎥 HPR4079: The Corresponding Source


📈 23.92 Punkte
🎥 Podcasts

🍏 What Formula Can Show the Corresponding Values From ONE Column Into Another?


📈 23.92 Punkte
🍏 iOS / Mac OS