我正在使用WITH SELECT INTO
來建立一個新表,我希望根據FLOAT
列中的一個降序排列值。這裏是原始代碼(最後一行是我嘗試給結果排序):ORDER BY爲什麼不對這個查詢進行排序?
WITH attributionErrorCandidates AS
(
SELECT
a.ID aID,
a.FREQUENCY aFREQ,
MAX(dbo.JaroWInkler(a.NAME,b.NAME)) score,
ROW_NUMBER() OVER(PARTITION BY a.ID ORDER BY MAX(dbo.JaroWInkler(a.NAME,b.NAME)) DESC) RN,
b.ID bID,
b.FREQUENCY bFREQ
FROM #brandDivisionAttributionCounts a
JOIN #brandDivisionAttributionCounts b
ON a.BRAND_LEVEL = b.BRAND_LEVEL
AND a.DIVISION = b.DIVISION
AND a.ID <> b.ID
AND a.NAME <> b.NAME
GROUP BY a.ID,b.ID,a.FREQUENCY,b.FREQUENCY
)
SELECT
bdac.NAME AS NAME,
bdac.FREQUENCY AS NAME_FREQUENCY,
bdac2.NAME AS OTHER_NAME,
bdac2.FREQUENCY AS OTHER_NAME_FREQUENCY,
score AS CONFIDENCE,
#brandLevels.NAME AS BRAND_LEVEL,
bdac.DIVISION AS DIVISION
INTO ArmanS.attributionErrorRankings
FROM attributionErrorCandidates
INNER JOIN #brandDivisionAttributionCounts AS bdac
ON bdac.ID = aID
AND RN = 1
AND score > @scoreCutOff
INNER JOIN #brandDivisionAttributionCounts AS bdac2
ON bdac2.ID = bID
INNER JOIN #brandLevels
ON bdac.BRAND_LEVEL = #brandLevels.ID
WHERE bdac.FREQUENCY/bdac2.FREQUENCY > @requiredRatio
ORDER BY score DESC -- doesn't seem to work!
我試圖與cast(score AS FLOAT)
和DIVISION
更換score
無濟於事。結果未按評分排序(在結果表中稱爲DIVISION
)。
看看這個問題的接受答案,http://stackoverflow.com/questions/14424929/preserving-order-by-in-select-into – jac
@jac是有道理的! – arman
重要的是要記住,唯一的「自然順序」是聚集索引,因爲我們可以使用它。在優化性能時,理解和利用自然順序非常重要,因爲Order By(== Sort)是一項非常昂貴的操作。例如,如果我像你的表一樣製作一個表作爲臨時表,並且我希望以相同順序多次使用它,那麼我可以在Insert之前創建關鍵列的聚集索引,從而僅對它進行排序一次(在插入期間)。 –