简体   繁体   English

选择范围在最大值内的行

[英]Selecting rows with max value in range

I have the followng data: 我有以下数据:

+----+---------+---------------------+-------+
| id | site_id |      datetime       | views |
+----+---------+---------------------+-------+
|  1 |       1 | 2013-11-01 23:59:59 |    12 |
|  2 |       1 | 2013-11-02 23:59:59 |    15 |
|  3 |       1 | 2013-11-03 23:59:59 |    18 |
|  4 |       1 | 2013-11-04 23:59:59 |    29 |
|  5 |       1 | 2013-11-05 23:59:59 |    38 |
|  6 |       1 | 2013-11-05 12:59:59 |    40 |
|  7 |       1 | 2013-11-06 23:59:59 |    45 |
|  8 |       1 | 2013-11-07 23:59:59 |    49 |
|  9 |       1 | 2013-11-08 23:59:59 |    52 |
| 10 |       2 | 2013-11-04 23:59:59 |    25 |
| 11 |       2 | 2013-11-05 21:59:59 |    42 |
| 12 |       2 | 2013-11-06 23:59:59 |    60 |
| 13 |       2 | 2013-11-07 23:59:59 |    75 |
| 14 |       2 | 2013-11-08 23:59:59 |    86 |
| 15 |       2 | 2013-11-09 23:59:59 |    90 |
| 16 |       2 | 2013-11-10 23:59:59 |    92 |
| 17 |       2 | 2013-11-11 23:42:59 |    98 |
+----+---------+---------------------+-------+

I would pass a day and wish to get the rows with the highest time in the given day or if there`s no records for this site for this day, the last available row in the past. 我希望通过一天,希望获得给定日期中时间最长的行,或者如果该站点在这一天中没有记录,则为过去的最后一行。

eg for 2013-11-01 例如对于2013-11-01

+----+---------+---------------------+-------+
| id | site_id |      datetime       | views |
+----+---------+---------------------+-------+
|  1 |       1 | 2013-11-01 23:59:59 |    12 |
+----+---------+---------------------+-------+

for 2013-11-05 2013-11-05

+----+---------+---------------------+-------+
| id | site_id |      datetime       | views |
+----+---------+---------------------+-------+
|  5 |       1 | 2013-11-05 23:59:59 |    38 |
| 11 |       2 | 2013-11-05 21:59:59 |    42 |
+----+---------+---------------------+-------+

and for 2013-11-10 而对于2013-11-10

+----+---------+---------------------+-------+
| id | site_id |      datetime       | views |
+----+---------+---------------------+-------+
|  9 |       1 | 2013-11-08 23:59:59 |    52 |
| 16 |       2 | 2013-11-10 23:59:59 |    92 |
+----+---------+---------------------+-------+

Thanks in advance. 提前致谢。

You can try this: 您可以尝试以下方法:

SELECT a.id,a.site_id,b.maxDate,a.views
FROM table1 a
INNER JOIN (
    SELECT  site_id ,MAX(datetime) as maxDate
    FROM table1
    WHERE datetime < DATEYOUWANTTOSEE + INTERVAL 1 DAY
    GROUP BY site_id
) b ON a.site_id = b.site_id AND a.datetime = b.maxDate 

The inner query will get you the MAX(datetime) for each site_id. 内部查询将为您获取每个site_id的MAX(datetime)。 Then you join it with your table to get the rest of the information. 然后,将其与表连接起来以获取其余信息。

sqlfiddle demo sqlfiddle演示

SELECT * FROM <tablename> WHERE datetime = <datetime> ORDER BY datetime DESC LIMIT 2

I believe this should work: 我相信这应该有效:

SELECT SUBSTRING(`datetime`, 1, 10) AS date, MAX(`views`)
FROM table
GROUP BY SUBSTRING(`datetime`, 1, 10)

If you need the id/site_id as well, write it in your post, as it is not clear 如果还需要id / site_id,请将其写在您的帖子中,因为目前尚不清楚

SQLFiddle to show you the result: http://sqlfiddle.com/#!2/e0ccb3/1 SQLFiddle向您显示结果: http ://sqlfiddle.com/#!2/e0ccb3/1

You data and examples do not really match themselves, nor the description, but what you probably are looking for is this: 您的数据和示例并没有真正匹配它们,也没有描述,但您可能正在寻找的是:

Select top 1 * 
  from table
 where date(datetime) <= date(@PARAMETER)

order by datetime desc 按日期时间顺序排序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM