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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 MAX() - Help

Author  Topic 

OON
Starting Member

22 Posts

Posted - 2010-09-23 : 16:32:47
Hello,

I have the following query, intended to get the MAX (NOTIF_DATETIME) AS Last_Activity -

-----------------------------------------------------------
Select distinct R.REFERRAL_ID,H.NOTE_SUMMARY,nh.NOTIF_ACT_C,
Z.NAME,MAX(nh.NOTIF_DATETIME) as Last_Activity

From KPGA_ODS.CLARITY.REFERRAL R
left outer Join KPGA_ODS.CLARITY.PATIENT P On R.PAT_ID = P.PAT_ID
left outer Join KPGA_ODS.CLARITY.NOTES_MC_RFL N On R.REFERRAL_ID = N.RECORD_ID
left outer Join KPGA_ODS.CLARITY.HNO_ENC_INFO H On N.NOTE_ID = H.NOTE_ID
left outer Join KPGA_ODS.CLARITY.ZC_NOTE_TYPE Z On H.NOTE_TYPE_C = Z.NOTE_TYPE_C
left outer JOIN KPGA_ODS.CLARITY.REFERRAL_NOTIF_HIS as nh on nh.REFERRAL_ID = R.REFERRAL_ID
--inner join KPGA_ODS.CLARITY.CLARITY_EMP E ON
Where EXTERNAL_ID_NUM = '201181380'
AND H.NOTE_SUMMARY <> 'NULL'
AND H.NOTE_SUMMARY NOT IN ('Auto: Notification Recipient List')
GROUP BY R.REFERRAL_ID,H.NOTE_SUMMARY,nh.NOTIF_ACT_C,
Z.NAME
--------------------------------------------------------------

Im interested in returning the MAX(NOTIF_DATETIME)as Last_Activity.
However, this is what i get for the Last_Activity column even with the MAX function around it -

Last_Activity
8/6/2010 10:38
8/6/2010 23:00
8/6/2010 10:41
8/6/2010 10:41
8/6/2010 10:41
8/6/2010 10:41
8/6/2010 10:41


I expected to only get th MAX Last_Activity which should be 8/6/2010 23:00.

what I'm I doing wrong??

Thanks!

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2010-09-23 : 19:47:58
What group does that MAX(NOTIF_DATETIME) belong to? Right now each one is specific to
R.REFERRAL_ID,H.NOTE_SUMMARY,nh.NOTIF_ACT_C,
Z.NAME

should it just be unique to referral_id? What is it that that you want the last activity for? If it is just the referral_id replace
left outer JOIN KPGA_ODS.CLARITY.REFERRAL_NOTIF_HIS as nh

with


left outer join (select referral_id,max(NOTIF_DATETIME) as lastActivity
,referral_id
from JOIN KPGA_ODS.CLARITY.REFERRAL_NOTIF_HIS
group by referral_id) as nh


and then instead of using max in your select just use
nh.lastActivity

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

namman
Constraint Violating Yak Guru

285 Posts

Posted - 2010-09-24 : 00:12:43
quote:

I have the following query, intended to get the MAX (NOTIF_DATETIME) AS Last_Activity -

-----------------------------------------------------------
Select distinct R.REFERRAL_ID,H.NOTE_SUMMARY,nh.NOTIF_ACT_C,
Z.NAME,MAX(nh.NOTIF_DATETIME) as Last_Activity

From KPGA_ODS.CLARITY.REFERRAL R
left outer Join KPGA_ODS.CLARITY.PATIENT P On R.PAT_ID = P.PAT_ID
left outer Join KPGA_ODS.CLARITY.NOTES_MC_RFL N On R.REFERRAL_ID = N.RECORD_ID
left outer Join KPGA_ODS.CLARITY.HNO_ENC_INFO H On N.NOTE_ID = H.NOTE_ID
left outer Join KPGA_ODS.CLARITY.ZC_NOTE_TYPE Z On H.NOTE_TYPE_C = Z.NOTE_TYPE_C
left outer JOIN KPGA_ODS.CLARITY.REFERRAL_NOTIF_HIS as nh on nh.REFERRAL_ID = R.REFERRAL_ID
--inner join KPGA_ODS.CLARITY.CLARITY_EMP E ON
Where EXTERNAL_ID_NUM = '201181380'
AND H.NOTE_SUMMARY <> 'NULL'
AND H.NOTE_SUMMARY NOT IN ('Auto: Notification Recipient List')
GROUP BY R.REFERRAL_ID,H.NOTE_SUMMARY,nh.NOTIF_ACT_C,
Z.NAME

--------------------------------------------------------------

Im interested in returning the MAX(NOTIF_DATETIME)as Last_Activity.
However, this is what i get for the Last_Activity column even with the MAX function around it -


Last_Activity
8/6/2010 10:38
8/6/2010 23:00
8/6/2010 10:41
8/6/2010 10:41
8/6/2010 10:41
8/6/2010 10:41
8/6/2010 10:41


I expected to only get th MAX Last_Activity which should be 8/6/2010 23:00.

what I'm I doing wrong??



Nothing, that is exactly how order by and max work ...

The red part indicates that your query returns 7 different groups combined by the blue part. Then each row of column "Last_Activity" is max of each group.

If you want all 7 rows of column "Last_Activity" have the same value 8/6/2010 23:00, or result having just 1 row with value 8/6/2010 23:00 on column "Last_Activity", then you have to write a different query.

Provide your sample table(with data) and expected result, I think lot people here can help you out.
Go to Top of Page

OON
Starting Member

22 Posts

Posted - 2010-09-24 : 12:41:01
Thank you all for your help. I now figured out what i did wrong.

thanks
Go to Top of Page
   

- Advertisement -