【问题标题】:How to sort multiple column based on condition如何根据条件对多列进行排序
【发布时间】:2017-08-10 13:11:48
【问题描述】:
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 |
+--------------+-------------------+---------------+-------------------+----------+------------+---------+---------------+

谢谢。

【问题讨论】:

  • ozh.github.io/ascii-tables 从您的输入中制作 ascii 表会更易于阅读。
  • ORDER BYCASE 应该这样做。在 SO 上搜索这些术语(和 SQL)将提供大量结果。
  • 试了很多方法,求发帖
  • 您的帖子未包含您的代码或研究。如果您添加它,也许有人可以帮助您修复它。
  • @HABO,当 FirstFieldID = -1 和 ObjectID = -1 和 SecondFieldID = -1 然后 TotalStudents 结束 desc 时,请从 #ResultTable 中选择 *,当 FirstFieldID != -1 和ObjectID = -1 and SecondFieldID != -1 then TotalStudents end desc, case when FirstFieldID != -1 and ObjectID != -1 then SecondFieldID end asc

标签: sql-server sql-server-2008 tsql


【解决方案1】:

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

你在寻找这样的东西吗?

【讨论】:

  • 基于data type precedenceObjectName将转换为INT进行比较。对于像“11 年级”这样的值来说,这不太可能顺利。
  • @HABO,我尝试如下,我能够得到 50% 的正确率。 select * from #ResultTable order by case when FirstFieldID = -1 and ObjectID = -1 and SecondFieldID = -1 then TotalStudents end desc, case when FirstFieldID != -1 and ObjectID = -1 and SecondFieldID != -1 then TotalStudents end desc , case when FirstFieldID != -1 and ObjectID != -1 then SecondFieldID end asc 不符合我的要求。
  • @HABO 感谢您的提醒,让我摆弄一下
  • 旁白:IsNumeric() 是出了名的problematic。希望这不是你摆弄的方向。
【解决方案2】:

我认为您正在查看如下查询:

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         

仍然不确定您所说的内部组是什么意思?需要如何对组进行排序?

【讨论】:

  • 如果 ObjectID =-1 和 ObjectName = -1,那么它是与 FirstFieldIDName 和 SecondFieldName 相关的底层 ObjectNames 的标头。
  • 按哪个顺序?
  • 如果 ObjectID =-1 且 ObjectName = -1 则 TotalStudents 将降序且 ObjectID !=-1 且 ObjectName != -1 分别对应 FirstFieldIDName 和 SecondFieldName 然后 TotalStudents 将递增
  • 在 ObjectId = -1 之间必须先出现还是 -1 必须先出现?哪个顺序?
  • , ObjectId = -1 优先
【解决方案3】:

试试下面的,让我知道它是否有效:

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'

【讨论】:

  • 这里的排序只根据studentid进行,需要根据自己的需求在row_number()子句中确定分组和排序方式
  • 谢谢。但是我在上面发布的预期结果是不同的。我期待与我发布的结果相同。
猜你喜欢
  • 2013-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-27
  • 2023-02-01
  • 2016-07-13
  • 1970-01-01
相关资源
最近更新 更多