MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old February 24th, 2004, 10:25 PM
nhminh nhminh is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 2 nhminh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Please help me grouping

I have following table
table NAME
Id name group_id date_changed
1 candy 3 20/01/01
2 cake 3 20/02/01
3 dog 3 20/01/02
4 Cat 3 20/02/02
5 tree 3 20/01/03
6 Flower 3 20/03/03
I want to filter newest name for each group_id (by date_changed) like below

2 cake 3 20/02/01
4 Cat 3 20/02/02
6 Flower 3 20/03/03
If call query: Select * from NAME order by group_id, date_changed DESC, result will be all rows (is this case is 6 rows). using this result is very difficult
please help me find appropriate query

Reply With Quote
  #2  
Old March 1st, 2004, 05:20 PM
sypher sypher is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: Montreal
Posts: 92 sypher User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 10 sec
Reputation Power: 5
you have to use a sub-quey to do what you need

but I think there is something wrong with your data...cuz I don't understand your end result...here's an exmaple with Pubs:
select distinct au_id,(select Max(royaltyper) from titleauthor b where a.au_id=b.au_id ) as the MaxRoyaltyper
from titleauthor a
but here's what I think you query should look like:
Select distinct goup_id,(select max(date_changed)from name B where a.group_id=b.group_id) as Last_date_changed from NAME A

Reply With Quote
  #3  
Old March 2nd, 2004, 02:21 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,766 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 6 h 24 m 8 sec
Reputation Power: 37
Code:
Select * from NAME n
where date_changed = (select max(date_changed)
from name
where group_id = n.group_id)

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Please help me grouping


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway