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 |
+--------------+-------------------+---------------+-------------------+----------+------------+---------+---------------+
At present my data would be like above. When ObjectID and ObjectName are -1 then the TotalStudents will be in descending order with in the group. Otherwise ObjectName is Ascending order. Expecting the data like below.
+--------------+-------------------+---------------+-------------------+----------+------------+---------+---------------+
| 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 |
+--------------+-------------------+---------------+-------------------+----------+------------+---------+---------------+
↧