将Mysql中的Query转换为SQL?

时间:2014-05-06 09:17:38

标签: sql sql-server-2008

我正在使用实体框架我收到错误'"查询语法无效。近期':',第1行,第109栏。"来自system .data.entity.sqlserver。所以请告诉我在SQL中转换下面的查询

SELECT Agentid,year(Date_Time) as Year,monthname(Date_Time) as  Month,SUM(AmountRecevied) as Amount,@rownum := @rownum + 1 AS Rank FROM collection_master,  
(SELECT @rownum := 0) r GROUP BY AgentID,year(Date_Time),monthname(Date_Time) ORDER BY  
    Amount DESC


  Agentid         logintime             AmountReceived

1        2013/10/01 00:10:10         10000
 1        2013/10/01 17:23:10         200
 1        2013/10/01 00:30:41         3000
2        2013/10/02 05:10:52         1000
  3        2013/10/02 09:10:25         2000 
3        2013/10/03 10:10:18         2000
2        2013/10/03 13:10:35         7000 

我想要一个应该将输出显示为

的查询
 Agentid    Amount  Rank
1         13200    1
2         8000     2
3         4000     3

我试过下面的查询没有得到预期的输出请检查一次

with temp as (
select row_number() over (order by AmountRecevied) as rownum,AgentID,YEAR(Date_Time) as Years,SUM(AmountRecevied) as amount 
from tblcollections  group by CustomerID,AgentID,Date_Time ,AmountRecevied

 select rownum,AgentID,Years,amount from temp 

1 个答案:

答案 0 :(得分:1)

这应该做的工作:我不能测试它,所以请试一试。

WITH CTE AS
(
SELECT Agentid,year(Date_Time) as Year, MONTH(Date_Time) as  Month,SUM(AmountRecevied) as Amount 
FROM collection_master,  
GROUP BY AgentID,year(Date_Time),MONTH(Date_Time) 
) 
SELECT *, ROW_NUMBER() OVER (ORDER BY CTE.Amount DESC) AS RANKING
FROM CTE 
Order by RANKING