我有一个select查询绑定到具有特定ORDER BY CASE
语句的数据集。查询功能正常并返回正确的数据,但主键Ordnum
的顺序每次在CASE
语句中排序不同。对此查询的任何编辑都会使排序保持一致?查询是:
SELECT Ordnum, BrwrFstNme, BrwrLastNme, PrpAddr, PrpCity, PrpSt, PrpZip5, PrpCntyNme, errorCategory, errorReason, errorReason2, userName, priority,
completed, timeAssigned, timeCompleted, notes, flag, week, process, ignoreGISScoreCard, currentMonth, currentYear, currentDay, currentDOY, OrdDTE, sfha
FROM qc_Orders WHERE (userName = @usrnme) AND (completed IS NULL) AND (week = (SELECT TOP (1) QCTrack FROM CloneOrders ORDER BY QCTrack DESC))
ORDER BY
(CASE WHEN process = 'Manual' AND notes LIKE '%wells%' THEN '1'
WHEN process IN ('883', '885', '886', '887', '900', '901', '902') AND notes LIKE '%wells%' THEN '2'
WHEN (process = '888' OR process LIKE '889%') AND notes LIKE '%wells%' THEN '3'
WHEN process = 'Manual' AND (notes NOT LIKE '%wells%' or notes IS NULL) THEN '4'
WHEN process IN ('883', '885', '886', '887', '900', '901', '902') AND (notes NOT LIKE '%wells%' or notes IS NULL) THEN '5'
ELSE process END) ASC
修改
我确实需要在tiebreaker
声明中加入Order By Case
。我还创建了更多CASE
语句,但简化了result_expression
。添加Ordnum
作为最终条款消除了每个CASE
内的随机排序。最终查询如下:
SELECT Ordnum, BrwrFstNme, BrwrLastNme, PrpAddr, PrpCity, PrpSt, PrpZip5, PrpCntyNme, errorCategory, errorReason, errorReason2, userName, priority,
completed, timeAssigned, timeCompleted, notes, flag, week, process, ignoreGISScoreCard, currentMonth, currentYear, currentDay, currentDOY, OrdDTE, sfha
FROM qc_Orders WHERE (userName = @usrnme) AND (completed IS NULL) AND (week = (SELECT TOP (1) QCTrack FROM CloneOrders ORDER BY QCTrack DESC))
ORDER BY
CASE WHEN NOTES LIKE '%WELLS%' THEN 1 END DESC,
CASE WHEN PROCESS = 'MANUAL' THEN 1 END DESC,
CASE WHEN PROCESS IN ('883', '885', '886', '887', '900', '901', '902') THEN 1 END DESC,
CASE WHEN PROCESS = '888' THEN 1 END DESC,
CASE WHEN PROCESS LIKE '889%' THEN 1 END DESC,
CASE WHEN NOTES LIKE '%WELLS%' THEN 2 END DESC,
CASE WHEN PROCESS = 'MANUAL' THEN 2 END DESC,
CASE WHEN PROCESS IN ('883', '885', '886', '887', '900', '901', '902') THEN 2 END DESC,
CASE WHEN PROCESS = '888' THEN 2 END DESC,
CASE WHEN PROCESS LIKE '889%' THEN 2 END DESC,
ORDNUM ASC
答案 0 :(得分:1)
您应在tiebreaker
子句中添加ORDER
。如果Ordnum
中的CASE
是唯一的,请在CASE
之后添加ORDER
。如果没有,那么您将不得不在SELECT Ordnum, BrwrFstNme, BrwrLastNme, PrpAddr, PrpCity, PrpSt, PrpZip5, PrpCntyNme, errorCategory, errorReason, errorReason2, userName, priority,
completed, timeAssigned, timeCompleted, notes, flag, week, process, ignoreGISScoreCard, currentMonth, currentYear, currentDay, currentDOY, OrdDTE, sfha
FROM qc_Orders WHERE (userName = @usrnme) AND (completed IS NULL) AND (week = (SELECT TOP (1) QCTrack FROM CloneOrders ORDER BY QCTrack DESC))
ORDER BY
(CASE WHEN process = 'Manual' AND notes LIKE '%wells%' THEN '1'
WHEN process IN ('883', '885', '886', '887', '900', '901', '902') AND notes LIKE '%wells%' THEN '2'
WHEN (process = '888' OR process LIKE '889%') AND notes LIKE '%wells%' THEN '3'
WHEN process = 'Manual' AND (notes NOT LIKE '%wells%' or notes IS NULL) THEN '4'
WHEN process IN ('883', '885', '886', '887', '900', '901', '902') AND (notes NOT LIKE '%wells%' or notes IS NULL) THEN '5'
ELSE process END) ASC, Ordnum
子句中添加更多列:
accountManager.getAccounts()