home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / database / sybase / 588 < prev    next >
Encoding:
Text File  |  1993-01-04  |  2.2 KB  |  90 lines

  1. Newsgroups: comp.databases.sybase
  2. Path: sparky!uunet!psinntp!ctp!kdomb
  3. From: kdomb@ctp.com (Kevin Dombroski)
  4. Subject: group by, max(), and unexpected results
  5. Message-ID: <1993Jan4.194100.25504@ctp.com>
  6. Keywords: unexpected results :-(
  7. Sender: news@ctp.com
  8. Nntp-Posting-Host: punch.ctp.com
  9. Organization: Cambridge Technology Partners
  10. Distribution: usa
  11. Date: Mon, 4 Jan 1993 19:41:00 GMT
  12. Lines: 76
  13.  
  14.  
  15. Folks,
  16.  
  17.     I'm trying to select a list of fields, grouping by field1, and gather
  18. ALL the fields for the row that has the maximum value (max(field2)). Asking
  19. for field1, max(field2) yields the results I want. However, if asking for 
  20. more fields than field1, field2 yields unexpected results.
  21.  
  22.     Here is a full, working example:
  23.  
  24.  
  25. create table #junk
  26. (
  27.     field1     int,
  28.     field2    int,
  29.     field3    int
  30. )
  31.  
  32. insert #junk values (1, 1, 5)
  33. insert #junk values (1, 2, 10)
  34. insert #junk values (1, 99, 15)
  35.  
  36. insert #junk values (2, 5, 200)
  37. insert #junk values (2, 10, 300)
  38. insert #junk values (2, 15, 400)
  39.  
  40. insert #junk values (3, 0, -1)
  41.  
  42. select field1, max(field2)
  43. from #junk
  44. group by field1
  45. order by field1
  46.  
  47. select field1, max(field2), field3
  48. from #junk
  49. group by field1
  50. order by field1
  51.  
  52. Results:
  53.  
  54.  field1      max field2
  55.  ----------- -----------
  56.            1          99
  57.            2          15
  58.            3           0
  59.  
  60. (3 rows affected)
  61.  
  62.  field1      max field2  field3
  63.  ----------- ----------- -----------
  64.            1          99           5
  65.            1          99          10
  66.            1          99          15
  67.            2          15         200
  68.            2          15         300
  69.            2          15         400
  70.            3           0          -1
  71.  
  72. (7 rows affected)
  73.  
  74.  
  75. Question: Why doesn't the second result yield the following 3 rows?
  76.  
  77.  field1      max field2  field3
  78.  ----------- ----------- -----------
  79.            1          99          15
  80.            2          15         400
  81.            3           0          -1
  82.  
  83.  
  84. Any input would be greatly appreciated...
  85. -- 
  86. ================================================================================
  87. Kevin M. Dombroski                  | "One good thing about music,
  88. Cambridge Technology Partners       |  When it hits, you feel no pain...." 
  89. ================================================================================
  90.