这里的一个微不足道的问题似乎无法解决。我正在使用LogParser来分析一些电话系统CSV日志文件。我正在尝试生成两个日期之间所有来电的图表,按星期几分组。我已经实现了这一点,但我很难让图表以正确的顺序显示列,从星期一开始。我传递给LogParser的SQL查询如下:
SELECT to_string(to_timestamp(c1, 'yyyy/MM/dd HH:mm'), 'dddd') as Day, count(*) as Calls
INTO graph.gif
FROM C:\logs\*.txt
WHERE c5 = 'I'
AND to_timestamp(c1, 'yyyy/MM/dd HH:mm')
BETWEEN
timestamp('10/05/2012', 'dd/MM/yyyy')
AND timestamp('24/05/2012', 'dd/MM/yyyy')
GROUP BY Day
它实际上输出的顺序没有任何ORDER BY语句,但我认为第一列始终是日期范围开始的那一天,而我希望它始终是星期一(为了便于比较)图表)。目前发生的是它将我的日志文件中的时间戳转换为星期几的字符串表示(出于输出目的),据我所知,没有内置逻辑“星期一”小于'星期二'等等。我的直觉是在ORDER BY中使用CASE语句,但LogParser不接受这个。
这是我的整个LogParser命令,只要它是相关的,
C:\Program Files (x86)\Log Parser 2.2\Logparser.exe" -i:CSV "SELECT to_string(to_timestamp(c1, 'yyyy/MM/dd HH:mm'), 'dddd') as Day, count(*) as Calls INTO graph.gif FROM C:\logs\*.txt WHERE c5 = 'I' AND to_timestamp(c1, 'yyyy/MM/dd HH:mm') BETWEEN timestamp('10/05/2012', 'dd/MM/yyyy') AND timestamp('24/05/2012', 'dd/MM/yyyy') GROUP BY Day" -chartType:Column3D -chartTitle:"Incoming Calls" -values:ON -config:MyConfig.js -o:CHART -headerRow OFF -iHeaderFile C:\logs\header\header.txt
我想要的只是我的结果订单总是在星期一开始,无论日期范围如何。有什么想法吗?
编辑 - 这是包含在ORDER BY中的CASE语句的查询。
SELECT to_string(to_timestamp(c1, 'yyyy/MM/dd HH:mm'), 'dddd') as Day, count(*) as Calls
INTO graph.gif
FROM C:\logs\*.txt
WHERE c5 = 'I'
AND to_timestamp(c1, 'yyyy/MM/dd HH:mm')
BETWEEN
timestamp('10/05/2012', 'dd/MM/yyyy')
AND timestamp('24/05/2012', 'dd/MM/yyyy')
GROUP BY Day
ORDER BY (CASE
WHEN Day = 'Monday' THEN 1
WHEN Day = 'Tuesday' THEN 2
WHEN Day = 'Wednesday' THEN 3
WHEN Day = 'Thursday' THEN 4
WHEN Day = 'Friday' THEN 5
WHEN Day = 'Saturday' THEN 6
WHEN Day = 'Sunday' THEN 7
END)
答案 0 :(得分:0)
您的订单需要看起来像:
order by (case when day = 'Monady' then 1
when day = 'Tuesday' then 2
when day = 'Wednesday' then 3
when day = 'Thursday' then 4
when day = 'Friday' then 5
when day = 'Saturday' then 6
when day = 'Sunday' then 7
end)
或者,如果您的数据库具有星期几功能,那么您可以执行以下操作:
order by (datepart(day, <date>)+5)%7
这是SQL Server语法。 datepart给出了星期日1和星期一2.算术只是将星期一换到0。
答案 1 :(得分:0)
.....
` CASE Day WHEN 'Monday' THEN 1
else CASE Day WHEN 'Tuesday' THEN 2
else CASE Day WHEN 'Wednesday' THEN 3
else CASE Day WHEN 'Thursday' THEN 4
else CASE Day WHEN 'Friday' THEN 5
else CASE Day WHEN 'Saturday' THEN 6
else CASE Day WHEN 'Sunday' THEN 7
END end end end end end end AS DayNumber`
........
`Order by DayNumber`