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 |
+--------------+-------------------+---------------+-------------------+----------+------------+---------+---------------+
谢谢。
答案 0 :(得分:0)
CASE WHEN statement for ORDER BY clause
SELECT *
FROM TABLE
ORDER BY
CASE WHEN ObjectID = -1 AND ObjectName = -1 THEN TotalStudents END DESC
CASE WHEN ObjectID <> -1 AND ObjectName <> -1 THEN ObjectName END ASC
你在找这样的东西吗?
答案 1 :(得分:0)
我认为您正在寻找如下查询:
Select * from #sortData
order by case when objectid =-1 and objectname = '-1' then row_number() over(order by TotalStudents)
when objectid <> -1 then row_number() over(order by ObjectName asc) end desc
仍然不确定你的内心组是什么意思?如何对组进行排序?
答案 2 :(得分:0)
尝试下面的内容,让我知道它是否有效:
select * ,row_number() over(partition by flag order by totalstudents desc)
as rn from(
select *,
case when objectid=-1 and objectname='-1' then 'Des' else 'Asc'
end as flag
from result
)a
where flag='Des'
union all
select * ,row_number() over(partition by flag order by totalstudents) as
rn1 from(
select *,
case when objectid=-1 and objectname='-1' then 'Des' else 'Asc'
end as flag
from result
)a
where flag='Asc'