我正在使用Sql Server Management Studio 2016,我有一個返回少於10個結果的查詢,問題是我需要通過特定的文本字段來排序結果,但所需的順序不是按字母順序排列的。如果可能的結果是abc,cde,ghf等,我想先選擇cde並忽略其他,如果ghf不可用,如果非那些則abc。因爲這是一個子查詢,所以我總是從更高級別的select中獲得相同數量的結果,但只需要滿足列表中第一個值的那個,而忽略所有其他值。任何想法,將不勝感激。感謝您可以使用SQL Server:Subquery Issue 900
0
A
回答
1
的一種方法是join
:
select top 1 q.*
from (<your query here>) q left join
(values ('abc', 1), ('def', 2), . . .
) v(val, ordering)
on q.col = v.val
order by v.ordering;
另一種方法就更簡單了。 。 。
select top 1 q.*
from (<your query here>) q
order by charindex('[' + col + ']', '[abc],[def],...')
0
另一種選擇,你可以使用case
表達式返回根據您要自定義排序分配給該列中的值:
select *
from t
order by
case col
when 'cde' then 0
when 'ghf' then 1
when 'abc' then 2
else 3
end
相關問題
- 1. Sql Select Issue
- 2. Sql Select Order Issue
- 3. SQL QUERY ISSUE(NOT IN)
- 4. SQL Server like statement issue
- 5. SQL INSERT ISSUE WITH CTE
- 6. AWS RDS SQL Server RESTORE ISSUE
- 7. SQL分揀WITH GROUP ISSUE BY
- 8. Materialise Sidenav Icon Issue Issue
- 9. 關於寬度大於900的火jquery> 900
- 10. (C#)If-Else Statement Issue Issue
- 11. SQL - 更新大表(900萬條記錄)的查詢性能
- 12. asp:textbox issue
- 13. UIApplicationInvalidInterfaceOrientation issue
- 14. SecurityAttribute.Unrestricted issue
- 15. Swift:CABasicAnimation Issue
- 16. android tabbar oreintation issue
- 17. HTTP Post issue
- 18. OpenGL Texture issue
- 19. spring:batch listener issue
- 20. jquery showhide issue
- 21. CSS flex width issue
- 22. Swing button repaint issue
- 23. ThreeJS ShaderMaterial Texture Issue
- 24. listbox select issue
- 25. scipy.special import issue
- 26. Yii relations issue
- 27. jquery if statement issue
- 28. Microsoft translator api issue
- 29. Image Carousel Interval Issue
- 30. NSNotification register place issue
問題不是特別清楚。你想根據你定義的一組有限的可能值進行排序? – AndySavage
你可以顯示你的表結構和一些代碼嗎? – shoover