我遇到了麻煩,我需要快速回答。SQL Server 2008 - 查詢按奇數和偶數列排序
我有一張表,其中有一列只包含奇數和偶數。
我想選擇第一隻行與含orderByColumn
奇數,然後將結果添加到「甚至查詢」
我的表看起來像這樣
col1 col2 orderByColumn col4
...................................
c c 44 c
c c 45 c
c c 46 c
...................................
...................................
我想我應該做一個工會,我想應該是這樣的:
select * from myTable
where [orderByColumn] % 2 = 0 --order by [orderByColumn]-->error
UNION
select * from myTable
where [orderByColumn] % 2 > 0 order by [orderByColumn]
查詢確實工會2個選擇,但很明顯,他們是由合併和秩序,我不想。
我希望結果是這樣的:
c c 44 c
c c 46 c
c c 48 c
........
c c 45 c
c c 47 c
c c 49 c
........
任何想法?謝謝:)
+1你仍然需要orderByColumn'添加',到'爲了by'到保證奇數/偶數值的順序。 –
如果您希望數字在各個奇數/偶數組中排列順序,您需要第二次按order byy列排序 – LittleBobbyTables