Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when taking the min between two dates

Tags:

sql

postgresql

DROP TABLE IF EXISTS tmp1_variables;
CREATE TEMPORARY TABLE tmp1_variables AS (
SELECT
    '2016-10-29'::date as start_date,
'2017-01-28'::date as end_date2,
dateadd(day,-13,getdate())::date as end_date);


SELECT cobrand_id, sum(calc) AS a, sum(count) AS b FROM jwn_calc s, tmp1_variables
        where s.optimized_transaction_date > start_date
AND s.optimized_transaction_date <= min(end_date,end_date2) 

I'm specifically getting the error at the min(end_date,end_date2), with error:

[42883] ERROR: function min(date, date) does not exist Hint: No function matches the given name and argument types. You may need to add explicit type casts.

like image 820
ZJAY Avatar asked Jul 18 '26 17:07

ZJAY


1 Answers

Use LEAST() instead of MIN():

SELECT cobrand_id,
       SUM(calc) AS a,
       SUM(count) AS b
FROM jwn_calc s
INNER JOIN tmp1_variables t
    ON s.optimized_transaction_date > t.start_date AND
       s.optimized_transaction_date <= LEAST(t.end_date, t.end_date2)

Note that I have replaced your implicit join with an explicit INNER JOIN. As a general rule, you should avoid using commas in the FROM clause.

Read here for more information on Postgres' LEAST() function

like image 176
Tim Biegeleisen Avatar answered Jul 20 '26 06:07

Tim Biegeleisen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!