4.7 Temp dimension table and segmented dimension table
SQL
SELECT a.CustomerID,a.OrderDate,a.ProductID,a.Quantity,a.Unit,
a.Price,a.Amount,a.EmployeeID,a.EmployeeName,a.ShipVia
FROM Orders a
JOIN (
SELECT ProductID,0.01*sum(Quantity) Quantity
FROM Orders
WHERE OrderDate>='2021-01-01' and OrderDate<='2021-12-31'
GROUP BY ProductID) b
ON a.ProductID=b.ProductID
WHERE a.OrderDate>='2021-01-01' and a.OrderDate<='2021-12-31' and a.Quantity>b.Quantity
SPL
A | |
---|---|
1 | >st=date(“2021-01-01”), et=date(“2021-12-31”), start=days@o(st), end=days@o(et) |
2 | =file(“Orders_Time.ctx”).open().cursor@mx(Quantity,ProductID;OrderDate>=start && OrderDate<=end) |
3 | =A2.groups(ProductID;0.01*sum(Quantity):Quantity).keys@i(ProductID) |
4 | =file(“Orders_Time.ctx”).open().cursor@mx(;OrderDate>=start && OrderDate<=end) |
5 | =A4.switch(ProductID,A3:ProductID) |
6 | =A5.select(Quantity>ProductID.Quantity) |
7 | =A6.switch(ProductID).fetch() |
A3 Perform grouping and aggregation to get 1% of the sales volume of each ProductID. Since Productid values are unique, the result set can be used as a dimension table.
A6 Get target records by referencing corresponding dimension records according to the join field.
A7 Restore join field values to the original primary key values and fetch result data.
SQL
SELECT sum(Amount* (CASE WHEN Amount<=1000 THEN 0.1
WHEN Amount>1000 and Amount<=2000 THEN 0.2
WHEN Amount>2000 and Amount<=3000 THEN 0.3
WHEN Amount>3000 and Amount<=4000 THEN 0.4
ELSE 0.5 END)
) Amount,EmployeeID
FROM Orders
WHERE OrderDate>='2021-01-01' and OrderDate<='2021-12-31'
GROUP BY EmployeeID
SPL
A | |
---|---|
1 | >st=date(“2021-01-01”), et=date(“2021-12-31”), start=days@o(st), end=days@o(et) |
2 | =file(“Orders_Time.ctx”).open().cursor@mx(Amount,EmployeeID;OrderDate>=start && OrderDate<=end) |
3 | =file(“Commission.txt”).import@t() |
4 | =A3.(Amount) |
5 | =A2.join(A4.pseg(Amount),A3:#,Rate) |
6 | =A5.groups(EmployeeID;sum(Amount*Rate):Amount) |
A3 Generate an in-memory segmented dimension table.
A5 The pseg function finds the segment of dimension table in which the current record’s Amount value falls and returns the ordinal number of the corresponding record. Then perform association through the ordinal number.
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProcSPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL