How can I get the latest registration by date and time

2

I would like to obtain the last value registered in a table, there are two columns in my table DATE and TIME. How can I consult the database so that I can return, for example, the data '8' of the value column?

  

Structure of my table

id_log_influ    id_usuario  valor     FECHA             TIEMPO
    1               2         6      2018-09-24      09:43:03.0000000
    1               4         8      2018-09-24      10:52:03.0000000
  

Data type

valor --> varchar
FECHA --> date
TIEMPO --> time
    
asked by runjavcos 26.09.2018 в 02:13
source

4 answers

2

If you are looking for the last Valor of a record of a given date depending on the field Fecha and Tiempo .

What you must do is select the Valor field of the first record, sorted by the Tiempo field in a descending manner, that complies with the condition where the Fecha field is what you indicate, in our example 2018-09-24 .

--id_log_influ    id_usuario  valor     FECHA             TIEMPO
--    1               2         6      2018-09-24      09:43:03.0000000
--    1               4         8      2018-09-24      10:52:03.0000000

CREATE TABLE #TABLATEMPORAL (
    ID_LOG_INFLU    INT,
    ID_USUARIO      INT,
    VALOR           INT,
    FECHA           DATE,
    TIEMPO          TIME
);

INSERT INTO #TABLATEMPORAL
VALUES  (1, 2, 6, '2018-09-24', '09:43:03'), 
        (1, 4, 8, '2018-09-24', '10:52:03'),
        (2, 2, 5, '2018-09-26', '12:52:03');

SELECT TOP(1) VALOR
FROM #TABLATEMPORAL
WHERE FECHA = '2018-09-24'  --AQUI USA UNA VARIABLE Y MANDARIAS LA FECHA
ORDER BY TIEMPO DESC

DROP TABLE #TABLATEMPORAL;

If on the other hand you only want the last Valor registered from your table, just use a simple SELECT in this way, adding the field Fecha and the field Tiempo to then order it in a descending way:

--id_log_influ    id_usuario  valor     FECHA             TIEMPO
--    1               2         6      2018-09-24      09:43:03.0000000
--    1               4         8      2018-09-24      10:52:03.0000000

CREATE TABLE #TABLATEMPORAL (
    ID_LOG_INFLU    INT,
    ID_USUARIO      INT,
    VALOR           INT,
    FECHA           DATE,
    TIEMPO          TIME
);

INSERT INTO #TABLATEMPORAL
VALUES  (1, 2, 6, '2018-09-24', '09:43:03'), 
        (1, 4, 8, '2018-09-24', '10:52:03'),
        (2, 2, 5, '2018-09-26', '12:52:03');

SELECT TOP(1) VALOR
FROM #TABLATEMPORAL
ORDER BY CAST(FECHA AS DATETIME) + CAST(TIEMPO AS DATETIME) DESC

DROP TABLE #TABLATEMPORAL;
    
answered by 26.09.2018 / 17:07
source
3
SELECT max(CONCAT(FECHA, ,TIEMPO)) from tabla ;

This way you can possibly serve, what we do is concatenate the two fields and get the most value out of that union.

I hope it works.

    
answered by 26.09.2018 в 02:44
2

This would be the query (I apologized because I had not seen that you use sql server)

 SELECT valor, CONCAT(fecha, tiempo) as mayor 
 from registro 
 ORDER BY valor
 OFFSET 1 ROWS;

Take into account that according to your question you are interested in showing the record of the value column you must write it explicitly in your query so that at the end it only shows you: valor tiempo mas grande(max)

  

In addition to that, the CONCAT function will be used to join several values   separated by commas, as in your statement you indicate that you want to obtain   the highest value per fecha and tiempo , that's why this   function, I attach an empty space in between single quotes   only so that at the time the query is displayed, it is not   see the names of the columns pasted

    
answered by 26.09.2018 в 02:49
0

If you always want to get the last value of your table you can do the following In your table your idimcrementable we will put it that is value for the example

declare @ultimovalor
set @ultimovalor =(select max(valor) from tabla)

select * from tabla where valor=@ultimovalor

With this script, the last record of your table will always appear

I hope I helped you. !!

    
answered by 26.09.2018 в 19:44