我有一个包含TIMESTAMP
和REMAINING
列的表格。我正在尝试按降序排序TIMESTAMP
,但让REMAINING
列以非常具体的顺序输出结果:
然而它是这样的结果(从我能说的相反顺序):
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]
答案 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;