SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseSQL 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 ASP Free Forums Sponsor:
  #1  
Old November 6th, 2003, 11:07 AM
andrekl andrekl is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 2 andrekl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Group By

I have 3 records in BUYER table:-

ID |NAME |STATE |COUNTRY |CODE
01 |AA |A1 |A4 |A01
01 |AA |A2 |A3 |A02
02 |BB |B1 |B2 |B01

1) Considering 'Code' is unique.
2) ID might have more than one same.


I was using 'select max(ID), max(NAME), max(STATE), max(COUNTRY), max(CODE) from buyer group by ID'.

What i get was :-
01 AA A2 A4 A02 -- 1st record
02 BB B1 B2 B01 -- 2nd record

U notice that 1st record the data was mix up between the two same ID records.

How can i get :-

01 AA A1 A4 A01 -- 1st record
02 BB B1 B2 B01 -- 2nd record

The problem was caused by max() function. And i cant ignore the max() function because they required an aggregate funtion when i used group by.

Any help?

Last edited by andrekl : November 6th, 2003 at 11:13 AM.

Reply With Quote
  #2  
Old November 7th, 2003, 12:28 PM
sbaxter sbaxter is offline
Moderator: Access, SQL
ASP Free God (5000 - 5499 posts)
 
Join Date: Oct 2003
Posts: 5,126 sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 1 h 2 m 51 sec
Reputation Power: 12
No are you really wanting MAX or are you wanting FIRST
(Take the ID Group it and give me the first value recorded for this group of data for this ID)

Max is the largest value within the group

Based on what you stated fo results

Using First
select ID, First(NAME), First(STATE), First(COUNTRY), First(CODE) from buyer group by ID



Using Max with some of the options
select ID, First(NAME), Min(STATE), max(COUNTRY), Min(CODE) from buyer group by ID

s-

Reply With Quote
  #3  
Old November 9th, 2003, 02:26 AM
andrekl andrekl is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 2 andrekl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
it only works in Access Databases.. What if for SQL Server query..

I tried it. But is prompt out an error message, 'First is not a recognized function name'.

Im using SQL Query Analyser (Transact SQL).

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseSQL Development > Group By


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
Stay green...Green IT