How to return date and time closer to the one informed by the user

2

I have a table with a list of places where a truck passed with the date / time of its entry and exit

+----+------------------+-----------+---------------+
| ID |   DATA E HORA    |   LOCAL   | ENTRADA/SAIDA |
+----+------------------+-----------+---------------+
|  1 | 28/01/2017 07:27 | FABRICA   | Entrada       |
|  2 | 28/01/2017 09:00 | FABRICA   | Saida         |
|  3 | 29/01/2017 00:02 | REVENDA 1 | Entrada       |
|  4 | 29/01/2017 04:00 | REVENDA 1 | Saida         |
|  5 | 29/01/2017 08:00 | REVENDA 2 | Entrada       |
|  6 | 29/01/2017 10:00 | REVENDA 2 | Saida         |
|  7 | 29/01/2017 20:00 | FABRICA   | Entrada       |
|  8 | 29/01/2017 23:00 | FABRICA   | Saida         |
+----+------------------+-----------+---------------+

I need to make a QUERY that with a user-entered date / time, returns where the truck was at the moment.

Is there a function that returns the nearest date / time occurrence from one reported in QUERY? Is there a more efficient way to do it? As? Thanks for the help

    
asked by anonymous 01.02.2017 / 02:42

4 answers

2

You can sort the dates, filter dates before the date entered, and limit the result to a record. As you have not passed actual information, such as table name and fields, I can not write the exact SQL. But it will be something like this:

SELECT LOCAL FROM _TABELA WHERE DATA_FORNECIDA > DATA_E_HORA ORDER BY DATA_E_HORA LIMITE 1
    
01.02.2017 / 03:31
1

I think this might help you:

    SELECT max(HORADOCAMINHAO), COD_CAMINHAO 
    FROM tabela
    WHERE
    DAT_SOLICITACAO < '2016-10-31 06:36:01.000'/*informe a data desejada*/ 
    and COD_CAMINHAO = 123
    GROUP BY COD_CAMINHAO

/*troque o que está dentro do() pela coluna que 
    informa a data, e informe as demais colunas desejadas 
    no GROUP BY */ 
    
01.02.2017 / 11:35
1

There is no INTERSECT for MySQL and you needed to emulate using UNION ALL. This SQL at first glance seems weird, but it's not complicated, and goes through all the tests, finding the correct location for each time . It follows mass for the test, the logic and the tests themselves.

First a table was created that reproduces the one that was posted in the question.

CREATE TABLE 'LISTA_LOCAIS' (
  'ID' int(11) NOT NULL,
  'DATA_HORA' timestamp NOT NULL,
  'LOCAL' varchar(100) NOT NULL,
  'ENTRADA_SAIDA' varchar(10) NOT NULL
)

Then we create a test mass identical to the one shown in the question.

INSERT INTO LISTA_LOCAIS(ID,DATA_HORA,LOCAL,ENTRADA_SAIDA) VALUES(1,'2017-01-28 07:27','FABRICA'   ,'Entrada');
INSERT INTO LISTA_LOCAIS(ID,DATA_HORA,LOCAL,ENTRADA_SAIDA) VALUES(2,'2017-01-28 09:00','FABRICA'   ,'Saida'  );
INSERT INTO LISTA_LOCAIS(ID,DATA_HORA,LOCAL,ENTRADA_SAIDA) VALUES(3,'2017-01-29 00:02','REVENDA 1' ,'Entrada');
INSERT INTO LISTA_LOCAIS(ID,DATA_HORA,LOCAL,ENTRADA_SAIDA) VALUES(4,'2017-01-29 04:00','REVENDA 1' ,'Saida'  );
INSERT INTO LISTA_LOCAIS(ID,DATA_HORA,LOCAL,ENTRADA_SAIDA) VALUES(5,'2017-01-29 08:00','REVENDA 2' ,'Entrada');
INSERT INTO LISTA_LOCAIS(ID,DATA_HORA,LOCAL,ENTRADA_SAIDA) VALUES(6,'2017-01-29 10:00','REVENDA 2' ,'Saida'  );
INSERT INTO LISTA_LOCAIS(ID,DATA_HORA,LOCAL,ENTRADA_SAIDA) VALUES(7,'2017-01-29 20:00','FABRICA'   ,'Entrada');
INSERT INTO LISTA_LOCAIS(ID,DATA_HORA,LOCAL,ENTRADA_SAIDA) VALUES(8,'2017-01-29 23:00','FABRICA'   ,'Saida'  );

Now SQL to search the location at certain times:

