Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
mattech13
Starting Member
4 Posts |
Posted - 2013-09-08 : 09:04:15
|
Hi there,fairly new to sql. Here's essentially what I'm trying to achieve...select Doctor, count(monthyear)- count(paidinfull) as Patientsfrom tableAwhere paidinfull = 'YES'group by doctorSo I have a table like Doctors Patients------ --------Burns 12White 7where Doctors is a group by from tableA and Patients is a result of the row count of paidinfull = yes minus the rowcount of monthyearAny help on the actual sql to get this?thanks, |
|
bitsmed
Aged Yak Warrior
545 Posts |
Posted - 2013-09-08 : 10:47:33
|
I think this might work for you.This will give you the results per doctor/month:select doctor ,monthyear ,count(*)-sum(case when paidinfull='YES' then 1 else 0 end) as Patients from tableA group by doctor ,monthyear This will give you the results per doctor/total:select doctor ,count(*)-sum(case when paidinfull='YES' then 1 else 0 end) as Patients from tableA group by doctor |
|
|
mattech13
Starting Member
4 Posts |
Posted - 2013-09-08 : 11:21:45
|
Hi bitsmed,I'm not entirely sure this solves my problem.If in tableA there are 100 rows for doctor White. All rows for doctorWhite have a monthyear field so the rowcount for monthyear would be 100. 20 rows have YES in the paidinfull column, so a row count for that would be 20.I'm looking to display the result of this asDoctors Patients------- --------White 80where patients is monthyear row count - paidinfull (where it equals = YES)rowcountthanks, |
|
|
bitsmed
Aged Yak Warrior
545 Posts |
Posted - 2013-09-08 : 11:30:33
|
Why don't you give it a try?In your latest scenario, count(*) would become 100, and the sum(...) would become 20. Subtracting those two, and you'll have your result (80).quote: Originally posted by mattech13 Hi bitsmed,I'm not entirely sure this solves my problem.If in tableA there are 100 rows for doctor White. All rows for doctorWhite have a monthyear field so the rowcount for monthyear would be 100. 20 rows have YES in the paidinfull column, so a row count for that would be 20.I'm looking to display the result of this asDoctors Patients------- --------White 80where patients is monthyear row count - paidinfull (where it equals = YES)rowcountthanks,
|
|
|
mattech13
Starting Member
4 Posts |
Posted - 2013-09-15 : 07:55:37
|
yep that's great, thanks for that. How do I mark this as closed? |
|
|
bitsmed
Aged Yak Warrior
545 Posts |
Posted - 2013-09-15 : 11:12:25
|
There's really no way to close this thread, but to change subject to:(Resolved) Date comparison problem |
|
|
|
|
|
|
|