'Aggregation on quantile column-NTILE
I have a CTE with below structure type-
WITH CTE1 AS (
SELECT
,
DATE,
ID,
NAME,
CATEGORY,
SUBCATEGORY,
CHANNEL_NAME,
price,
discount,
NTILE(4) OVER (
PARTITION BY CHANNEL_NAME
ORDER BY
PRICE
) as PRICE_QUARTILE
from
table 1
)
SELECT
DATE,
ID,
NAME,
CATEGORY,
SUBCATEGORY,
'all',
AVG(price) OVER (
PARTITION BY PRICE_QUARTILE
ORDER BY
PRICE ROWS UNBOUND PRECEDING
) AS PRICE,
AVG(DISCOUNT) OVER (
PARTITION BY PRICE_QUARTILE
ORDER BY
DISCOUNT ROWS UNBOUND PRECEDING
) AS DISCOUNT
FROM
CTE1
GROUP BY
1,
2,
3,
4,
5,
6
UNION ALL
SELECT
*
FROM
CTE1
This is giving me an error as union is used and there is 1 column missing in select statement. I am not getting how to get the 'PRICE_QUARTILE' column as an aggregation in below select statement. Basically I have written above query to aggregation of prices for channel as 'overall' also did union to get individual channel's values too. Need to this NTILE(4) function to put price bands on products as to which product belongs to which price.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
