Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用聚合函数选择字段值的mysql查询_Mysql_Aggregate - Fatal编程技术网

使用聚合函数选择字段值的mysql查询

使用聚合函数选择字段值的mysql查询,mysql,aggregate,Mysql,Aggregate,MySQL 5.5.29 下面是我正在处理的一个mysql查询,但没有成功: SELECT ID, Bike, (SELECT IF( MIN( ABS( DATEDIFF( '2011-1-1', Reading_Date ) ) ) = ABS( DATEDIFF( '2011-1-1', Reading_Date ) ) , Reading_Date, NULL ) FROM odometer WHERE Bike=10 ) AS StartDate, (SELECT IF( M

MySQL 5.5.29 下面是我正在处理的一个mysql查询,但没有成功:

SELECT ID, Bike, 
(SELECT IF( MIN( ABS( DATEDIFF(  '2011-1-1', Reading_Date ) ) ) = ABS( DATEDIFF(  '2011-1-1', Reading_Date ) ) , Reading_Date, NULL ) FROM odometer WHERE Bike=10  ) AS StartDate, 
(SELECT IF( MIN( ABS( DATEDIFF(  '2011-1-1', Reading_Date ) ) ) = ABS( DATEDIFF(  '2011-1-1', Reading_Date ) ) , Miles, NULL ) FROM odometer WHERE Bike=10  ) AS BeginMiles, 
(SELECT IF( MIN( ABS( DATEDIFF(  '2012-1-1', Reading_Date ) ) ) = ABS( DATEDIFF(  '2012-1-1', Reading_Date ) ) , Reading_Date, NULL ) FROM odometer  WHERE Bike=10  ) AS EndDate,
(SELECT IF( MIN( ABS( DATEDIFF(  '2012-1-1', Reading_Date ) ) ) = ABS( DATEDIFF(  '2012-1-1', Reading_Date ) ) , Miles, NULL ) FROM odometer WHERE Bike=10  ) AS EndMiles
FROM  `odometer` 
WHERE Bike =10;
结果是:

ID  Bike    StartDate   BeginMiles  EndDate EndMiles
14  10 [->] 2011-04-15  27.0    NULL    NULL
15  10 [->] 2011-04-15  27.0    NULL    NULL
16  10 [->] 2011-04-15  27.0    NULL    NULL
摩托车车主每年在1月1日或临近1月1日时输入一次里程表读数。我想计算一下骑摩托车的总里程

以下是里程表表中的数据: 资料来源:

因此,为了计算2011年这辆自行车的里程数,我需要确定哪些记录更接近2011年1月1日,这是记录14。起始里程为27英里。我需要找到最接近2012年1月1日的记录,即记录15。2011年的终点里程为10657英里,这也是计算2012年时的起始里程表读数

这是表格:

DROP TABLE IF EXISTS `odometer`;
CREATE TABLE IF NOT EXISTS `odometer` (
  `ID` int(3) NOT NULL AUTO_INCREMENT,
  `Bike` int(3) NOT NULL,
  `is_MOA` tinyint(1) NOT NULL,
  `Reading_Date` date NOT NULL,
  `Miles` decimal(8,1) NOT NULL,
  PRIMARY KEY (`ID`),
  KEY `Bike` (`Bike`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=22 ;
表式里程表数据

我试图从不同的记录中获取日期和英里数,这样我就可以将开始英里数与结束英里数进行细分,以获得示例中某辆自行车的总英里数。在本例中,2011年为某一特定年份,自行车=10

我已经读了很多关于聚合函数和从正确记录中获取值的问题的书。我想答案在某种程度上是存在的。但是当尝试上面的查询时,我只从第一条记录中获取数据。在这种情况下,结束里程应来自第二条记录


我希望有人能给我指出正确的方向。

英里数应该在稳步增加。如果像这样的事情能奏效,那就太好了:

select year(Reading_Date) as yr,
        max(miles) - min(miles) as MilesInYear
from odometer o
where bike = 10
group by year(reading_date)
唉,你的逻辑比你想象的要困难得多。在SQL Server 2012或Oracle等具有超前和滞后功能的数据库中,这将更容易实现

我的方法是找到每年的第一次和最后一次阅读日期。可以使用相关子查询计算此值:

select o.*,
       (select max(date) from odometer o2 where o.bike = o2.bike and o2.date <= o.date - dayofyear(o.date) + 1
       ) ReadDateForYear
from odometer o
接下来,从自行车和年份两个层面进行总结。如果第一年或年初之前没有读取日期,请使用第一个日期:

select bike, year(date) as yr,
       coalesce(min(ReadDateForYear), min(date)) as FirstReadDate,
       coalesce(min(ReadDateForNextYear), max(date)) as LastReadDate
from (select o.*,
             (select max(date) from odometer o2 where o.bike = o2.bike and o2.date <= o.date - dayofyear(o.date) + 1
             ) ReadDateForYear,
             (select max(date) from odometer o2 where o.bike = o2.bike and o2.date <= date_add(o.date -      dayofyear(0.date) + 1 + interval 1 year)
             ) ReadDateForNextYear
      from odometer o
     ) o
group by bike, year(date)
让我称之为。要获得最终结果,您需要以下内容:

select the fields you need
from <q> q join
     odometer s
     on s.bike = q.bike and year(s.date) = q.year join
     odometer e
     on s.bike = q.bike and year(e.date) = q.year

注意:此SQL未经测试。我肯定有语法错误。

查询的预期结果是什么?
select the fields you need
from <q> q join
     odometer s
     on s.bike = q.bike and year(s.date) = q.year join
     odometer e
     on s.bike = q.bike and year(e.date) = q.year