我有一個查詢:爲什麼查詢會顯示錯誤月份的數據?
;with date_cte as(
SELECT r.starburst_dept_name,r.monthly_past_date as PrevDate,x.monthly_past_date as CurrDate,r.starburst_dept_average - x.starburst_dept_average as Average
FROM
(
SELECT *,ROW_NUMBER() OVER(PARTITION BY starburst_dept_name ORDER BY monthly_past_date) AS rowid
FROM intranet.dbo.cse_reports_month
) r
JOIN
(
SELECT *,ROW_NUMBER() OVER(PARTITION BY starburst_dept_name ORDER BY monthly_past_date) AS rowid
FROM intranet.dbo.cse_reports_month
Where month(monthly_past_date) > month(DATEADD(m,-2,monthly_past_date))
) x
ON r.starburst_dept_name = x.starburst_dept_name AND r.rowid = x.rowid+1
Where r.starburst_dept_name is NOT NULL
)
Select *
From date_cte
Order by Average DESC
所以做了一些測試,我改變了一些列的數據,看看爲什麼它給了我一定的信息。我不知道爲什麼,當我運行查詢它給了我一個日期列不應該從「一月」(第4行),在那裏像下面的圖片:
數據庫具有具有相同的多個數據確切的日期'2014年1月25日00:00:00:00',所以我不知道爲什麼它只會得到這一行,並比較平均?
我做了之前我運行查詢改變該行中的列並更改日期?但我不確定這是否與它有關。
更新: 我已經添加了sqlfinddle, 我想獲得它減去平均 從LAST_MONTH - 最後2個月前。 它實際上一直在工作,直到我做出更改並更改數據。 我做了一些修改來測試某種情況,這顯然導致 得知查詢存在缺陷。
一個SQL小提琴將是很好的方式 – UnhandledExcepSean
http://sqlfiddle.com/#!3/d74b6/1 – user3408399