Код: Выделить всё
00-01 01-02 ... 23-00
05.09.2007 10 3 ... 5
06.09.2007 6 12 ... 1
Сейчас делается через создание промежуточных таблиц
1. СоздаЮ таблицу Matrix со структурой
Код: Выделить всё
StartDat EndDat Cnt00 Cnt01 ... Cnt23
05.09.2007 00:00:00 05.09.2007 01:00:00 1 0 0
05.09.2007 01:00:00 05.09.2007 02:00:00 0 1 0
.......
05.09.2007 23:00:00 06.09.2007 00:00:00 0 0 1
Код: Выделить всё
select mx.StartDat, mx.EndDat,
count(od.OrderID)*mx.Cnt00 as Cnt00,
count(od.OrderID)*mx.Cnt01 as Cnt01,
.....
count(od.OrderID)*mx.Cnt23 as Cnt23
from Orders od, Matrix mx
where (od.Start between mx.StartDat and mx.EndDat)
and (od.Start <> mx.EndDat)
group by mx.StartDat, mx.EndDat
4. СоздаЮ таблицу TmpDat очень простой структуры
Код: Выделить всё
StartCalc EndCalc
05.09.2007 23:00:00 06.09.2007 00:00:00
......
15.09.2007 23:00:00 16.09.2007 00:00:00
Код: Выделить всё
select dat.StartCalc,
sum(Cnt.Cnt00) as Cnt00,
sum(Cnt.Cnt01) as Cnt01,
...
sum(Cnt.Cnt23) as Cnt23
from TmpDat dat, TmpCount cnt
where (cnt.StartDat between dat.StartCalc and dat.EndCalc)
and cnt.StartDat <> dat.EndCalc
group by dat.StartCalc
И вот кажется мне, что я чего-то недопонимаю, и можно упросить это дело, но чего-то видимо не учитываю... А?