General SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesGeneral SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old August 12th, 2004, 05:33 PM
sumanb sumanb is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 1 sumanb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
sql select help required

I have a table T1 with the following columns
( A Char, B char, C Char, D char, E Date)

I have the data for A and B and I want to retrieve the records with the maximum date, The columns C and D will repeat for a combination of A, B.

I can write a sql with a subquery like this , but due to performance reasons I dont prefer a subquery
Select C, D, E from T where A = n and B= m and T = ( select Max(E) from T where A= n and B= m)

Performace is very important for this query. Please help.

Thanks,
Sumanb

Reply With Quote
  #2  
Old August 13th, 2004, 03:32 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 992 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 740050 Folding Title: Super Ultimate Folder - Level 2Folding Points: 740050 Folding Title: Super Ultimate Folder - Level 2Folding Points: 740050 Folding Title: Super Ultimate Folder - Level 2Folding Points: 740050 Folding Title: Super Ultimate Folder - Level 2Folding Points: 740050 Folding Title: Super Ultimate Folder - Level 2Folding Points: 740050 Folding Title: Super Ultimate Folder - Level 2Folding Points: 740050 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 13 h 37 m 16 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
maybe something like:

SELECT C, D, E FROM T1 WHERE A='n' AND B='m', ORDER BY E DESC LIMIT 1;

this will order the items that apply (A=n and B=m), with the biggest on top, then only take the first (one with the highest date)

Alternatively, if what you mean by
Quote:
The columns C and D will repeat for a combination of A, B.
is that C and D are the same for all lines in which A=n and B=m (so only E is different) you can use:

SELECT C, D, MAX(E) FROM T1 WHERE A='n' AND B='m' GROUP BY a, b);

only use this if that's true though, ontherwise you'll get wrong results.

Reply With Quote
  #3  
Old September 15th, 2004, 05:46 PM
rajitha_kba rajitha_kba is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 6 rajitha_kba User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thumbs down Inner Join

Hi Sumanb

I prefer doing this query using INNER JOIN. It will be much quicker

Raj

Quote:
Originally Posted by sumanb
I have a table T1 with the following columns
( A Char, B char, C Char, D char, E Date)

I have the data for A and B and I want to retrieve the records with the maximum date, The columns C and D will repeat for a combination of A, B.

I can write a sql with a subquery like this , but due to performance reasons I dont prefer a subquery
Select C, D, E from T where A = n and B= m and T = ( select Max(E) from T where A= n and B= m)

Performace is very important for this query. Please help.

Thanks,
Sumanb

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesGeneral SQL Development > sql select help required


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