1

这是一个我似乎无法解决的微不足道的问题。我正在使用 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)
4

2 回答 2

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。

于 2012-07-12T13:22:51.597 回答
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`
于 2013-10-02T22:37:05.243 回答