Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 6th, 2003, 12:48 PM
Authorized User
 
Join Date: Nov 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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?

 
Old November 6th, 2003, 02:32 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Do you only want to get the first record for a given ID? If so, what determines the "first" record? Should it be determined by some sorting sequence?

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old November 7th, 2003, 11:37 AM
Authorized User
 
Join Date: Nov 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

if there is more than one same 'ID', i just want to get EITHER one record. instead of now they get the final record consists of mix data from same 'ID'.

 
Old November 7th, 2003, 11:48 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

But which row? There is no "pick any old random row" function in SQL. Nor do the concepts of "first", "second", or "last" apply, unless you are able to impose some sort of ordering on the rows according to some column value ...

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old November 7th, 2003, 12:16 PM
Authorized User
 
Join Date: Nov 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks for replying..

the problem is not the row, i can get the rows.

the problem was the field, example the 'state' was jumble up.

One sample scenario:-

The owner live in Singapore and the state was 'Sing'.
Then he registered another account in Thailand and state was 'Thai'.

When i get the end result, it may came out :-

Country = 'Singapore'
State = 'Thai'

Which is wrong. Suppose to be either :-

Country = 'Singapore'
State = 'Sing'

OR

Country = 'Thailand'
State = 'Thai'

I just want to get EITHER one record. But the max() function will cause this problem if i use group by.


 
Old November 7th, 2003, 12:47 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

This is crude and rather ugly, but it seems to work.

SELECT * INTO #tempBuyer FROM buyer
GO
ALTER TABLE #tempBuyer ADD RowId INT NOT NULL IDENTITY
GO
SELECT b1.ID, b1.NAME, b1.STATE, b1.COUNTRY, b1.CODE
FROM #tempBuyer AS b1
JOIN (SELECT MAX(RowID) AS RowID FROM #tempBuyer GROUP BY ID) AS b2 ON b1.RowID = b2.RowID
DROP TABLE #tempBuyer

Peter
 
Old November 7th, 2003, 01:00 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:
... I just want to get EITHER one record.
But which row? As I said, there is no "pick a random row" function, nor is there a "first" row, etc.

I don't think you understand what the MAX aggregate function does.

The MAX function determines the Maximum value across the set of rows defined by the GROUP BY clause. The values are not "jumbled up" as you suggest, but represent each maxmimum value as you requested. So, when you query:
Code:
SELECT max(NAME), max(STATE), max(COUNTRY), max(CODE) from buyer group by ID
you are requesting a resultset that contains the maximum NAME, maximum STATE, maximum COUNTRY, and maximum CODE value from each set of rows where the ID value is the same. You are asking for the maximum value of each column, so each will be its maximum across the entire set of rows for each ID, not necessarily from the same row. The Maxmimum value for a set of strings will be the string that colates last in your character set.

In order to pick, say, the "first" (or perhaps "last") row in each group as defined by the ID, you must have some column value that places the rows where the ID is the same into some defined order.

Let's try to use your CODE value as the ordering column.

Suppose you have the following query:
Code:
SELECT MAX(CODE), ID FROM buyer GROUP BY ID
This will select the value of the CODE column which is a maxmimum for each set of ID values. Using your data from your original post, this would be:

A02 01
B01 02

Now we can use this ordering to select each row from your table where the CODE value is this maximum value. We need to use a correlated subquery to do this:
Code:
SELECT ID, NAME, STATE, COUNTRY, CODE
    FROM buyer B1 
WHERE CODE=(SELECT MAX(CODE) FROM buyer B2 WHERE B1.ID=B2.ID)
The subquery finds the maximum CODE value for each ID value in turn, and the outer query then selects the contents of the row for the ID where the CODE value is that maximum.

Alternatively you could use the MIN function to instead retrieve the "first" row in each ID group.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old November 8th, 2003, 01:55 PM
Authorized User
 
Join Date: Nov 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thxs for all your infos guys.. appreciate that..

it still not solve my problem yet.. maybe its because my explanation was bad..

anyway, my problem is that im not finding the max value for the record, i using it cos sql needed an aggregate function when im using group by.

i want to get informations from the same row of data instead of jumbles up when i use aggregate funtion and group by.

 
Old November 8th, 2003, 03:02 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

OK. Let's try this again.

What are you trying to accomplish by using GROUP BY?


Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old November 9th, 2003, 03:52 AM
Authorized User
 
Join Date: Nov 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

im using 'group by' to group the records with the same ID into one single record.

when i use group by, sql needs an aggregate function right?

so, im using the max() function. In which they will get the maximum value of the field with the same ID. Which i dont want.

I want the record value from the same row instead of jumbled data from mix records with same ID.

Anyway other method to go with?

Thxs for your patient..






Similar Threads
Thread Thread Starter Forum Replies Last Post
Group Within another Group, xslt1.0 jhansib4u BOOK: XSLT Programmer's Reference, 2nd Edition 4 November 22nd, 2007 01:24 AM
Restart new group number in Group Footer sukarso Crystal Reports 2 October 13th, 2006 12:11 PM
Welcome To New Group vector C++ Programming 0 December 30th, 2005 03:16 AM
Group by , Sub Group by and Sum mateenmohd SQL Server 2000 1 March 29th, 2005 09:51 AM
Group edramail Crystal Reports 0 May 18th, 2004 05:51 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.