12-09-2024 02:08 PM - edited 12-11-2024 11:53 AM
This article discusses how to unlock complex data questions with Spark SQL Views and streamline Data Access with Real-World Examples.
We recommend that you be familiar with these Incorta concepts before exploring this topic further.
These concepts apply to Incorta versions 2024.7.x and above
Here are some real world examples of how Spark SQL views can be used. Keep in mind that with Spark SQL views you can use any sql functions supported in Spark 3.x.
WITH Customer_Aggregates AS
(SELECT Customer,
SUM(Order_Amount) AS total_order_amount
FROM OrderToCashCopilot.Order2Cash
WHERE Order_Amount > 1000
GROUP BY Customer
),
Percentile_Calculation AS
(SELECT case
when getPresVar("p_var", "90") = 90
then PERCENTILE(total_order_amount, 0.9)
when getPresVar("p_var", "90") = 80
then PERCENTILE(total_order_amount, 0.8)
when getPresVar("p_var", "90") = 70
then PERCENTILE(total_order_amount, 0.7)
when getPresVar("p_var", "90") = 60
then PERCENTILE(total_order_amount, 0.6)
else PERCENTILE(total_order_amount, 0.5)
end percentile_n
FROM
Customer_Aggregates
)
SELECT c.Customer,
round(c.total_order_amount) total_order_amount
FROM Customer_Aggregates c
JOIN
Percentile_Calculation p
ON c.total_order_amount > p.percentile_n
ORDER BY
c.total_order_amount DESC