Necesito imprimir la variable en el conjunto de resultados

Necesito poder convertir una variable de entrada en una fecha y hora o en una marca de tiempo, y luego enviarla nuevamente al conjunto de resultados como parte de la declaración de selección. He preparado el código a continuación, que por supuesto no funciona; me envía desde 1970-01-01 independientemente de la hora de Unix. Estas son las piezas que intento enviar al conjunto de resultados:

FROM_UNIXTIME (fecha de inicio, "% Y-% m-% d") AS fecha de inicio,     FROM_UNIXTIME (endDate, "% Y-% m-% d") COMO endingDate

CREATE PROCEDURE reportFreeCoolingTracker (
        IN fromDate varchar (50),
        IN toDate varchar (50),
        IN timeZone varchar (50))

    BEGIN
            DECLARE startDate varchar (50);
            DECLARE endDate varchar (50);


             SET startDate = FROM_UNIXTIME(fromDate/1000);
             SET endDate = FROM_UNIXTIME(toDate/1000);

 SELECT g.groupId,
    g.name AS groupName,
    g1.parentId AS parentId1, 
    g1.name AS group1Name,
    g2.parentId AS parentId2, 
    g2.name AS group2Name,
    g3.parentId AS parentId3,
    g3.name AS group3Name,
    l.logId,l.name AS logName,
    l.ordering AS logOrder,
    a.activityId AS activityId,
    a.ordering AS activityOrder,
    a.name AS activityName,
    l1.recordId,
    l1.started, l1.completed,
    l1.userId,l1.note,
    u.name,
    TO_SECONDS(t2.completed) - TO_SECONDS(l1.completed) AS timeInSeconds,
    substr(l.details, instr(l.details , ':' ) +1)AS charge,l.details,
    i.itemId,
    i.name AS itemName,
    i.itemType,
    i.details,l1.item31985,l1.item31987,
    (l1.item31985 - l1.item31987) AS kwDifference,
    ((l1.item31985 - l1.item31987) *  (substr(l.details, instr(l.details , ':' ) +1))) AS cost,
    (((l1.item31985 - l1.item31987) *  (substr(l.details, instr(l.details , ':' ) +1)))
    *(time_to_sec(timediff(t2.completed, l1.completed))/3600)) AS costT,
      time_to_sec(timediff(t2.completed, l1.completed))/3600 AS coolingHours,
      time_to_sec(timediff(endDate, startDate))/3600 AS totalTimeRange,
    FROM_UNIXTIME (fecha de inicio, "% Y-% m-% d") AS fecha de inicio,
    FROM_UNIXTIME (endDate, "% Y-% m-% d") COMO endingDate

  FROM logs l 
      INNER JOIN groups g ON g.groupId = l.groupId
      LEFT JOIN groups g1 ON g.parentId = g1.groupId
      LEFT JOIN groups g2 ON g1.parentId = g2.groupId
      LEFT JOIN groups g3 ON g2.parentId = g3.groupId                
      INNER JOIN activities a ON l.logId = a.logId 
      INNER JOIN log1644 l1 ON a.activityId = l1.activityId 
      INNER JOIN log1644 t2 ON t2.recordId = l1.recordid + 1
      INNER JOIN items i ON l.logId = i.logId AND i.name LIKE '## KW%'
      INNER JOIN users u ON l1.userId = u.userId AND i.name LIKE '## KW%'
     WHERE i.itemID = "31985"  AND  l1.activityId = 1257
      AND l1.started
        BETWEEN startDate
            AND endDate
     ORDER BY l1.started;
    END //

DELIMITER ;
0

2 Respuestas

Esto realmente funciona, de hecho, la consulta original funcionó. Lo estaba agregando a la base de datos incorrecta.

(CONVERT_TZ ((FROM_UNIXTIME (fromDate/1000)), 'UTC', timeZone)) COMO fecha de inicio,     (CONVERT_TZ ((FROM_UNIXTIME (toDate/1000)), 'UTC', timeZone)) COMO endingDate,

0
agregado

Tienes que eliminar

SET startDate = FROM_UNIXTIME(fromDate/1000);
SET endDate = FROM_UNIXTIME(toDate/1000);

porque la marca de tiempo de Unix funciona con milisegundos y se usa en su SELECCIÓN como

FROM_UNIXTIME(fromDate,"%Y-%m-%d") AS startingDate, 
FROM_UNIXTIME(toDate,"%Y-%m-%d") AS endingDate
0
agregado