MySQL Forums
Forum List  »  Newbie

JOIN Usage
Posted by: Sean S
Date: May 03, 2005 08:50AM

I'm stumped...

I have two tables that look like this...

Table Name: Dispositions
+---------------+------+--------------------------+
| DispositionID | Code | Disposition |
+---------------+------+--------------------------+
| 1 | 278 | Strong |
| 2 | NULL | Refused Treat./Transp. |
| 3 | 272 | Highland |
| 4 | 273 | Lakeside |
| 5 | 275 | Park Ridge |
| 6 | 276 | RGH |
| 7 | 277 | St. Mary's |
| 9 | NULL | Nursing home |
| 10 | NULL | Other med. Facility |
| 11 | NULL | Residence |
| 12 | NULL | Transfer Care to Other |
| 13 | NULL | Call Cancelled |
| 14 | NULL | Standby |
| 15 | NULL | No patient found |
| 16 | NULL | Other |
| 17 | NULL | Police/Jail |
| 18 | NULL | Morgue |
| 19 | NULL | Provided Care with Other |
+---------------+------+--------------------------+

...and...

Table Name: Calls
+--------+------------+---------------+
| CallID | Date | dispositionID |
+--------+------------+---------------+
| 1856 | 2005-04-16 | 1 |
| 1857 | 2005-04-16 | 1 |
| 1858 | 2005-04-16 | 1 |
| 1859 | 2005-04-17 | 2 |
| 1860 | 2005-04-17 | 2 |
| 1862 | 2005-04-18 | 1 |
| 1863 | 2005-04-18 | 12 |
| 1864 | 2005-04-18 | 15 |
| 1865 | 2005-04-18 | 1 |
| 1866 | 2005-04-18 | 1 |
| 1867 | 2005-04-19 | 1 |
| 1868 | 2005-04-19 | 14 |
| 1869 | 2005-04-19 | 12 |
| 1870 | 2005-04-19 | 2 |
| 1871 | 2005-04-21 | 1 |
| 1872 | 2005-04-21 | 14 |
| 1873 | 2005-04-21 | 1 |
| 1874 | 2005-04-22 | 1 |
| 1875 | 2005-04-22 | 2 |
| 1876 | 2005-04-22 | 14 |
| 1877 | 2005-04-22 | 14 |
| 1878 | 2005-04-22 | 1 |
| 1879 | 2005-04-22 | 1 |
| 1880 | 2005-04-23 | 1 |
| 1881 | 2005-04-23 | 2 |
| 1882 | 2005-04-23 | 1 |
| 1883 | 2005-04-24 | 2 |
| 1884 | 2005-04-25 | 1 |
| 1885 | 2005-04-25 | 1 |
| 1886 | 2005-04-25 | 1 |
+--------+------------+---------------+

I want to write a SELECT statement that returns all of the dispositions and counts those that match. So I'd like to end up with something like...
Strong 15
Refused Treat./Transp. 10
Highland 2
Lakeside 0
Park Ridge 3
RGH 5
St. Mary's 0
Nursing home 0
Other med. Facility 0
Residence 0
Transfer Care to Other 6
Call Cancelled 6
Standby 10
No patient found 1
Other 1
Police/Jail 0
Morgue 0
Provided Care with Other 0

I have come up with this,
SELECT
Dispositions.Disposition,
Dispositions.dispositionID,
COUNT(Calls.dispositionID) AS DispositionCount
FROM Dispositions
LEFT JOIN Calls ON Dispositions.dispositionID = Calls.dispositionID
WHERE Calls.date > '2005-03-31'
AND Calls.date < '2005-05-01'
AND ( Calls.ambulanceID IS NOT NULL
OR Calls.dispositionID = '2'
OR Calls.dispositionID = '12' )
GROUP BY Calls.dispositionID
ORDER BY Dispositions.Disposition

But that only provides,
+------------------------+---------------+------------------+
| Disposition | dispositionID | DispositionCount |
+------------------------+---------------+------------------+
| Call Cancelled | 13 | 3 |
| Highland | 3 | 3 |
| Other | 16 | 1 |
| Refused Treat./Transp. | 2 | 21 |
| Standby | 14 | 7 |
| Strong | 1 | 27 |
| Transfer Care to Other | 12 | 3 |
+------------------------+---------------+------------------+

Any help would be greatly appreciated. Thanks.
-Sean

Options: ReplyQuote


Subject
Written By
Posted
JOIN Usage
May 03, 2005 08:50AM
May 03, 2005 11:57AM
May 03, 2005 12:06PM
May 03, 2005 12:16PM
May 03, 2005 12:20PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.