SQL Query以相反的顺序生成结果

时间:2015-10-16 09:02:13

标签: sql sql-server tsql

我有一个包含TIMESTAMPREMAINING列的表格。我正在尝试按降序排序TIMESTAMP,但让REMAINING列以非常具体的顺序输出结果:

  1. AgentStateEvent
  2. TerminalConnectionCreated
  3. 无论如何
  4. 然而它是这样的结果(从我能说的相反顺序):

    TIMESTAMP|| REMAINING
    %[TIMESTAMP=1441737006102]||[EVENT=CallControlTerminalConnectionHeld]
    %[TIMESTAMP=1441737006102]||[EVENT=TerminalConnectionCreated][callID=11411]
    %[TIMESTAMP=1441737006102]||[EVENT=agentStateEvent]
    %[TIMESTAMP=1441737006102]||[EVENT=CallControlTerminalConnectionHeld]
    %[TIMESTAMP=1441737006102]||[EVENT=TerminalConnectionCreated][callID=11411]
    %[TIMESTAMP=1441737006102]||[EVENT=CallControlTerminalConnectionHeld]
    %[TIMESTAMP=1441737006102]||[EVENT=TerminalConnectionCreated]
    %[TIMESTAMP=1441737006102]||[EVENT=agentStateEvent]
    %[TIMESTAMP=1441737006102]||[EVENT=agentStateEvent]
    

    应该是:

       %[TIMESTAMP=1441737006102]||[EVENT=agentStateEvent]
     %[TIMESTAMP=1441737006102]||[EVENT=TerminalConnectionCreated][callID=11411]
     %[TIMESTAMP=1441737006102]||[EVENT=CallControlTerminalConnectionHeld]
    

    等...

    我目前拥有的SQL如下:

    select *
    from [TIMESTAMP_ORGANISER].[dbo].[TEST_DB]
    order by 
        timestamp,
        case 
           when remaining like '%[EVENT=agentStateEvent]%' then 1
           when remaining like '%[EVENT=TerminalConnectionCreated]%' then 2 
           else 3
        end;
    

    我试过像这样反过来做SQL:

    select *
    from [TIMESTAMP_ORGANISER].[dbo].[TEST_DB]
    order by 
        timestamp,
        case 
           when remaining like '%[EVENT=agentStateEvent]%' then 3
           when remaining like '%[EVENT=TerminalConnectionCreated]%' then 2 
           else 1
        end;
    

    但这没有区别。我做错了什么?

    编辑,尝试了以下内容:

    SELECT * 
    FROM [TIMESTAMP_ORGANISER].[dbo].[TEST_DB]
    ORDER BY 
        timestamp, 
        CASE 
           WHEN remaining LIKE '%agentStateEvent%' THEN -3
           WHEN remaining LIKE '%TerminalConnectionCreated%' THEN -2
           ELSE -1 
        END
    

    不幸的是,结果会像这样结束:

     %[TIMESTAMP=1441737006102]||[EVENT=agentStateEvent]
     %[TIMESTAMP=1441737006102]||[EVENT=agentStateEvent]
     %[TIMESTAMP=1441737006102]||[EVENT=agentStateEvent]
     %[TIMESTAMP=1441737006102]||[EVENT=agentStateEvent]
     %[TIMESTAMP=1441737006102]||[EVENT=agentStateEvent]
    %[TIMESTAMP=1441737006102]||[EVENT=TerminalConnectionCreated]%
    [TIMESTAMP=1441737006102]||[EVENT=TerminalConnectionCreated]%
    [TIMESTAMP=1441737006102]||[EVENT=TerminalConnectionCreated]
    

2 个答案:

答案 0 :(得分:2)

您可以将所有CASE排名乘以-1以获得所需的订单:

SELECT * FROM [TIMESTAMP_ORGANISER].[dbo].[TEST_DB]
ORDER BY timestamp, CASE WHEN remaining LIKE '%agentStateEvent%' THEN -1
                         WHEN remaining LIKE '%TerminalConnectionCreated%' THEN -2
                    ELSE -3 END

我还更改了您的LIKE匹配条件,因为根据您的搜索结果,即使原始查询中的内容出现故障也是您不想要的。

答案 1 :(得分:0)

Order by remaining desc, case when remaining like '%[EVENT=agentStateEvent]%' then 1
          when remaining like '%[EVENT=TerminalConnectionCreated]%' then 2 
          else 3
     end asc;