declare @result table (FirstFieldID int, FirstFieldIDName varchar(100), SecondFieldID int, SecondFieldName varchar(100),ObjectID int, ObjectName varchar(100), SubSort int ,TotalStudents int)
insert into @result
select 1000003, 'Gender', 1000125, 'Female', -1 ,'-1', -4, 3
union select 1000003, 'Gender', 1000125, 'Female', 220 ,'Grade 12', -3, 2
union select 1000003, 'Gender', 1000125, 'Female', 200 ,'Grade 10', -3, 1
union select 1000003, 'Gender', 1000126, 'Male', -1 ,'-1', -4, 5
union select 1000003, 'Gender', 1000126, 'Male', 210 ,'Grade 11', -3, 3
union select 1000003, 'Gender', 1000126, 'Male', 220 ,'Grade 12', -3, 1
union select 1000003, 'Gender', 1000126, 'Male', 140 ,'Grade 4', -3, 1
union select 1000021, 'Title I Indicator', 1000380, 'Title I Indicator', -1, '-1', -4, 7
union select 1000021, 'Title I Indicator', 1000380, 'Title I Indicator', 210 ,'Grade 11', -3, 3
union select 1000021, 'Title I Indicator', 1000380, 'Title I Indicator', 220 ,'Grade 12', -3, 3
union select 1000021, 'Title I Indicator', 1000380, 'Title I Indicator', 200 ,'Grade 10', -3, 1
union select 1000010, 'Birth Country', 1000285, 'US', -1 ,'-1', -4, 4
union select 1000010, 'Birth Country', 1000285, 'US', 210 ,'Grade 11', -3, 2
union select 1000010, 'Birth Country', 1000285, 'US', 220 ,'Grade 12', -3, 2
select * from @result
+--------------+-------------------+---------------+-------------------+----------+------------+---------+---------------+
| FirstFieldID | FirstFieldIDName | SecondFieldID | SecondFieldName | ObjectID | ObjectName | SubSort | TotalStudents |
+--------------+-------------------+---------------+-------------------+----------+------------+---------+---------------+
| 1000003 | Gender | 1000125 | Female | -1 | -1 | -4 | 3 |
| 1000003 | Gender | 1000125 | Female | 220 | Grade 12 | -3 | 2 |
| 1000003 | Gender | 1000125 | Female | 200 | Grade 10 | -3 | 1 |
| 1000003 | Gender | 1000126 | Male | -1 | -1 | -4 | 5 |
| 1000003 | Gender | 1000126 | Male | 210 | Grade 11 | -3 | 3 |
| 1000003 | Gender | 1000126 | Male | 220 | Grade 12 | -3 | 1 |
| 1000003 | Gender | 1000126 | Male | 140 | Grade 4 | -3 | 1 |
| 1000021 | Title I Indicator | 1000380 | Title I Indicator | -1 | -1 | -4 | 7 |
| 1000021 | Title I Indicator | 1000380 | Title I Indicator | 210 | Grade 11 | -3 | 3 |
| 1000021 | Title I Indicator | 1000380 | Title I Indicator | 220 | Grade 12 | -3 | 3 |
| 1000021 | Title I Indicator | 1000380 | Title I Indicator | 200 | Grade 10 | -3 | 1 |
| 1000010 | Birth Country | 1000285 | US | -1 | -1 | -4 | 4 |
| 1000010 | Birth Country | 1000285 | US | 210 | Grade 11 | -3 | 2 |
| 1000010 | Birth Country | 1000285 | US | 220 | Grade 12 | -3 | 2 |
+--------------+-------------------+---------------+-------------------+----------+------------+---------+---------------+
目前我的數據如上所述。 當ObjectID和ObjectName爲-1時,TotalStudents將按組中的降序排列。否則,ObjectName是升序。 期待下面的數據。如何根據條件對多列進行排序
+--------------+-------------------+---------------+-------------------+----------+------------+---------+---------------+
| FirstFieldID | FirstFieldIDName | SecondFieldID | SecondFieldName | ObjectID | ObjectName | SubSort | TotalStudents |
+--------------+-------------------+---------------+-------------------+----------+------------+---------+---------------+
| 1000021 | Title I Indicator | 1000380 | Title I Indicator | -1 | -1 | -4 | 7 |
| 1000021 | Title I Indicator | 1000380 | Title I Indicator | 200 | Grade 10 | -3 | 1 |
| 1000021 | Title I Indicator | 1000380 | Title I Indicator | 210 | Grade 11 | -3 | 3 |
| 1000021 | Title I Indicator | 1000380 | Title I Indicator | 220 | Grade 12 | -3 | 3 |
| 1000003 | Gender | 1000126 | Male | -1 | -1 | -4 | 5 |
| 1000003 | Gender | 1000126 | Male | 140 | Grade 4 | -3 | 1 |
| 1000003 | Gender | 1000126 | Male | 220 | Grade 12 | -3 | 1 |
| 1000003 | Gender | 1000126 | Male | 210 | Grade 11 | -3 | 3 |
| 1000010 | Birth Country | 1000285 | US | -1 | -1 | -4 | 4 |
| 1000010 | Birth Country | 1000285 | US | 210 | Grade 11 | -3 | 2 |
| 1000010 | Birth Country | 1000285 | US | 220 | Grade 12 | -3 | 2 |
| 1000003 | Gender | 1000125 | Female | -1 | -1 | -4 | 3 |
| 1000003 | Gender | 1000125 | Female | 200 | Grade 10 | -3 | 1 |
| 1000003 | Gender | 1000125 | Female | 220 | Grade 12 | -3 | 2 |
+--------------+-------------------+---------------+-------------------+----------+------------+---------+---------------+
謝謝。
https://ozh.github.io/ascii-tables/使ASCII表從你的投入將是asier閱讀。 – xQbert
['ORDER BY'](https://docs.microsoft.com/zh-cn/sql/t-sql/queries/select-order-by-clause-transact-sql)和['CASE'](https ://docs.microsoft.com/en-us/sql/t-sql/language-elements/case-transact-sql)應該這樣做。對這些術語(和SQL)的搜索將提供大量結果。 – HABO
嘗試了很多方法,請求發佈查詢 –