按案例陈述排序不按预期工作

时间:2016-10-27 22:38:00

标签: sql-server sql-order-by

我正在尝试按最终用户想要的方式对报告进行排序。

首先,它应按以下方案排序:

  1. 填充String_Field,但Comment_Date为空
  2. 未填充String_Field
  3. 填充String_Field和Comment_Date
  4. 然后它应该按Task_Date的老化排序。

    我已将以下内容添加到我的ORDER BY:

    (case when max(table1.comment_date) is null then 2 else 1 end) DESC,
    (case when table1.string_field is null then 2 else 1 end) ASC,
    max(table2.task_date) ASC
    

    然后我将它们添加到我的SELECT中:

    (case when max(table1.comment_date) is null then 2 else 1 end) as sort1
    (case when table1.string_field is null then 2 else 1 end) as sort2
    max(table2.task_date) as task_date
    

    我得到了什么,并且无法弄清楚我的生活为什么,这是(编辑显示每次更改时的高/低):

    Picture of Sample Results

    我已经尝试过投射,移动聚合并将值加在一起,而我仍然最终将那些2s卡在1s中的一些看不见的分裂中间。

    如果它有任何区别,我通过CTE联合查询得出string_field的值,并且在第二个CTE(table1)连接到包含task_date的表之前,在第二个CTE中将comment_date连接到string_field (表2)。你可以有一个没有comment_date的string_field,但是没有一个没有string_field的comment_date。必须有一个task_date才能使报表中显示该条目。该报告按string_field分组。

    我在使用SQL Server 2008 R2。

    提前感谢您的帮助!

    编辑添加Quasi-SQL:

    WITH 
    old_new_string AS (
        SELECT
        tablea.old_string_field,
        tablea.string_field,
        'A' as match_type
        FROM
        tablea
        WHERE
        tablea.old_string_field is not null
        UNION ALL
        SELECT
        B1.string_field AS old_string_field,
        B2.string_field AS string_field,
        'B1' as match_type
        FROM
        tableb B1
        INNER JOIN tableb B2 ON B1.string_field <> B2.string_field AND B1.id1 = B2.id1
        WHERE
        B1.id1 is not null
        UNION ALL
        SELECT
        B1.string_field AS old_string_field,
        B2.string_field AS string_field,
        'B2' as match_type
        FROM
        tableb B1
        INNER JOIN tableb B2 ON B1.string_field <> B2.string_field AND B1.id1 = B2.id2
        WHERE
        B1.id1 is not null),
    table1 AS (
        SELECT TOP 100 PERCENT /* Enables order by to improve join performance */
        old_new_string.old_string_field,
        old_new_string.string_field,
        old_new_string.match_type,
        comment_date = (SELECT max(comment_table.comment_date) FROM comment_table WHERE old_new_string.string_field = comment_table.string_field and comment_table.comment_code = '001')
        FROM
        old_new_string
        ORDER BY
        old_new_string.old_string_field)
    SELECT
    tablez.string_field as old_string_field, 
    max(table2.task_date) as task_date, 
    min(cast(getdate() - table2.task_date as bigint)) as Aging
    table1.string_field as new_string_field, 
    max(table1.match_type) as match_type, 
    max(table1.comment_date) as comment_date, 
    sort_order1 = (case when max(table1.comment_date) is null then 2 else 1 end), 
    sort_order2 = (case when table1.string_field is null then 2 else 1 end) 
    FROM 
    tablez 
    INNER JOIN table2 ON tablez.string_field = table2.string_field 
    LEFT JOIN table1 on tablez.string_field = table1.old_string_field
    Where 
    table2.task_date IS NOT NULL
    Group By 
    tablez.string_field, table1.new_string_field  
    ORDER BY 
    (case when max(table1.comment_date) is null then 2 else 1 end) DESC,
    (case when table1.string_field is null then 2 else 1 end) ASC,
    max(table2.task_date) ASC
    

1 个答案:

答案 0 :(得分:0)

ORDER BY 
  case when max(table1.comment_date) is null then 2 else 0 end 
+ case when table1.string_field is null then 0 else 1 end DESC

ORDER BY CASE
   WHEN max(table1.comment_date) is null and table1.string_field is not null 
      THEN 0
   WHEN table1.string_field is null 
      THEN 1
   WHEN max(table1.comment_date) is not null and table1.string_field is not null 
      THEN 2
      ELSE 3 END