Discussion
I have a table with lots of records in it, each record has a date-time field.
I want to present the distribution of record counts on a time line basis. The time line might be weeks, months, days, hours or minutes.
Is there a cunning way to do this in SQL where I can group the record counts according to time periods or do I need to code up loops that do a count for a period and increment a variable onto the next period before executing the count SQL again?
The idea is to both generate graphs and also provide the user with an option to delete records in a specific period.
I want to present the distribution of record counts on a time line basis. The time line might be weeks, months, days, hours or minutes.
Is there a cunning way to do this in SQL where I can group the record counts according to time periods or do I need to code up loops that do a count for a period and increment a variable onto the next period before executing the count SQL again?
The idea is to both generate graphs and also provide the user with an option to delete records in a specific period.
Interesting question ... I don't think I've seen any sort of aggregation SQL query that does this (which is not to say that there isn't one), but what you're wanting to do is a bog standard statistical task, so there may be some analysis extensions for your database platform that will do this for you. What database platform are you using?
ATG said:
Interesting question ... I don't think I've seen any sort of aggregation SQL query that does this (which is not to say that there isn't one), but what you're wanting to do is a bog standard statistical task, so there may be some analysis extensions for your database platform that will do this for you. What database platform are you using?
It's MySQL 
Hree's a book that probably would help ... but you seem to have to pay for it before you can read it ... bastards 
http://safari.oreilly.com/0596001452/mysqlckbk-CHP...

http://safari.oreilly.com/0596001452/mysqlckbk-CHP...
SlidingSideways said:
Create a table containing the start and end time/date of each period.
Join the data table to this "period" table so you get result rows with a period ID column, then sord/order/group by this column.
HTH.
Can you elaborate a bit, I get your drift, just not sure how to implement it.Join the data table to this "period" table so you get result rows with a period ID column, then sord/order/group by this column.
HTH.
Say I wanted a month-count view, start date would be Jan 2007, end date would be Sept 2007.
I want to produce a table in a web page that shows
Period...Record Count
01-01-2007 31-01-2007...123421
01-02-2007 28-02-2007...323422
01-03-2007 31-03-2007...423223
etc....
mystomachehurts said:
SlidingSideways said:
Create a table containing the start and end time/date of each period.
Join the data table to this "period" table so you get result rows with a period ID column, then sord/order/group by this column.
HTH.
Can you elaborate a bit, I get your drift, just not sure how to implement it.Join the data table to this "period" table so you get result rows with a period ID column, then sord/order/group by this column.
HTH.
Say I wanted a month-count view, start date would be Jan 2007, end date would be Sept 2007.
I want to produce a table in a web page that shows
Period...Record Count
01-01-2007 31-01-2007...123421
01-02-2007 28-02-2007...323422
01-03-2007 31-03-2007...423223
etc....
from mainrecord
inner join periods on mainrecord.thedatetime >= periods.start and mainrecord.thedatetime <= periods.finish
group by periods.periodnumber
Period Table - PERIODS
P_ID | START_DATE | END_DATE
-----+------------+------------
1 | 01/01/2007 | 31/01/2007
2 | 01/02/2007 | 28/02/2007
3 | 01/03/2007 | 31/03/2007
Data Table - DATA
D_ID | RECORD_DATE | DATA
-----+-------------+------------
1 | 09/02/2007 | wibble1
2 | 02/02/2007 | wibble2
3 | 21/03/2007 | wibble3
4 | 28/01/2007 | wibble4
5 | 16/01/2007 | wibble5
6 | 19/02/2007 | wibble6
To join them use the following SQL:
SELECT
p.P_ID, d.*
FROM
PERIOD p,
DATA d
WHERE
d.START_DATE <= p.RECORD_DATE
AND d.END_DATE >= p.RECORD_DATE
Result is:
P_ID | D_ID | RECORD_DATE | DATA
-----+------+-------------+------------
2 | 1 | 09/02/2007 | wibble1
2 | 2 | 02/02/2007 | wibble2
3 | 3 | 21/03/2007 | wibble3
1 | 4 | 28/01/2007 | wibble4
1 | 5 | 16/01/2007 | wibble5
2 | 6 | 19/02/2007 | wibble6
Don said:
mystomachehurts said:
SlidingSideways said:
Create a table containing the start and end time/date of each period.
Join the data table to this "period" table so you get result rows with a period ID column, then sord/order/group by this column.
HTH.
Can you elaborate a bit, I get your drift, just not sure how to implement it.Join the data table to this "period" table so you get result rows with a period ID column, then sord/order/group by this column.
HTH.
Say I wanted a month-count view, start date would be Jan 2007, end date would be Sept 2007.
I want to produce a table in a web page that shows
Period...Record Count
01-01-2007 31-01-2007...123421
01-02-2007 28-02-2007...323422
01-03-2007 31-03-2007...423223
etc....
from mainrecord
inner join periods on mainrecord.thedatetime >= periods.start and mainrecord.thedatetime <= periods.finish
group by periods.periodnumber

So I create a table with 4 colums,
id - the id of the frequency count being performed.
start-date
end-date
count
So for a 3 month span starting in Jan we have 3 records, one for each period...
1...01-01-2007...31-01-2007...?
1...01-02-2007...28-02-2007...?
1...01-03-2007...31-03-2007...?
Someone else could be running weekly span query for Jan and would have
2...01-01-2007...07-01-2007...?
2...08-01-2007...14-01-2007...?
2...15-01-2007...21-01-2007...?
2...22-01-2007...28-01-2007...?
2...29-01-2007...31-01-2007...?
Am I not better just running a loop in SQL that takes the startdate, a period, be it weeks, days months, and doing the counts that way?
Won't I run into trouble if the user clicks off the web page before the results are processed and leaves redundant records in this table?
mystomachehurts said:
So I just need a cunning algorythm to populate the the period table with records for the different start and end dates and different intervals - i.e. the monthly, weekly, daily, hourly intervals.
Well you can either set up the periods table permanently OR you could generate it on the fly into a temporary table in a stored procedure. Most SQL databases will happily create a temporary table that drops itself (no need to do it explicitly) when the stored procedure drops out of scope.This is a piece of piss in SQL Server - I'm not a MySQL guy but I'll bet it can do something similar.
I do these types of stats regularly.
Column A What I do is stick it all in Excel, change the datetime stamp to be date only, ie 05-Sep-07 (ie get rid of time)
Column B In the next column put a numeric integer to point to the date, ie '1' for '05-Sep-07' '2' for '06-Sep-07'
Column C In the next column do a INT(B1/7), for example if you were doing a week by week usage
Then use the excel Grouping feature to get totals, ie
Week 1 count = 123
Week 2 count = 456
and so on.
Column A What I do is stick it all in Excel, change the datetime stamp to be date only, ie 05-Sep-07 (ie get rid of time)
Column B In the next column put a numeric integer to point to the date, ie '1' for '05-Sep-07' '2' for '06-Sep-07'
Column C In the next column do a INT(B1/7), for example if you were doing a week by week usage
Then use the excel Grouping feature to get totals, ie
Week 1 count = 123
Week 2 count = 456
and so on.
mystomachehurts said:
FunkyGibbon said:
if you can connect to the database via ODBC/OLEDB then you can use the pivot table and aggregate functions in M$ Excel.
Not an option, I need to do this in PHP for the purge and graphing functions.mystomachehurts said:
Nice solution and excellent for standard durations. That looks like what you need!For non-standard durations (i.e. not all the same length) the "period table" approach would do nicely.
Don said:
mystomachehurts said:
Nice solution and excellent for standard durations. That looks like what you need!For non-standard durations (i.e. not all the same length) the "period table" approach would do nicely.
However if you're doing this sort of OLAP stuff on your main transaction table, then you may want to either take heed of the comments in the web link regarding building a separate table and adding to it if performance starts becoming a concern.
I mention this because the 'period table' concept Don and SlidingSideways presented would require your report SQL to join the date column with a BETWEEN operator or less than / greater than. However I can come up with a slightly less elegant but faster method... if your date field on the transaction table is simply a date (with no time component) then you can populate the period table with one row for *every day* and its corresponding period ID. The join between the two tables will then be where the transaction date *equals* the period table date, and most modern DBMS optimisers should use a hash join from the fact table to the dimension table which is MUCH faster than inner loop for this type of OLAP query.
This isn't practical if your date field on the transaction table contains a time component, though. And if you use a function to strip off the time portion then the speed advantage will be lost since most DBMSes won't use an index when your join has a computed column (i.e. a function).
As you know I'm not a MySQL expert though - I do the big stuff but I assume that MySQL has a range of join strategies and algorithms.

Gassing Station | Computers, Gadgets & Stuff | Top of Page | What's New | My Stuff




