2014-05-06 91 views
0

我正在使用實體框架我得到一個錯誤「」查詢語法無效。近期':',第1行,第109列。「從系統.data.entity.sqlserver。所以請告訴我下面的查詢轉換成SQL將Mysal中的查詢轉換爲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 
+0

您無法使用:將值分配給變量。 你想得到什麼?查詢的弊端是什麼? – PeterRing

+0

請檢查我的eddited問題 – user123

+0

SQL Server具有真正的窗口功能,不需要破解來生成一個rownumber。檢查出'row_number()'函數 –

回答

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 
+0

如何查找日期在sql – user123

+0

日期(日期時間)天作爲它顯示無效函數 – user123

+0

sry我的意思MONTH – PeterRing