2017-08-09 94 views
0
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 | 
+--------------+-------------------+---------------+-------------------+----------+------------+---------+---------------+ 

謝謝。

+2

https://ozh.github.io/ascii-tables/使ASCII表從你的投入將是asier閱讀。 – xQbert

+0

['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

+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 

您正在尋找這樣的事情?

+0

謝謝。 但預期產量不同。 –

+1

根據[數據類型優先順序](https://docs.microsoft.com/zh-cn/sql/t-sql/data-types/data-type-precedence-transact-sql),「ObjectName」將爲轉換爲「INT」進行比較。對於像「11年級」這樣的價值觀來說,這不太可能。 – HABO

+0

@HABO,我嘗試了下面的內容,我可以得到50%的正確率。 從FirstResultID = -1和ObjectID = -1和SecondFieldID = -1開始按#ResultTable排序,然後TotalStudents結束desc,當FirstFieldID!= -1和ObjectID = -1且SecondFieldID!= -1時,則TotalStudents結束desc,當FirstFieldID!= -1和ObjectID!= -1時,則SecondFieldID結束asc 它不符合我的要求。 –

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   

仍不能確定你由內而外組是什麼意思?團隊如何進行排序?

+0

如果ObjectID = -1且ObjectName = -1,則它是與FirstFieldIDName和SecondFieldName相關的基礎ObjectName的標頭。 –

+0

在哪個序列中? –

+0

如果ObjectID = -1且ObjectName = -1,那麼** TotalStudents **將降序並且ObjectID!= - 1和ObjectName!= -1爲相應的FirstFieldIDName和SecondFieldName,然後** TotalStudents **將升序 –

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' 
+0

此處的排序僅基於studentid完成。您需要根據您的要求在row_number()子句中找出​​分組和排序 – Aparna

+0

謝謝。 但我上面發佈的預期結果是不同的。 我期待與我發佈的結果相同。 –

相關問題