开发者

MySQL - Reporting using a week to represent all rows with a datestamp in that week

I am trying to build a table of data where each row represents a week (i.e., 09/23-09/30). Each row is built from data from several different rows that have datestamps within that week period. The rest of the columns are subselects that typically give averages of all the data in all the rows in the week period.

Logically, what is the best way to go about this? I've managed to create a query that gives me one row with a startDate and endDate and all the data for the startDate and endDate.

What I need is to have the query return each consequent weeks single row of data, basically giving me the entire report as opposed to one line of the report.

Unfortunately, I can't simply loop through the query in PHP. This has to be in a stored procedure when all is said and done.

Edit:

Here is the SQL I am currently using

BEGIN

if (NOT fromDate) then
    SET fromDate = "2007-9-25";
end if;

if (NOT toDate) then
    SET toDate = CURDATE();
end if;

SELECT
    (SELECT
        CONCAT(
            MONTH(fromDate),
            '/',
        开发者_StackOverflow社区    DAY(fromDate),
            ' - ',
            MONTH(toDate),
            '/',
            DAY(toDate)
        )
    ) AS dateRange
    ,
    (SELECT
        COUNT(DISTINCT kioskID)
        FROM tbl_rpt_kiosksessions
        WHERE startDate
        BETWEEN fromDate AND toDate

    ) AS NumberOfStores
    ,
    (SELECT
        COUNT(sessionID)
        FROM tbl_rpt_kiosksessions
        WHERE startDate
        BETWEEN fromDate AND toDate
    ) AS NumberOfSessions
    ,
    (
        (
            (
            (SELECT
            COUNT(sessionID)
            FROM tbl_rpt_kiosksessions
            WHERE startDate
            BETWEEN fromDate AND toDate
            )
            /
            (SELECT
            COUNT(DISTINCT kioskID)
            FROM tbl_rpt_kiosksessions
            WHERE startDate
            BETWEEN fromDate AND toDate
            )
            )

        /7)
    ) AS SessionsPerDay
    ,
    (SELECT
        (AVG(duration)/60)
        FROM tbl_rpt_kiosksessions
        WHERE startDate
        BETWEEN fromDate AND toDate
    ) AS AvgSessionLength
FROM
tbl_rpt_kiosksessions
WHERE
startDate
BETWEEN "2010-02-09" AND CURDATE()
GROUP BY DATE_FORMAT(startDate, '%Y-%V');

END

The result of this supposing the following parameters ('2010-02-07','2010-02-14') is:

Image of the record returned

Which is obviously exactly what you'd expect. What I need to figure out is how to get this automated using just a startDate. So, for instance:

startDate = '2010-01-24'

And then getting result sets returned for:

01/24 - 01/30 01/31 - 02/06 02/07 - 02/14

and so on until the endDate is given or is simply CURDATE().

Thoughts?


SELECT data columns / aggregate functions FROM table GROUP BY DATE_FORMAT(date_column, '%Y-%V')

(%V assumes weeks start on Sunday - switch to %v for Monday)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