当前位置:  开发笔记 > 编程语言 > 正文

此子查询出错

如何解决《此子查询出错》经验,为你挑选了1个好方法。

这个查询:

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

因为你只是试图获得每个客户的总支出.



1> Vamsi Prabha..:
(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

因为你只是试图获得每个客户的总支出.

推荐阅读
跟我搞对象吧
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有