简体   繁体   English

如何在 MySQL 中以 30 分钟为间隔获取 SELECT 数据?

[英]How to SELECT data by interval of 30 minutes in MySQL?

I'm trying to select data from MySQL table by interval of 30 minutes where all values are summed, the data are type of payments and total per payment, the issue is that once i've got the data i get sometimes data twice for same time range like:我正在尝试每隔 30 分钟对 MySQL 表中的 select 数据进行求和,其中所有值相加,数据是付款类型和每次付款总额,问题是一旦我得到数据,我有时会获得两次相同的数据时间范围如:

在此处输入图像描述

As those are both 8:30:00 the should be summed but aren't..因为这两个时间都是 8:30:00,所以应该相加但不是..

My query looks like this:我的查询如下所示:

SELECT SUM(IF(TIPODOC_PA = 'SCONTRINO' OR TIPODOC_PA = 'FATTURA', (IMPORTOPAG_PA - RESTO_PA), 0)) AS TOTPAG, DESCRPAG_PA, RISCPAG_PA, FROM_UNIXTIME(ROUND(UNIX_TIMESTAMP(TIMESTAMP(DATA_PA, ORA_PA))/(30* 60)) * (30*60)) AS DATA FROM pagamenti WHERE TIPODOC_PA <> 'VE' AND DESCRPAG_PA <> 'PR' AND DESCRPAG_PA <> 'NR' AND DESCRPAG_PA <> 'FC' AND DATA_PA BETWEEN '2022-02-04' AND '2022-02-04' AND (NPV_PA, NCASSA_PA) IN ((0, 1)) GROUP BY DESCRPAG_PA, ( 4 * HOUR(TIMESTAMP(DATA_PA, ORA_PA)) + FLOOR( MINUTE(TIMESTAMP(DATA_PA, ORA_PA)) / 30 )) ORDER BY TIMESTAMP(DATA_PA, ORA_PA)

And the Create Schema:和创建模式:

DROP TABLE IF EXISTS `pagamenti`;
CREATE TABLE  `pagamenti` (
  `NPV_PA` int(11) NOT NULL default '0',
  `NCASSA_PA` int(11) NOT NULL default '0',
  `TIPODOC_PA` varchar(250) NOT NULL default '',
  `TIPOPAG_PA` varchar(250) NOT NULL default '',
  `DESCRPAG_PA` varchar(250) NOT NULL default '',
  `RISCPAG_PA` int(1) default '0',
  `DATA_PA` date NOT NULL default '2001-01-01',
  `ORA_PA` time default '00:00:00',
  `AZZ_PA` varchar(10) NOT NULL default '0000',
  `NSC_PA` int(11) NOT NULL default '0',
  `ID_PA` int(30) NOT NULL default '0',
  `IMPORTODOC_PA` float(10,3) default '0.000',
  `IMPORTOPAG_PA` float(10,3) default '0.000',
  `RESTO_PA` float(10,3) default '0.000',
  `OPERATORE_PA` int(10) default '0',
  `CODICEBUONO_PA` varchar(250) default '',
  `IDTICKET_PA` int(11) default '0',
  `TIPOOPER_PA` varchar(250) default '',
  PRIMARY KEY  USING BTREE (`NPV_PA`,`NCASSA_PA`,`TIPODOC_PA`,`DATA_PA`,`AZZ_PA`,`NSC_PA`,`ID_PA`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

The GROUP BY is on this expression: GROUP BY在这个表达式上:

( 4 * HOUR(TIMESTAMP(DATA_PA, ORA_PA)) + FLOOR( MINUTE(TIMESTAMP(DATA_PA, ORA_PA)) / 30 ))

but that expression is not returned in the SELECT list.但该表达式未在 SELECT 列表中返回。 What we see in the SELECT list is a different expression.我们在 SELECT 列表中看到的是不同的表达方式。

FROM_UNIXTIME(ROUND(UNIX_TIMESTAMP(TIMESTAMP(DATA_PA, ORA_PA))/(30* 60)) * (30*60))

We don't have a guarantee that those two expressions will return the same value.我们不能保证这两个表达式将返回相同的值。

I suspect that if we exercise those expressions we will find that they do NOT always return the same value.我怀疑如果我们练习这些表达式,我们会发现它们并不总是返回相同的值。 (One obvious tipoff is that one of the expressions uses FLOOR and the other uses ROUND . Consider: (一个明显的提示是其中一个表达式使用FLOOR而另一个使用ROUND 。考虑:

 SELECT FLOOR(1.6) AS _one , ROUND(1.6) AS _two 

Typically we include the expression(s) in the GROUP BY in the SELECT list, so we can see those values returned.通常我们在SELECT列表中的GROUP BY中包含表达式,因此我们可以看到返回的这些值。 If we do that, add that expression as a column in the SELECT list, we will observe differences.如果我们这样做,将该表达式作为列添加到 SELECT 列表中,我们将观察到差异。

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

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