Martine Sivertsen Nilsen Martine Sivertsen Nilsen - 5 months ago 8
PHP Question

Combine results of two SQL queries

I am stuck.
I want to combine the results of two queries into one resulttable with three columns, result1, resul2 and dateday, the queries are contradictory

SELECT COUNT( DISTINCT `cust` ) AS result1, DATE( `date` ) AS dateday
FROM `salg_test`
WHERE `saved` =0
AND `is_void` =0
GROUP BY dateday
ORDER BY dateday DESC

SELECT COUNT( DISTINCT `cust` ) AS result2, DATE( `date` ) AS dateday
FROM `salg_test`
WHERE `saved` =1
AND `is_void` =0
GROUP BY dateday
ORDER BY dateday DESC


I want both results to be grouped by and oredered by the dateday variable.

Thanks for your help!

Answer

Should be able to do something like this:

SELECT COUNT( DISTINCT CASE `saved` when 0 then `cust` else null end ) AS result1
, COUNT( DISTINCT CASE `saved` when 1 then `cust` else null end ) AS result2
, DATE( `date` ) AS dateday
FROM `salg_test`
WHERE `is_void` =0
GROUP BY dateday
ORDER BY dateday DESC