gpt4 book ai didi

sql - 该子查询出错

转载 作者:行者123 更新时间:2023-12-02 15:58:34 26 4
gpt4 key购买 nike

此查询:

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 上给我一个错误行:

Incorrect syntax near the keyword 'where'

最佳答案

(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。您还应该按 customerid 分组

内部查询可以简化为

select SUM(spent) as tSpent, Customerid
from Customer_Sales
WHERE Customerid is not null
group by customerid

因为您只想获取每个客户的总支出。

关于sql - 该子查询出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34270919/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com