home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.databases.sybase
- Path: sparky!uunet!psinntp!ctp!kdomb
- From: kdomb@ctp.com (Kevin Dombroski)
- Subject: group by, max(), and unexpected results
- Message-ID: <1993Jan4.194100.25504@ctp.com>
- Keywords: unexpected results :-(
- Sender: news@ctp.com
- Nntp-Posting-Host: punch.ctp.com
- Organization: Cambridge Technology Partners
- Distribution: usa
- Date: Mon, 4 Jan 1993 19:41:00 GMT
- Lines: 76
-
-
- Folks,
-
- I'm trying to select a list of fields, grouping by field1, and gather
- ALL the fields for the row that has the maximum value (max(field2)). Asking
- for field1, max(field2) yields the results I want. However, if asking for
- more fields than field1, field2 yields unexpected results.
-
- Here is a full, working example:
-
-
- create table #junk
- (
- field1 int,
- field2 int,
- field3 int
- )
-
- insert #junk values (1, 1, 5)
- insert #junk values (1, 2, 10)
- insert #junk values (1, 99, 15)
-
- insert #junk values (2, 5, 200)
- insert #junk values (2, 10, 300)
- insert #junk values (2, 15, 400)
-
- insert #junk values (3, 0, -1)
-
- select field1, max(field2)
- from #junk
- group by field1
- order by field1
-
- select field1, max(field2), field3
- from #junk
- group by field1
- order by field1
-
- Results:
-
- field1 max field2
- ----------- -----------
- 1 99
- 2 15
- 3 0
-
- (3 rows affected)
-
- field1 max field2 field3
- ----------- ----------- -----------
- 1 99 5
- 1 99 10
- 1 99 15
- 2 15 200
- 2 15 300
- 2 15 400
- 3 0 -1
-
- (7 rows affected)
-
-
- Question: Why doesn't the second result yield the following 3 rows?
-
- field1 max field2 field3
- ----------- ----------- -----------
- 1 99 15
- 2 15 400
- 3 0 -1
-
-
- Any input would be greatly appreciated...
- --
- ================================================================================
- Kevin M. Dombroski | "One good thing about music,
- Cambridge Technology Partners | When it hits, you feel no pain...."
- ================================================================================
-