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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With