skip to Main Content

I have 2 subqueries which give me a result consisting of dates.
I would like to calculate the difference between them.

For example:

SELECT DATEDIFF(
  DAY,
  (SELECT MAX(invoices.date) FROM invoices WHERE invoices.orderno = odH.orderno),
  (SELECT min(history.date) FROM history WHERE history.orderno = odH.orderno)
) ...

2

Answers


  1. To calculate the difference between two dates in Postgres, you can simply subtract one date from the other. Here’s how you can modify your query:

    SELECT (SELECT MAX(INVOICE.date) FROM INVOICES WHERE INVOICES.orderno = odH.orderno) - (SELECT min(history.date) FROM history WHERE history.orderno = ODH.orderno)
    

    This will give you the difference in days between the maximum INVOICE date and the minimum history date for a given order number.

    Login or Signup to reply.
  2. There is no DATEDIFF in Postgres. You can simplify your query to:

    SELECT 
        MAX(invoices.date) - MIN(invoices.date) 
    FROM 
        invoices 
    WHERE 
        invoices.orderno = odH.orderno
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search