1

I have one query result as:

Wname    NewCases
Mon       2
Tues      1
Sat       3

Second query result as:

Wname     FollowUp
Tues         3
Wed          4
Friday       2

I need to write mysql query to get result as:

Wname   NewCases   FollowUp
Mon       2          0
Tues      1          3
Wed       0          4
Fri       0          2
Sat       3          0

my 1st query:

Select   substring(dayname(Dated),1,3) as wname,
         count(RegId) as NewCases 
from     patienttable 
where    Patient_Type='New' 
     and week(Dated)=week(now()) 
group by dayname(Dated) 
order by Dated

2nd query:

Select   substring(dayname(Dated),1,3) as wname,
         count(RegId) as FollowUp
from     patienttable
where    Patient_Type='FollowUp' 
     and week(Dated)=week(now()) 
group by dayname(Dated)
order by Dated
0

1 Answer 1

-1

There is no need to join the queries.
You can do it in one query with conditional aggregation:

SELECT SUBSTRING(DAYNAME(Dated), 1, 3) AS wname,
       SUM(Patient_Type = 'New') AS NewCases,
       SUM(Patient_Type = 'FollowUp') AS FollowUp  
FROM patienttable 
WHERE Patient_Type IN ('New', 'FollowUp') AND WEEK(Dated) = WEEK(NOW()) 
GROUP BY DAYNAME(Dated);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.