Hi,
I am pretty new to Postgres and I want to migrate an old project of mine to this new datbase.
Mostly I am fine but I have one mySQL query which I cannot “translate” to PostgresSQL without any help.
So maybe one of you is able to help?
This is the mySQL Query
SELECT
@calc_days := CASE WHEN t.tool_purchasedate > $1
THEN DATEDIFF($2, t.tool_purchasedate) + 1
ELSE DATEDIFF($2, $1) + 1
END AS calc_days,
@used_days :=
IFNULL(
(SELECT SUM(DATEDIFF(CASE WHEN order_enddate <= $2
THEN order_enddate
ELSE $2
END,
CASE WHEN order_startdate >= $1
THEN order_startdate
ELSE $1
END) + 1)
FROM ge_ordertool
LEFT JOIN ge_order ON ordertool_order = order_id
WHERE ordertool_tool = t.tool_id AND order_enddate >= $1 AND order_startdate <= $1
),
0) AS used_days,
ROUND((@used_days * 100) / @calc_days) AS percentage
FROM ....
where $1 is the start date and $2 is the end date of the date interval I want to look at.
My problem is, that there seems to be no inline variable declare in Postgres. Otherwise I could solve the problem myself.
so how do you solve such problems in Postgres?