我正在尝试创建一个SQL查询,但是确实有些挣扎,不确定如何编写最佳查询方式。我目前有2列,一列包含时间戳,另一列包含随机信息。我希望时间戳按降序排列(足够容易),但是对于第二列,我想按以下顺序输出结果
注意:%是有意的,因为它们在“剩余”列中。
例如:
两列都是字符串(最大varchar)
时间戳| 剩余时间 10:30 | %[EVENT = agentStateEvent] [Agentid = 424] [Queue = 45235]% TIMESTAMP 10:30 | %[EVENT = TerminalConnectionCreated] [Agentid = 424] [Queue = 45235]% TIMESTAMP:10.31 | %[EVENT = agentStateEvent] [Agentid = 425] [Queue = 453635]% TIMESTAMP 10.31 | %[EVENT = TerminalConnectionCreated] [Agentid = 425] [Queue = 45235]% TIMESTAMP 10.31 | %[EVENT = CallDropped] [Agentid = 425] [Queue = 45235]% TIMESTAMP 10.32 | %[EVENT = TerminalConnectionCreated] [Agentid = 426] [Queue = 44235]% TIMESTAMP 10.32 | %[EVENT = CallDropped] [Agentid = 426] [Queue = 45235]%
由于REMAINING列包含很多信息,因此需要将其包装在“ like”命令中。
我到目前为止的查询是:
select * from TimestampsStorage
order by timestamp desc, remaining desc
您正在寻找CASE ... WHEN
:
order by
timestamp,
case when remaining like '%[EVENT=agentStateEvent]%' then 1
when remaining like '%[EVENT=TerminalConnectionCreated]%' then 2
else 3
end;
万一'%[EVENT=...'
总是第一个进来remaining
,您可以寻找'%'显式。这样可以加快查询速度。
order by
timestamp,
case when remaining like '#%[EVENT=agentStateEvent]%' escape '#' then 1
when remaining like '#%[EVENT=TerminalConnectionCreated]%' escape '#' then 2
else 3
end;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句