Hello I have the following tables:
Brand Columns: Brand_ID(Primary Key), Company Name
Orders Columns: ORDER_ID(Primary Key), USER_ID, BRAND_ID(Foreign Key), DATE
I'm trying to get the 3rd order for each USER_ID, placed after 2018, if the order is from COMPANY_NAME X. I'm kind of lost on how to proceed. I've tried using a window function, but I don't think it lets me use a WHERE function inside?
I have the following code:
SELECT *
FROM (
SELECT O.*,
row_number() OVER (
PARTITION BY USER_ID
WHERE DATE >= '2018/01/01'
ORDER BY DATE
) as rank
FROM Orders O
) O
JOIN Brands B ON O.BRAND_ID = B.BRAND_ID
WHERE O.rank = 3 AND B.COMPANY_NAME = 'X';
You want to filter in a where
clause -- which is not part of the window function specification:
SELECT *
FROM (SELECT O.*,
row_number() OVER (PARTITION BY USER_ID
ORDER BY DATE
) as rank
FROM Orders O
WHERE DATE >= '2018-01-01'
) O JOIN
Brands B
ON O.BRAND_ID = B.BRAND_ID
WHERE O.rank = 3 AND B.COMPANY_NAME = 'X';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.