SELECT LOCAL from (
  (SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Entrada' AND 'SUA_TIMESTAMP_PESQUISADA' >= DATA_HORA ORDER BY DATA_HORA DESC LIMIT 2)
  UNION ALL 
  (SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Saida' AND 'SUA_TIMESTAMP_PESQUISADA' <= DATA_HORA ORDER BY DATA_HORA ASC LIMIT 2)
) AS TABELA_UNIFICADA  GROUP BY LOCAL HAVING count(*) >= 2

Explanation: First it takes a part that marries with the entrance. Then take a part that marries the exit. It intersects the two. Note the balcony of the descending order for the entrance and ascending order for the exit to limit the records of this intersection well.

Follow the tests:

-- TESTE 1:  REVENDA 1 em 2017-01-29 00:03:00
SELECT LOCAL from (
  (SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Entrada' AND '2017-01-29 00:03:00' >= DATA_HORA ORDER BY DATA_HORA DESC LIMIT 2)
  UNION ALL 
  (SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Saida' AND '2017-01-29 00:03:00' <= DATA_HORA ORDER BY DATA_HORA ASC LIMIT 2)
) AS TABELA_UNIFICADA  GROUP BY LOCAL HAVING count(*) >= 2


-- TESTE 2: FABRICA em 2017-01-28 08:00
SELECT LOCAL from (
  (SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Entrada' AND '2017-01-28 08:00' >= DATA_HORA ORDER BY DATA_HORA DESC LIMIT 2)
  UNION ALL 
  (SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Saida' AND '2017-01-28 08:00' <= DATA_HORA ORDER BY DATA_HORA ASC LIMIT 2)
) AS TABELA_UNIFICADA  GROUP BY LOCAL HAVING count(*) >= 2


-- TESTE 3: EM LUGAR NENHUM / CAMINHÃO EM ROTA em 2017-01-28 12:00 - não tem a entrada do local para esse período
SELECT LOCAL from (
  (SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Entrada' AND '2017-01-28 12:00' >= DATA_HORA ORDER BY DATA_HORA DESC LIMIT 2)
  UNION ALL 
  (SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Saida' AND '2017-01-28 12:00' <= DATA_HORA ORDER BY DATA_HORA ASC LIMIT 2)
) AS TABELA_UNIFICADA  GROUP BY LOCAL HAVING count(*) >= 2


-- TESTE 4: REVENDA 2 em 2017-01-29 08:00
SELECT LOCAL from (
  (SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Entrada' AND '2017-01-29 08:00' >= DATA_HORA ORDER BY DATA_HORA DESC LIMIT 2)
  UNION ALL 
  (SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Saida' AND '2017-01-29 08:00' <= DATA_HORA ORDER BY DATA_HORA ASC LIMIT 2)
) AS TABELA_UNIFICADA  GROUP BY LOCAL HAVING count(*) >= 2


-- TESTE 5: REVENDA 2 em 2017-01-29 09:00
SELECT LOCAL from (
  (SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Entrada' AND '2017-01-29 09:00' >= DATA_HORA ORDER BY DATA_HORA DESC LIMIT 2)
  UNION ALL 
  (SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Saida' AND '2017-01-29 09:00' <= DATA_HORA ORDER BY DATA_HORA ASC LIMIT 2)
) AS TABELA_UNIFICADA  GROUP BY LOCAL HAVING count(*) >= 2


-- TESTE 6: REVENDA 2 em 2017-01-29 10:00
SELECT LOCAL from (
  (SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Entrada' AND '2017-01-29 10:00' >= DATA_HORA ORDER BY DATA_HORA DESC LIMIT 2)
  UNION ALL 
  (SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Saida' AND '2017-01-29 10:00' <= DATA_HORA ORDER BY DATA_HORA ASC LIMIT 2)
) AS TABELA_UNIFICADA  GROUP BY LOCAL HAVING count(*) >= 2


-- TESTE 7: LUGAR NENHUM / CAMINHÃO EM ROTA em 2017-01-29 16:00
SELECT LOCAL from (
  (SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Entrada' AND '2017-01-29 16:00' >= DATA_HORA ORDER BY DATA_HORA DESC LIMIT 2)
  UNION ALL 
  (SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Saida' AND '2017-01-29 16:00' <= DATA_HORA ORDER BY DATA_HORA ASC LIMIT 2)
) AS TABELA_UNIFICADA  GROUP BY LOCAL HAVING count(*) >= 2


-- TESTE 8: FABRICA em 2017-01-29 20:00
SELECT LOCAL from (
  (SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Entrada' AND '2017-01-29 20:00' >= DATA_HORA ORDER BY DATA_HORA DESC LIMIT 2)
  UNION ALL 
  (SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Saida' AND '2017-01-29 20:00' <= DATA_HORA ORDER BY DATA_HORA ASC LIMIT 2)
) AS TABELA_UNIFICADA  GROUP BY LOCAL HAVING count(*) >= 2


-- TESTE 9: FABRICA em 2017-01-29 23:00
SELECT LOCAL from (
  (SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Entrada' AND '2017-01-29 23:00' >= DATA_HORA ORDER BY DATA_HORA DESC LIMIT 2)
  UNION ALL 
  (SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Saida' AND '2017-01-29 23:00' <= DATA_HORA ORDER BY DATA_HORA ASC LIMIT 2)
) AS TABELA_UNIFICADA  GROUP BY LOCAL HAVING count(*) >= 2


-- TESTE 10: LUGAR NENHUM  / CAMINHÃO EM ROTA  em 2017-01-29 23:10
SELECT LOCAL from (
  (SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Entrada' AND '2017-01-29 23:10' >= DATA_HORA ORDER BY DATA_HORA DESC LIMIT 2)
  UNION ALL 
  (SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Saida' AND '2017-01-29 23:10' <= DATA_HORA ORDER BY DATA_HORA ASC LIMIT 2)
) AS TABELA_UNIFICADA  GROUP BY LOCAL HAVING count(*) >= 2
    
01.02.2017 / 12:56
0

With this select I can select the closest location to that truck at any given time.

SELECT LOCAL, min( abs( UNIX_TIMESTAMP(DATA_HORA) -  UNIX_TIMESTAMP(STR_TO_DATE('29-01-2017 05:00', '%d-%m-%Y %H:%i:%s')))) AS PROVAVEL from lista_locais
GROUP BY LOCAL 
ORDER BY PROVAVEL LIMIT 1

At5o'clockon01/29/2017heisclosertoresale1.

SELECTLOCAL,min(abs(UNIX_TIMESTAMP(DATA_HORA)-UNIX_TIMESTAMP(STR_TO_DATE('29-01-201708:30','%d-%m-%Y%H:%i:%s'))))ASPROVAVELfromlista_locaisGROUPBYLOCALORDERBYPROVAVELLIMIT1

And at 8:30 the same day he is in resale 2.

    
01.02.2017 / 13:35