I have problem in changing the query from MySQL to SQL Server. I tried to change it to ISNULL and IIF in SQL but still doesn't work. Any tips or help are welcome, thanks!
Question 1 :
MYSQL
SELECT i.item_name
, r.item_version
, SUM(IF(r.group_status=1,r.quantity,0)) AS `Approved`
, SUM(IF(r.group_status=2,r.quantity,0)) AS `NotApproved`
, SUM(r.quantity) AS `Total`
FROM requesters r
JOIN items i
ON i.item_id = r.item_id
WHERE r.group_status IN (1,2)
AND r.requested_date >= '$a'
AND r.requested_date <= '$b'
GROUP
BY i.item_name
, r.item_version
SQL Server
SELECT i.item_name
, r.item_version
, SUM(ISNULL(r.group_status=1,r.quantity,0)) AS `Approved`
, SUM(ISNULL(r.group_status=2,r.quantity,0)) AS `NotApproved`
, SUM(r.quantity) AS `Total`
FROM requesters r
JOIN items i
ON i.item_id = r.item_id
WHERE r.group_status IN (1,2)
AND r.requested_date >= '$a'
AND r.requested_date <= '$b'
GROUP
BY i.item_name
, r.item_version
It keep saying Incorrect syntax near ',' and still doesn't work.
Question 2 :
MYSQL
SELECT IFNULL(SUM(IF(endusers.price,endusers.price,0)),0) AS `totalprice`
FROM endusers
WHERE requester_code = '$req_code'
SQL Server
SELECT NULLIF(SUM(COALESCE(endusers.price,endusers.price,0)),0) AS 'totalprice'
FROM endusers
WHERE requester_code = '$req_code'
The problem when I use COALESCE in Question 2, it won't display '0' when there's no value in the table.