2016-02-29 61 views
-4
WITH rows AS 
(
    SELECT 
     *, ROW_NUMBER() OVER (ORDER BY dtDeliveryDate) AS rn 
    FROM  
     TCheckRecipient 
) 
SELECT 
    AVG(avarage) 
FROM 
    (SELECT 
     TCheck.iCheckId, 
     AVG(DATEDIFF(minute, mc.dtDeliveryDate, mp.dtDeliveryDate)) AS average 
    FROM  
     rows mc 
    LEFT JOIN 
     TCheck ON TCheck.iCheckId = mc.iCheckId 
    JOIN  
     rows mp ON mc.rn = mp.rn - 1 
    GROUP BY 
     TCheck.iCheckId) 
+1

發佈沒有任何源數據查詢一些與期望的輸出,並聲明「它不工作」是不是非常有幫助。除了你知道這個查詢應該如何工作,它應該產生什麼結果以及實際產生什麼,否則這裏沒有人。 –

+1

請詳述:*詳細說明* enigmatic *不工作(不編譯,返回意外的數據等,並提供預期的行爲以及)*格式化*查詢 –

+1

哪個RDBMS是這樣的?請添加一個標籤來指定您是使用'mysql','postgresql','sql-server','oracle'還是'db2' - 或者其他的東西。 –

回答

0

你有錯字錯誤AVG(avarage),它應該是平均

此外,請記住在您的子查詢上分配別名。

WITH rows AS 
(
    SELECT 
     *, ROW_NUMBER() OVER (ORDER BY dtDeliveryDate) AS rn 
    FROM  
     TCheckRecipient 
) 
SELECT 
    AVG(avarage) 
FROM 
    (SELECT 
     TCheck.iCheckId, 
     AVG(DATEDIFF(minute, mc.dtDeliveryDate, mp.dtDeliveryDate)) AS average 
    FROM  
     rows mc 
    LEFT JOIN 
     TCheck ON TCheck.iCheckId = mc.iCheckId 
    JOIN  
     rows mp ON mc.rn = mp.rn - 1 
    GROUP BY 
     TCheck.iCheckId) as A 
相關問題