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

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 December 4th, 2006, 05:09 AM
xitin xitin is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2006
Posts: 2 xitin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 m 18 sec
Reputation Power: 0
Please help me about sql query?

Hi, everyone

I have a data set following:
Datetime Name Value
2006-10-10 13:30:00 AM A 2
2006-10-10 13:30:00 AM B 1
2006-10-10 13:30:00 AM C 5
2006-10-10 14:30:00 AM A 6
2006-10-10 14:30:00 AM B 3
2006-10-10 14:30:00 AM C 1
2006-10-10 15:30:00 AM A 0
2006-10-10 15:30:00 AM B 8
2006-10-10 15:30:00 AM C 4
2006-10-10 16:30:00 AM A 2
2006-10-10 16:30:00 AM B 4
2006-10-10 16:30:00 AM C 3
..................

Now, I want to the result that A, B, C are max of fields:
2006-10-10 14:30:00 AM A 6
2006-10-10 15:30:00 AM B 8
2006-10-10 13:30:00 AM C 5

How to write sql query to return this result?
Please help me!

Thanks for your advise

Best regard,

Reply With Quote
  #2  
Old December 6th, 2006, 07:02 AM
Amitktikkiwal Amitktikkiwal is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2006
Posts: 1 Amitktikkiwal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 m 39 sec
Reputation Power: 0
query...

assume the following table structure...

create table test1
(
dt datetime,
name varchar(10),
num int
)


This is the query for ur problem...
select *
from test1 a
where num = (select max(num) from test1 b where b.name = a.name)
order by name

Reply With Quote
  #3  
Old December 7th, 2006, 01:55 AM
xitin xitin is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2006
Posts: 2 xitin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 m 18 sec
Reputation Power: 0
Thanks very much

That's a good idea. I's apply to my database, but it have a problem.

If I have 12 tables are the same structure, every table contain data for a month. If I use this query:

SELECT * FROM (

select *
from test1_200601 a
where num = (select max(num) from test1_200601 b where b.name = a.name)
order by name

UNION

select *
from test1_200602 a
where num = (select max(num) from test1_200602 b where b.name = a.name)
order by name

UNION

select *
from test1_200603 a
where num = (select max(num) from test1_200603 b where b.name = a.name)
order by name

....


UNION

select *
from test1_200612 a
where num = (select max(num) from test1_200612 b where b.name = a.name)
order by name
) AS AA

ORDER BY name

Results:

2006-01-10 13:30:00 AM A 2
2006-02-04 13:30:00 AM B 1
2006-03-12 13:30:00 AM C 5
2006-04-10 14:30:00 AM A 6
2006-05-10 14:30:00 AM B 3
2006-06-19 14:30:00 AM C 1
2006-07-10 15:30:00 AM A 0
2006-08-10 15:30:00 AM B 8
2006-09-23 15:30:00 AM C 4
2006-10-10 16:30:00 AM A 2
2006-11-04 16:30:00 AM B 4
2006-12-10 16:30:00 AM C 3

Return results is a table that have many record is the same info but value is difference (Each max record is gotten from 1 table).

I want results are following:

2006-04-10 14:30:00 AM A 6
2006-08-10 15:30:00 AM B 8
2006-03-12 13:30:00 AM C 5

But I can't use your technology for this case. How to select max record from this table?

Thanks

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMicrosoft SQL Server > Please help me about sql query?


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 4 hosted by Hostway
Stay green...Green IT