Mike Mike - 6 months ago 12
SQL Question

SQL Server 2012 Max Date in Subquery in a joined table

I manage a state wide application and use

Tableau
to create visualizations of data.

I have been tasked with creating a visualization that show how much time is passing between contact entries and today (Case Note Dates). I know how to isolate the max case note date in the case note table:

Select
[Case_Master_ID],
[Case_Note_Date],
[Case_Note_Category_Desc],
[Case_Note_Summary_Narr]
From
buCase_Note
Where
Case_Note_Date = (Select MAX(Case_Note_Date)
From buCase_Note)


This query will show me that max case notes in the table from today. The issue is I need to show the max case note for all participants, not just the ones from today. The original query I have been using to view case notes is:

Select
vc.[_Case Master ID],
vc.[_Caseload Assignment Current],
vc.[_Participant Name],
vc.[Case Status],
vc.[Reporting Structure Level 4],
vc.[Reporting Structure Level 5],
vc.[Application Date],
vc.[Eligibility Date],
vc.[Eligibility Determination Extension Date],
vc.[Eligibility Extended To Date],
vc.[Days in Application],
cn.[Case_Note_Date],
cn.[Case_Note_Category_Desc],
cn.[Case_Note_Summary_Narr]
From
biVR_Cases vc
Left outer Join
buCase_Note cn ON cn.Case_Master_ID = vc.[_Case Master ID]


I need to keep
biVR_Cases
on the left to show all the open clients. Then I need to join in the case note table and for every participant, I want to show their max case note date. When I add this to the end of the above query:

Where cn.[Case_Note_Date] = (
Select
MAX(cn.Case_Note_Date)
From buCase_Note)


I get the following error is SSMS 2012:


An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.


I am looking to retain the
bi
table on the left while successfully joining in the case note table and bringing in only the most recent case note per participant.

Adding details:
Of course,
Here is a sample of data I get when running the following query:

Select
vc.[_Case Master ID],
vc.[_Caseload Assignment Current],
vc.[_Participant Name],
cn.[Case_Note_Date],
From biVR_Cases vc
LEFT outer JOIN buCase_Note cn ON vc.[_Case Master ID] = cn.Case_Master_ID


_Caseload Assignment Current Test Participant Name Casenote Date
Test Counselor Participant A
September 29, 2010
September 23, 2010
August 30, 2010
June 30, 2010
June 1, 2010

The bi table contains participant information like name, application, case master ID, etc. The casenote table contains the case master ID as well hence the join. It also contains the dates each entry is created. So for the dataset above, I am trying to only bring in the most recent casenote for each participant. I only included 1 in the sample above, but we have over 15,000. Each participant will have many case notes. I am trying to grab the top casenote so I can calculate the date difference between the most recent case note and today for each participant. When I add :

Where cn.[Case_Note_Date] = (Select
top 1 [Case_Note_Date]
From buCase_Note
Order by 1 DESC))
OR
Where Case_Note_Date=(
Select
MAX(Case_Note_Date)
From buCase_Note)


It is only showing the top or max casenote for participants that had a casenote created today. Instead of showing the max casenote in the casenote table, I need the max casenote per participant. I hope that makes more sense.

Answer

You might try something like what I have included below. Without actual data, I do not know if it is efficient enough for you, but it should work. If you get a better answer, however, I would love to know it.

Select vc.[_Case Master ID],
 vc.[_Caseload Assignment Current],
 vc.[_Participant Name],
 vc.[Case Status],
 vc.[Reporting Structure Level 4],
 vc.[Reporting Structure Level 5],
 vc.[Application Date],
 vc.[Eligibility Date],
 vc.[Eligibility Determination Extension Date],
 vc.[Eligibility Extended To Date], 
 vc.[Days in Application],
 cn.[Case_Note_Date], 
 cn.[Case_Note_Category_Desc],
 cn.[Case_Note_Summary_Narr]
From  biVR_Cases vc
LEFT outer JOIN 
   (SELECT Case_Master_ID, Case_Note_Date, Case_Note_Category_Desc, Case_Note_Summar_Narr, 
           ROW_NUMBER() OVER (PARTITION BY Case_Master_ID ORDER BY Case_Note_Date DESC) as RowNum FROM buCase_Note) cn 
   ON cn.Case_Master_ID = vc.[_Case Master ID] AND cn.RowNum=1
Comments