dbTalk Databases Forums  

Group By Question

microsoft.public.sqlserver.dts microsoft.public.sqlserver.dts


Discuss Group By Question in the microsoft.public.sqlserver.dts forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Robert Hamilton
 
Posts: n/a

Default Group By Question - 04-21-2005 , 11:55 AM






I have a table the looks something like:
c1 c2 c3
1 3 1/1/2005
1 1 2/1/2005
1 5 4/1/2005
1 2 9/1/2005

I want to Group By c1 select the the last c2 value based on Max(c3); which,
is c2=2 and c3=9/1/2005. I'm sure I've done this before, but can't figure it
out.

Thanks,
Rob

Reply With Quote
  #2  
Old   
Alejandro Mesa
 
Posts: n/a

Default RE: Group By Question - 04-21-2005 , 01:39 PM






Try,

select c1, c2, c3
from table1 as t1
where c3 = (select max(t2.c3) from table1 as t2 where t2.c1 = t1.c1)


AMB

"Robert Hamilton" wrote:

Quote:
I have a table the looks something like:
c1 c2 c3
1 3 1/1/2005
1 1 2/1/2005
1 5 4/1/2005
1 2 9/1/2005

I want to Group By c1 select the the last c2 value based on Max(c3); which,
is c2=2 and c3=9/1/2005. I'm sure I've done this before, but can't figure it
out.

Thanks,
Rob

Reply With Quote
  #3  
Old   
Allan Mitchell
 
Posts: n/a

Default Re: Group By Question - 04-21-2005 , 01:52 PM



So maybe something like


So it would be something similar to

USE TempDB
GO
CREATE TABLE calc (c1 int, c2 int, c3 smalldatetime)

INSERT calc VALUES(1, 3 , '1/1/2005')
INSERT calc VALUES(1, 1 , '2/1/2005')
INSERT calc VALUES(1, 5 , '4/1/2005')
INSERT calc VALUES(1 , 2, '9/1/2005')

SELECT MAX(c2) as C2Val, c1 as GroupByCol
FROM calc calc1
WHERE c3 = (SELECT MAX(c3) FROM calc calc2 WHERE calc2.c1 = calc1.c1)
group by c1


--

Allan Mitchell MCSE,MCDBA, (Microsoft SQL Server MVP)
www.SQLDTS.com - The site for all your DTS needs.
www.SQLIS.com - SQL Server 2005 Integration Services.
www.Konesans.com


"Robert Hamilton" <RobertHamilton (AT) discussions (DOT) microsoft.com> wrote

Quote:
I have a table the looks something like:
c1 c2 c3
1 3 1/1/2005
1 1 2/1/2005
1 5 4/1/2005
1 2 9/1/2005

I want to Group By c1 select the the last c2 value based on Max(c3); which,
is c2=2 and c3=9/1/2005. I'm sure I've done this before, but can't figure it
out.

Thanks,
Rob



Reply With Quote
  #4  
Old   
Robert Hamilton
 
Posts: n/a

Default Re: Group By Question - 04-21-2005 , 02:41 PM



Thanks!

"Allan Mitchell" wrote:

Quote:
So maybe something like


So it would be something similar to

USE TempDB
GO
CREATE TABLE calc (c1 int, c2 int, c3 smalldatetime)

INSERT calc VALUES(1, 3 , '1/1/2005')
INSERT calc VALUES(1, 1 , '2/1/2005')
INSERT calc VALUES(1, 5 , '4/1/2005')
INSERT calc VALUES(1 , 2, '9/1/2005')

SELECT MAX(c2) as C2Val, c1 as GroupByCol
FROM calc calc1
WHERE c3 = (SELECT MAX(c3) FROM calc calc2 WHERE calc2.c1 = calc1.c1)
group by c1


--

Allan Mitchell MCSE,MCDBA, (Microsoft SQL Server MVP)
www.SQLDTS.com - The site for all your DTS needs.
www.SQLIS.com - SQL Server 2005 Integration Services.
www.Konesans.com


"Robert Hamilton" <RobertHamilton (AT) discussions (DOT) microsoft.com> wrote in message
news:3065A406-6AC9-4D2D-B49B-BBF8D21CE697 (AT) microsoft (DOT) com...
I have a table the looks something like:
c1 c2 c3
1 3 1/1/2005
1 1 2/1/2005
1 5 4/1/2005
1 2 9/1/2005

I want to Group By c1 select the the last c2 value based on Max(c3); which,
is c2=2 and c3=9/1/2005. I'm sure I've done this before, but can't figure it
out.

Thanks,
Rob




Reply With Quote
  #5  
Old   
Robert Hamilton
 
Posts: n/a

Default RE: Group By Question - 04-21-2005 , 02:42 PM



Thanks!

"Alejandro Mesa" wrote:

Quote:
Try,

select c1, c2, c3
from table1 as t1
where c3 = (select max(t2.c3) from table1 as t2 where t2.c1 = t1.c1)


AMB

"Robert Hamilton" wrote:

I have a table the looks something like:
c1 c2 c3
1 3 1/1/2005
1 1 2/1/2005
1 5 4/1/2005
1 2 9/1/2005

I want to Group By c1 select the the last c2 value based on Max(c3); which,
is c2=2 and c3=9/1/2005. I'm sure I've done this before, but can't figure it
out.

Thanks,
Rob

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.