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 |
|
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_ActivityFrom 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_Activity8/6/2010 10:388/6/2010 23:008/6/2010 10:418/6/2010 10:418/6/2010 10:418/6/2010 10:418/6/2010 10:41I 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 replaceleft outer JOIN KPGA_ODS.CLARITY.REFERRAL_NOTIF_HIS as nhwithleft outer join (select referral_id,max(NOTIF_DATETIME) as lastActivity,referral_idfrom JOIN KPGA_ODS.CLARITY.REFERRAL_NOTIF_HISgroup by referral_id) as nh and then instead of using max in your select just usenh.lastActivityJimEveryday I learn something that somebody else already knew |
 |
|
|
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_ActivityFrom KPGA_ODS.CLARITY.REFERRAL Rleft outer Join KPGA_ODS.CLARITY.PATIENT P On R.PAT_ID = P.PAT_IDleft outer Join KPGA_ODS.CLARITY.NOTES_MC_RFL N On R.REFERRAL_ID = N.RECORD_IDleft outer Join KPGA_ODS.CLARITY.HNO_ENC_INFO H On N.NOTE_ID = H.NOTE_IDleft outer Join KPGA_ODS.CLARITY.ZC_NOTE_TYPE Z On H.NOTE_TYPE_C = Z.NOTE_TYPE_Cleft 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 ONWhere 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_Activity8/6/2010 10:388/6/2010 23:008/6/2010 10:418/6/2010 10:418/6/2010 10:418/6/2010 10:418/6/2010 10:41I 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. |
 |
|
|
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 |
 |
|
|
|
|
|
|
|