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 |
|
Lemmy44
Starting Member
20 Posts |
Posted - 2011-06-29 : 10:21:24
|
| OK, so this is one of those deals that seems simple in my mind, but for the life of me I cannot make this work.What I have so far, is a query that pulls out client names, their case open date, and the date of contact, based on a few different criteria.What I need to do, is list only those clients, whose LAST contact was this certain contact type (Wait List Letter). I've tried using the MAX fucntion, but I just can't get it to return any values (I have confirmed that there are several clients that should be showing up).Here is what I have so far:SELECT distinct person.last_name + ', ' + person.first_name, cases.open_date, contact.contact_dateFROM personINNER JOIN collection_date ONperson.person_id = collection_date.parent_idAND collection_date.child_id = 'CLIENTROLE'INNER JOIN participant_collection ONperson.person_id = participant_collection.child_idINNER JOIN contact ONparticipant_collection.parent_id = contact.contact_idAND contact.contact_type_id = '_L050001AB' /* Wait List Letter */INNER JOIN response_choice ONcontact.contact_id = response_choice.parent_idAND response_choice.question_id = '399L000017' /* Letter Type */AND response_choice.answer_id IN ('399L000019', '399L00001A') /* Reminder, Still Waiting */INNER JOIN cases ONperson.person_id = cases.parent_idAND cases.close_date IS NULLINNER JOIN response_choice AS response_choice1 ON cases.case_id = response_choice1.parent_id AND response_choice1.question_id = 'QFJD0000BR' /* Case Status */AND response_choice1.answer_id = 'QFJD0000ON' /* Waiting */INNER JOIN case_type ONcases.case_type_id=case_type.case_type_idAND (case_type.case_type_name LIKE 'YF%'OR case_type.case_type_name LIKE 'EY%'OR case_type.case_type_name LIKE 'ACT%'OR case_type.case_type_name LIKE 'AO%'OR case_type.case_type_name LIKE 'Day%'OR case_type.case_type_name LIKE '%Triple%') WHERE person.person_id NOT IN(SELECT person.person_id FROM question AS question6, response_choice AS response_choice6, cases AS cases6, answer AS answer6 WHERE person.person_id=cases6.parent_id AND question6.question_name='Case Status' AND response_choice6.question_id=question6.question_id AND response_choice6.parent_id=cases6.case_id AND response_choice6.answer_id=answer6.answer_id AND answer6.answer_name='active')AND DATEDIFF(dd, contact.contact_date, GETDATE()) > 90Any suggestions would be appreciated.Cheers. |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2011-06-29 : 10:30:50
|
| You can incorporate the RANK function, add something like this to your existing code,rank() over(partition by person.firstName,person.lastName order by contactDate desc) as Rowand then wrap your current code in another selectSELECT *FROM(<your current code + the ranking function>) twhere row = 1 and contactType = 'Wait List Letter'JimEveryday I learn something that somebody else already knew |
 |
|
|
|
|
|
|
|