I am trying to compare value with an alias but it says scolumn not recognized. Docs says can't use alias in where clause
WITH CTE AS
(
SELECT
StockCode AS TopProduct,
COUNT(CustomerID) AS mostCustomers
FROM
dbfinalweek.`e-commerce`
GROUP BY
StockCode
ORDER BY
mostCustomers DESC
LIMIT 1
)
SELECT
StockCode AS stock, CustomerID,
LEAD(StockCode, 1) OVER (ORDER BY CustomerID, InvoiceDate) AS NextItem,
LAG(StockCode, 1) OVER () AS PreviousItem,
InvoiceDate
FROM
dbfinalweek.`e-commerce` AS table1
WHERE
(table1.StockCode = (SELECT CTE.TopProduct FROM CTE)) OR
(table1.NextItem = (SELECT CTE.TopProduct FROM CTE))
Here is my query. Any idea how I could make Table1.NextItem
work?
question from:
https://stackoverflow.com/questions/65864724/how-to-use-alias-in-where-clause-in-mysql-with-lead-lag-functions 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…