
February 4th, 2012, 10:35 PM
|
|
Registered User
|
|
Join Date: Apr 2011
Posts: 11
Time spent in forums: 2 h 46 m 47 sec
Reputation Power: 0
|
|
|
Using Left Outer Join
Hello, thanks for taking the time to read my post. This query was written for me by someone who I cannot get in contact with anymore and I need to alter it. Unfortunately, I do not understand the language well enough. Any help would be appreciated. The query I have totals the number of sessions each client has had in the past 2 months, and subtracts that from the number of sessions each client has paid for to give their total remaining sessions. It lists all clients in the database and how many sessions they have remaining.
SELECT app.demographic_no,app.name,demo.sessions_paid,COALESCE(app.sessions_used,0) AS sessions_used,(demo.sessions_paid – COALESCE(app.sessions_used,0)) AS sessions_remaining FROM (SELECT demographic_no,COUNT(*) AS sessions_used FROM appointment WHERE (appointment_date BETWEEN ‘2012-01-01’ AND ‘2012-02-03’) GROUP BY demographic_no) AS app LEFT OUTER JOIN (SELECT demographic_no,sessions_paid FROM demographic GROUP BY demographic_no) AS demo ON app.demographic_no = demo.demographic_no ORDER BY sessions_remaining desc
What I need is a query that will take ONLY the clients coming TODAY, and subtract all the sessions they have had to date (the day before today) from the sessions they have paid for. Thank you for any input you can offer.
Alexandra
|