Search code examples
sqldata-sciencedata-analysispresto

Trouble with Syntax format for Datediff - SQL


I have a syntax formatting issue with the query below. I am trying to get the difference between two time columns and then subtract 20 to get whatever the difference is minus 20. I also want to take the max value of either that or 0 so anything less than 0 will be 0.

select id, sum(max(0, (date_diff('minute', time_a, time_b))  - 20)) as mins
    FROM tbl

What am doing wrong in the query above that is erorring out?

Thanks!


Solution

  • sum(max()) is highly suspicious. Perhaps you intend:

    select id, sum(greatest(0, date_diff('minute', time_a, time_b) - 20)) as mins
    from tbl