I'm looking for some advice how to set up a set of queries which check for an aggregated value for sampling points within a time period. This should run at an IQ-server, so maybe not this many procedure calls would be cool 😉
I had a look into the windowing feature. I think it might only work for aggregate data for timeframe, not at a sampling point. So having this scenario:
I have a list of items with a start and an end date. I need to collect a sum of processes active at a current time. It's not about system processes, but more about something like how many craftsman were working at a given time.
Imagine tables like this (I've modified the example a little to leave out boring parts…. so might it not run perfectly)
create table items (
id int ot null default autoincrement,
"Type" integer,
TimeStampStart datetime null,
TimeStampend datetime null
)
is currently used by this queryset:
create table #Processes(
"Type" integer,
"timestamp" "datetime" null,
"Sum" integer null
)
set @date = '20120303'
while @date <= '20130505'
begin
insert into #Processes
select "Type",'timestamp'=@date,'Sum'="count"()
from "items"
and "TimeStampStart" between "dateadd"("day",-"abs"(100),@date) and @date
and "TimeStampStart" <= @date
and "isnull"("TimeStampEnd",@date) >= @date
group by "Type"
set @date = "dateadd"("ss",3600,@date)
end
select * from #Processes;
This might not the best way of doing it. So I'm looking for a better approach 😉
Best Answer
I would take answer by Micheael Green a step further and suggest generating a Numbers table. It will help many other algorythms as well. Another handy table is Calendar table with every date for +/- 20 years. You can get numbers from ID column of such table as well.
Here is a query I came up with. You can easily wrap it into SP to TVF. It works for me. I tested on my dev db so had to prefix table names with tmp.
Play with >, >=, <, <= sings to count or not those processes that start/finish on boundary.
Another way to organise this query to overcome single CPU limitatation