这个查询:
left join (select tSpent, Customerid from (select SUM(spent) as tSpent, Customerid, RN = ROW_NUMBER() OVER (PARTITION BY Customerid ORDER BY Customerid DESC) from Customer_Sales WHERE Customerid is not null) where RN = 1) Sales ON Customer_Sales.Customerid = Data.Customerid
在线给我一个错误RN=1
:
关键字'where'附近的语法不正确
Vamsi Prabha.. 5
(select tSpent, Customerid from (select SUM(spent) as tSpent, Customerid, RN = ROW_NUMBER() OVER (PARTITION BY Customerid ORDER BY Customerid DESC) from Customer_Sales WHERE Customerid is not null) t --use a alias for the derived table where RN = 1) Sales ON Customer_Sales.Customerid = Data.Customerid
不确定为什么row_number
被使用.你也应该group by customerid
.
内部查询可以简化为
select SUM(spent) as tSpent, Customerid from Customer_Sales WHERE Customerid is not null group by customerid
因为你只是试图获得每个客户的总支出.
(select tSpent, Customerid from (select SUM(spent) as tSpent, Customerid, RN = ROW_NUMBER() OVER (PARTITION BY Customerid ORDER BY Customerid DESC) from Customer_Sales WHERE Customerid is not null) t --use a alias for the derived table where RN = 1) Sales ON Customer_Sales.Customerid = Data.Customerid
不确定为什么row_number
被使用.你也应该group by customerid
.
内部查询可以简化为
select SUM(spent) as tSpent, Customerid from Customer_Sales WHERE Customerid is not null group by customerid
因为你只是试图获得每个客户的总支出.