简体   繁体   English

PHP Mysql按累积值按日期分组

[英]PHP Mysql group by date from accumulative values

I have a table with ID, value1, value2, value3 and tstamp (TIMESTAMP format) and i'm trying to group by each day and find a total for each day, however all values are accumulative, which poses a problem, because sum(value1) doesnt give the right output, this is my code: 我有一个ID,value1,value2,value3和tstamp(TIMESTAMP格式)的表,我试图按天分组并找到每一天的总数,但是所有值都是累加的,这引起了问题,因为sum( value1)没有给出正确的输出,这是我的代码:

$sql = "select date(tstamp), sum(".$column.") from mash group by date(tstamp) order by tstamp asc limit 10";
$result = mysql_query($sql);
$previous = 0;
$firstRun = true;
while($row = mysql_fetch_array($result))
{
  $difference = $row[1] - $previous;
  if (!$firstRun)
  {
     $strXML .= "<set name='".$row[0]."' value='".$difference."' color='AFD8F8' />";
   }
  $previous = $row[1];
  $firstRun = false;
}

Can anyone spot the issue in this code, its not erroring, its just giving wrong answers. 任何人都可以在此代码中发现问题,没有错误,只是给出了错误的答案。

EDIT: 编辑:

To clear up any confusion, this is the SQL: 为了消除任何混乱,这是SQL:

 --------------------------------------------------------

--
-- Table structure for table `mash`
--

CREATE TABLE IF NOT EXISTS `mash` (
  `id` int(25) NOT NULL AUTO_INCREMENT,
  `steam` int(25) NOT NULL,
  `bore_water` int(25) NOT NULL,
  `boiler1oil` int(25) NOT NULL,
  `boiler2oil` int(25) NOT NULL,
  `tstamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5362 ;

--
-- Dumping data for table `mash`
--

INSERT INTO `mash` (`id`, `steam`, `bore_water`, `boiler1oil`, `boiler2oil`, `tstamp`) VALUES
(2, 436, 73, 15, 1, '2010-11-25 12:28:03'),
(3, 495, 74, 36, 1, '2010-11-25 12:38:04'),
(4, 553, 76, 58, 1, '2010-11-25 12:48:09'),
(5, 565, 77, 74, 1, '2010-11-25 12:58:05'),
(6, 584, 79, 78, 1, '2010-11-25 13:08:05'),
(7, 630, 82, 100, 1, '2010-11-25 13:18:11'),
(8, 686, 86, 130, 1, '2010-11-25 13:28:07'),
(9, 740, 89, 151, 1, '2010-11-25 13:38:07'),
(10, 780, 93, 173, 1, '2010-11-25 13:48:13'),
(11, 883, 100, 218, 1, '2010-11-25 14:08:10');

Assuming I'm reading your table correctly, you're looking for the last entry of "value1" (or value2, etc.) which in-effect is the sum (judging by your mention of accumulative values) 假设我正确地读取了您的表,您正在寻找“ value1”(或value2等)的最后一个条目,其影响是总和(根据您提到的累积值来判断)

+----+------+---------------------+
| id | val  | tstamp              |
+----+------+---------------------+
|  6 |    1 | 2010-01-02 01:00:00 |
|  7 |    4 | 2010-01-02 02:00:00 |
|  8 |    6 | 2010-01-02 03:00:00 |
|  9 |   15 | 2010-01-02 04:00:00 |
| 10 |   20 | 2010-01-02 05:00:00 | <-- this value
| 11 |    1 | 2010-01-03 01:00:00 |
| 12 |    4 | 2010-01-03 02:00:00 |
| 13 |    6 | 2010-01-03 03:00:00 |
| 14 |   15 | 2010-01-03 04:00:00 |
| 15 |   20 | 2010-01-03 05:00:00 | <- this value
|  1 |    2 | 2010-02-01 01:00:00 |
|  2 |    8 | 2010-02-01 02:00:00 |
|  3 |   16 | 2010-02-01 03:00:00 |
|  4 |   32 | 2010-02-01 04:00:00 |
|  5 |   64 | 2010-02-01 05:00:00 | <- this value
+----+------+---------------------+

Then you should be able to use MAX&DATE: 然后,您应该可以使用MAX&DATE:

SELECT    DATE(tstamp), MAX(val)
FROM      mash
GROUP     DATE(tstamp)
ORDER BY  tstamp ASC;

Which will produce: 会产生:

+--------------+----------+
| DATE(tstamp) | MAX(val) |
+--------------+----------+
| 2010-01-02   |       20 |
| 2010-01-03   |       20 |
| 2010-02-01   |       64 |
+--------------+----------+

If you are grouping by the date, it's going to group by the smallest increment of the date. 如果按日期分组,则将按日期的最小增量分组。 You should be grouping by the day of the date. 您应该按日期进行分组。 Something like: 就像是:

select DATE_FORMAT(tstamp, '%Y-%m-%c') as ymd, sum(".$column.") from mash group by ymd order by tstamp asc limit 10

Which will give you a year-month-date format that it'll group on. 这将为您提供一个年月日格式,并将其分组。 If you need access to the year month and day values individually do: 如果您需要分别访问年月日值,请执行以下操作:

select YEAR(tstamp) as year, MONTH(tstamp) as month, DAY(tstamp) as day, sum(".$column.") from mash group by year, month, day order by tstamp asc limit 10

The key part of your question is not the GROUP BY portion, that will work just fine, it is what you mean by accumulative. 您问题的关键部分不是GROUP BY部分,它可以很好地工作,这就是您所指的累积性。

If you mean that a certain column tracks the total amount of rain in a year for each day (ie, the number keep going up because the delta between two records is the actual rain that occurred on that day) then what you need to do is first get rid of the delta. 如果您是说某列跟踪一年中一年的总降雨量(即,该数字持续上升,因为两条记录之间的差额是当天的实际降雨量),那么您需要做的是首先摆脱三角洲。 The SQL is going to be a bit hairy no matter how you do it, so you're probably better off doing it in PHP, but here is me trying with SQL. 无论您如何执行,SQL都会有些毛病,因此您最好在PHP中进行操作,但这是我尝试使用SQL的原因。

create temporary table foo select * from bar;

create temporary table non_accumulative
select b.id, b.rain - f.rain as "rain", rain_timestamp from bar b join foo f on (f.id + 1) = b.id;

select sum(rain), date(rain_timestamp) from non_accumulative group by date(rain_timestamp) order by rain_timestamp;

Of course you can do the same with sub selects, but I find the temporary tables are faster. 当然,您可以对子选择执行相同的操作,但是我发现临时表更快。

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

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