-- 指定日以前にOpenのチケット
SELECT sum(delta) AS count
FROM (
SELECT id,date(time,'unixepoch') tm,1 delta
FROM ticket
WHERE time < strftime('%s','2008-03-24')
UNION ALL
SELECT ticket id , date(time,'unixepoch') tm, (CASE newvalue WHEN 'closed' THEN -1 WHEN 'reopened' THEN 1 ELSE 0 END) delta
FROM ticket_change
WHERE field='status'
AND time < strftime('%s','2008-03-24')
ORDER BY tm ,id
)
-- 日別のNewまたはreopenedチケット
SELECT tm , sum(delta) AS count
FROM (
SELECT id,date(time,'unixepoch') tm,1 delta
FROM ticket
WHERE time >= strftime('%s','2008-03-24') AND time <= strftime('%s','2008-04-23')
UNION ALL
SELECT ticket id , date(time,'unixepoch') tm, (CASE newvalue WHEN 'reopened' THEN 1 ELSE 0 END) delta
FROM ticket_change
WHERE field='status'
AND time >= strftime('%s','2008-03-24') AND time <= strftime('%s','2008-04-23')
ORDER BY tm ,id
)
GROUP BY tm
ORDER BY tm ASC
-- 日別にClosedになったチケット数
SELECT tm , sum(delta) AS count
FROM (
SELECT ticket id , date(time,'unixepoch') tm, (CASE newvalue WHEN 'closed' THEN 1 ELSE 0 END) delta
FROM ticket_change
WHERE field='status'
AND time >= strftime('%s','2008-03-24') AND time <= strftime('%s','2008-04-23')
ORDER BY tm ,id
)
GROUP BY tm
ORDER BY tm ASC
RE: TracのDBから、チケット数を集計するSQLについて (2008-04-25 00:18 by okamototk #36266)
私もあまりSQLは詳しくありませんが、ちょっと質問させてください。
> ORDER BY tm ,id
> )
> GROUP BY tm
> ORDER BY tm ASC
上記のORDER BY tm, idのtmによるソートは必要ですか?
どっちみに最後のORDER BY tmでソートされるように思うのですが...