I have a table : tbl_uesr
admission discharge name unit
2017-05-07 2017-05-10 Deepu 100
2017-05-07 2017-05-09 Nisanth 100
2017-05-08 2017-05-10 Ragesh 100
2017-05-15 2017-05-20 Vidhu 100
2017-05-15 2017-05-21 Reshma 100
2017-05-15 2017-05-21 Sneha 104
2017-06-01 2017-06-10 Lekshmi 104
2017-06-03 2017-06-10 Nidhin 104
2017-06-17 2017-06-20 Vineesh 104
2017-05-07 2017-05-08 Rithun 107
The above table describes a particular user's admission date, discharge date and its unit. From the above table I have to find, for a given date and unit how many admission happened, how many discharge happened.
For example on 2017-05-07, there is 2 admission in unit 100, 1 admission in unit 107, no discharge in any unit.
So in my result I want all dates(admission date and discharge date), number of admission, number of discharge
Expected result
Date no_admission no_discharge unit
2017-05-07 2 0 100
2017-05-07 1 0 107
2017-05-08 1 0 100
2017-05-08 0 1 107
2017-05-15 2 0 100
2017-05-15 1 0 104
2017-05-09 0 1 100
2017-05-10 0 3 100
2017-05-20 0 1 100
2017-05-21 0 1 100
2017-05-21 0 1 104
2017-06-01 1 0 104
2017-06-03 1 0 104
2017-06-17 1 0 104
2017-06-10 0 2 104
2017-06-20 0 1 104
What I have tried:
I have no idea to take all dates to a new table(result) as above. I tried to find no_admission by:
SELECT admission as date, count(name) as no_admission, unit FROMtbl_user GROUP BY admission
And tried to find the no_discharge by:
SELECT discharge as date, count(name) as no_discharge, unit FROMtbl_user GROUP BY discharge
But still am unable to find my result as expected..
Please help me to find the expected result..