SQL:按列的日期和范围获取数据

时间:2015-05-12 13:00:31

标签: mysql

我有一个名为' Library'的MySQL表。下面有三列。我需要计算每个日期的停留时间范围。我需要的范围是0-30,31-60,61-90和91-100。

    Id   Date        dwell_time 
    1  2015-05-10      27
    2  2015-05-10      28
    3  2015-05-10      32
    4  2015-05-10      65
    5  2015-05-11      27
    6  2015-05-11      28
    7  2015-05-11      65

应该返回

2015-05-10 0-30 2
2015-05-10 31-60 1
2015-05-10 61-90 1
2015-05-11 0-30 2
2015-05-11 61-90 1

您能帮忙解决我需要使用的查询吗?

1 个答案:

答案 0 :(得分:2)

$(document).ready(function(){ 
$(".rD").draggable({
stop: function(event, ui) { 
// get the index of the dragged element 
id = $(this).index();
// get coordinates of the dragged element
rect = document.getElementsByTagName("div")[id].getBoundingClientRect();
alert("top:" + rect.top + ", left: " + rect.left);
// clearing variables didn't help to solve the problem
delete rect;
delete id;
}
});
编辑(戈登,因为我在评论中被问到):

SELECT
    date,
    date_range,
    count(1)
FROM (
    SELECT
        date,
        CASE
            WHEN dwell_time BETWEEN 0 AND 30 THEN '0-30'
            WHEN dwell_time BETWEEN 31 AND 60 THEN '31-60'
            WHEN dwell_time BETWEEN 61 AND 90 THEN '61-90'
            ELSE '90-100'
        END AS date_range
    FROM Library) lib
GROUP BY date, date_range

Here是SQL小提琴。