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 |
|
rdoghanna
Starting Member
6 Posts |
Posted - 2009-04-21 : 12:41:00
|
Hi all, I do really well with simple select statements... but the more complicated ones make me buggy still. Since I've been trying without success for 2 days, I could really use some help. Please be very specific as again... simple statments I do very well with... :) Thank you in advance.My project - I am trying to pull a count of SERVICETYPE from one table. Which I have working. BUT, my database includes 461 TEST clients - and over 1153 SERCVICETYPE records for those TEST clients - which skew my data. I have to have EXACT data - so I want to EXCLUDE any clients with last name like "test" Additionally, I want to display the description of the service type... which is pulled from a THIRD table. My tables: CLIENTSCLIENTPROGRESSNOTESERVICETYPEMy BASIC query - which returns count of ALL client service type: SELECT SERVICETYPEKEY, COUNT (*) FROM CLIENTPROGRESSNOTE WHERE CLIENTPROGRESSNOTE.VOIDDATE IS NULL AND CLIENTPROGRESSNOTE.NOSHOW='0' AND (CLIENTPROGRESSNOTE.SERVICEDATE>='2009-01-01 00:00:00.000' AND CLIENTPROGRESSNOTE.SERVICEDATE<='2009-03-31 00:00:00.000') GROUP BYCLIENTPROGRESSNOTE.SERVICETYPEKEYORDER BYCLIENTPROGRESSNOTE.SERVICETYPEKEYNow, with this... 1) I need to join to the CLIENTS table (CLIENTPROGRESSNOTE.CLIENTKEY=CLIENTS.ID) and filter CLIENTS.CLIENTLASTNAME LIKE 'TEST%'. AND, 2) Would like to attach to the SERVICETYPE table (CLIENTPROGRESSNOTE.SERVICETYPEKEY=SERVICETYPE.ID) to display the TYPEOFSERVICE and DESCRIPTION fields. When I do anything with this now, it seems to exponentially be changing my COUNT ... which I know is clearly wrong! Right now my data - with TEST patients looks like this: SERVICETYPEKEY________COUNT56____________________3401758____________________1160____________________161____________________38What I would like it to be is: LESS any TEST PATIENT progressnotes being counted.DESCRIPTION_______SERVICETYPEKEY______COUNTServiceType1____________56____________34017ServiceType2____________58____________11ServiceType3____________60____________1ServiceType4____________61____________38I need it to connect to the SERVICETYPE table because I have more service types then are being used (and we're talking hundreds here)... and the SERVICETYPEKEY is the PK and is a unidentifiable number unless I go back to the SERVICETYPE table. I know it's got to be simple and I'm just not putting something in the right spot... please help!!!!!!!!!!Thank you! Laura  |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-04-21 : 13:03:58
|
I would like to see how you JOINed in the serviceType table which resulted in exponentially high counts. This code assumes that there is one servicetype row per ClientProgressNote.servicetypekeyselect st.[description] ,cpn.servicetypekey ,count(*)from CLIENTPROGRESSNOTE cpninner join clients c on c.[id] = cpn.CLIENTKEYinner join serviceType st on st.[id] = cpn.servicetypekeywhere cpn.VOIDDATE IS NULLand cpn.NOSHOW='0' and cpn.SERVICEDATE>='2009-01-01 00:00:00.000'and cpn.SERVICEDATE<='2009-03-31 00:00:00.000'and c.ClientLastName not like 'test%'GROUP BY st.[description] ,cpn.servicetypekey Be One with the OptimizerTG |
 |
|
|
rdoghanna
Starting Member
6 Posts |
Posted - 2009-04-21 : 13:51:54
|
| TG - YOU'RE THE BEST! Thank you... that was EXACTLY what I was looking for! :) I don't remember the exact syntax I was using... but it was ugly! I had done something about embedding the entire select statement in another select statement. LOL... I guess... Live and learn! |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-04-21 : 14:31:06
|
>>TG - YOU'RE THE BEST! Thank you... that was EXACTLY what I was looking for! :) I so rarely get to hear a woman say that...thank YOU Be One with the OptimizerTG |
 |
|
|
|
|
|
|
|