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;