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 |
|
akas
Starting Member
42 Posts |
Posted - 2008-07-22 : 11:45:05
|
| SELECT contact_id, max(contact_date), contactFROM vwcontactloggroup by contact_id, contactcontact_id, cotact_date contact-----------------------------------1 10/02/2007 1 10/02/2007 GC/SG/Sherry Mulligan1 08/13/2007 PEK and KN--Meeting Shannon Oates3 10/18/2007 Brian C.5 01/30/2008 AP5 10/02/2007 EO coordinated with Jennifer Stawarz7 07/30/2007 Conf. Call - KN, AP, PH, AIR & Levine7 07/11/2007 Email - John Walsh 7 07/18/2007 Email - John Walsh - Lara Crissey7 01/01/2008 EO/KN/Ifdy7 01/01/2008 JW-PEK7 08/09/2007 OK from John Walsh7 04/02/2008 testi m still getting wrong results..i just want max date row..contact_id, max(cotact_date) contact-----------------------------------1 10/02/2007 GC/SG/Sherry Mulligan3 10/18/2007 Brian C.5 01/30/2008 AP7 04/02/2008 testhow can i get this results..can anyone help me.thanks |
|
|
sross81
Posting Yak Master
228 Posts |
Posted - 2008-07-22 : 12:12:19
|
It seems like its duplicating the date when the contact is different. Have you tried to max(contact)?quote: Originally posted by akas SELECT contact_id, max(contact_date), contactFROM vwcontactloggroup by contact_id, contactcontact_id, cotact_date contact-----------------------------------1 10/02/2007 1 10/02/2007 GC/SG/Sherry Mulligan1 08/13/2007 PEK and KN--Meeting Shannon Oates3 10/18/2007 Brian C.5 01/30/2008 AP5 10/02/2007 EO coordinated with Jennifer Stawarz7 07/30/2007 Conf. Call - KN, AP, PH, AIR & Levine7 07/11/2007 Email - John Walsh 7 07/18/2007 Email - John Walsh - Lara Crissey7 01/01/2008 EO/KN/Ifdy7 01/01/2008 JW-PEK7 08/09/2007 OK from John Walsh7 04/02/2008 testi m still getting wrong results..i just want max date row..contact_id, max(cotact_date) contact-----------------------------------1 10/02/2007 GC/SG/Sherry Mulligan3 10/18/2007 Brian C.5 01/30/2008 AP7 04/02/2008 testhow can i get this results..can anyone help me.thanks
Thanks in Advance!Sherri |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2008-07-22 : 12:33:27
|
| [code]; with a as (select *, row_number() over (partition by contact_id order by contact_date desc, contact desc) as row from vwcontactlog)select contact_id, contact_date, contact from a where row = 1[/code]Ryan Randall Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-22 : 13:20:39
|
| [code]select t.*from yourtable tinner join (select contact_id,max(contact_date) as maxdate from yourtable group by contact_id) tmpon tmp.contact_id=t.contact_idand tmp.maxdate=t.contact_date[/code] |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2008-07-22 : 13:28:59
|
As long as we're throwing out alternatives:SELECT cl.contact_id, ca.contact_date, ca.contactFROM vwcontactlog clcross apply (select top 1 z.conctact_date, z.contact from vwcontactlog z where z.contactid = cl.contact_id order by z.conctact_date desc) cagroup by cl.contact_id, ca.contact_date, ca.contact Be One with the OptimizerTG |
 |
|
|
|
|
|
|
|