English 中文(简体)
MySQL - 使用一周来表示所有在该周内具有日期时间戳的行的报告
原标题:MySQL - Reporting using a week to represent all rows with a datestamp in that week
  • 时间:2010-02-17 20:11:20
  •  标签:
  • sql
  • mysql

我正在尝试建立一个数据表,其中每行代表一个星期(即09/23-09/30)。每行都由在该星期时间段内具有日期时间戳的几个不同行的数据构建而成。其余列是子查询,通常给出该星期时间段内所有行中所有数据的平均值。

从逻辑上讲,怎样去做是最好的?我已经成功创建了一个查询,给我提供了一个包含开始日期和结束日期以及所有开始日期和结束日期的数据的行。

我需要的是查询返回每个连续周的单行数据,基本上给我整个报告而不是报告的一行。

很遗憾,我不能简单地在PHP中循环查询。最终必须在存储过程中完成。

编辑:

这里是我目前正在使用的SQL。

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),
             / ,
            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

假设参数为(2010-02-07,2010-02-14),则结果是:

返回的记录图像

这显然就是你所期望的。我需要弄清楚的是如何仅使用开始日期来自动化处理这个问题。例如:

startDate =  2010-01-24 

然后获得返回的结果集:

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

然后一直进行直到给定endDate或仅为CURDATE()。

想法?

问题回答
SELECT data columns / aggregate functions FROM table GROUP BY DATE_FORMAT(date_column,  %Y-%V )

%V 假设每周从星期日开始 - 切换到 %v 以获得星期一。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签