2

I need the time difference between two datestamps in hours and need to arrange them in descending order to get the median

SELECT "fulfillmentshistories"."fulfillmentid" as "Fulfillment ID" FROM FULFILLMENTHISTORIES
WHERE (Fulfillments."status" IN ('paid','delivered') AND (DATE("fulfillmenthistories"."createdAt") BETWEEN DATE('{{startDate}}') AND DATE('{{endDate}}')))
union
SELECT DATEDIFF(HOUR,   'startDate'-'endDate') as DATEDIFF;
ORDER BY DATEDIFF, DESC

This is what I tried please help

SELECT "fulfillmentshistories"."fulfillmentid" as "Fulfillment ID" FROM    FULFILLMENTHISTORIES
WHERE (Fulfillments."status" IN ('paid','delivered') AND (DATE ("fulfillmenthistories"."createdAt") BETWEEN DATE('{{startDate}}') AND DATE('{{endDate}}')))
union
SELECT DATEDIFF(HOUR,   'startDate'-'endDate') as DATEDIFF;
ORDER BY DATEDIFF, DESC

Error running query: syntax error at or near "ORDER" LINE 5: ORDER BY DATEDIFF, DESC ^

Arulkumar
  • 12,153
  • 12
  • 44
  • 61

2 Answers2

0
SELECT DATEDIFF(HOUR, 'startDate'-'endDate') as DATEDIFF; ORDER BY DATEDIFF, DESC

should be

SELECT DATEDIFF(HOUR, 'startDate'-'endDate') as DATEDIFF ORDER BY DATEDIFF, DESC ;

and try avoiding alias names similar to the function name.

saurabh
  • 2,098
  • 1
  • 16
  • 25
0
UNION
SELECT DATEDIFF(HOUR,   'startDate'-'endDate') as DATEDIFF;
ORDER BY DATEDIFF, DESC

should be

UNION
SELECT DATEDIFF(HOUR,   'startDate'-'endDate') as DATEDIFF
ORDER BY DATEDIFF DESC;

Remove the semicolon after DATEDIFF and the comma after ORDER BY DATEDIFF. The semicolon should come at the end of the SQL statement since the whole of that is one sql statement.

M. Arnold
  • 186
  • 1
  • 8