简体   繁体   中英

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:

在此处输入图像描述

As those are both 8:30:00 the should be summed but aren't..

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:

( 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. What we see in the SELECT list is a different expression.

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:

 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. If we do that, add that expression as a column in the SELECT list, we will observe differences.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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