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

  1. Newsgroups: comp.databases.sybase
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!uwm.edu!ux1.cso.uiuc.edu!news.cso.uiuc.edu!wenzel
  3. From: wenzel@ncsa.uiuc.edu (Patricia Wenzel)
  4. Subject: Re: SQL sort problems (group by?)
  5. References:  <1993Jan4.193801.25423@ctp.com>
  6. Message-ID: <C0E9EA.Asz@news.cso.uiuc.edu>
  7. Originator: wenzel@pogo.ncsa.uiuc.edu
  8. Sender: usenet@news.cso.uiuc.edu (Net Noise owner)
  9. Organization: Nat'l Ctr for Supercomp App (NCSA) @ University of Illinois
  10. Date: Tue, 5 Jan 1993 18:44:33 GMT
  11. Lines: 57
  12.  
  13.  
  14. In article <1993Jan4.193801.25423@ctp.com>, sreit@ctp.com (Stefan Reitshamer) writes:
  15. |> I need to sort data in a bizarre way and have NO idea how to do it (other
  16. |> than looping through rows and assigning index numbers and then selecting
  17. |> by ascending index number). Here's the data involved:
  18. |> 
  19. |> Claim #  Subscriber #  Date
  20. |> -------  ------------  --------
  21. |> 22402    111111111     10/10/92
  22. |> 55600    333333333     04/04/92
  23. |> 45200    222222222     09/15/92
  24. |> 22404    111111111     08/08/92
  25. |> 22400    111111111     09/09/92
  26. |> 44900    111111111     08/30/92
  27. |> 45202    222222222     07/01/92
  28. |> 
  29. |> The last 2 digits of the claim # are adjustment numbers (the 00 claim is the
  30. |> original, others are adjustments of the original).
  31. |> I need to sort this data by subscriber number, and by descending date 
  32. |> within each sub #. BUT, if a claim has been adjusted, I need the adjusted 
  33. |> claims to appear after the original (regardless of the date).
  34. |> 
  35. |> Basically, rows with the first 3 digits of the claim # in common should be
  36. |> grouped together. These groups should then be sorted by sub # and descending
  37. |> date.
  38. |> 
  39. |> So, the above data would be sorted as follows:
  40. |> 
  41. |> Claim #  Subscriber #  Date
  42. |> -------  ------------  --------
  43. |> 44900    111111111     05/05/92                     ]Group 1
  44. |> 22400    111111111     02/02/92                          ]Group 2
  45. |> 22402    111111111     03/03/92 <- Adjustment of 22400   ]Group 2
  46. |> 22404    111111111     08/08/92 <- Adjustment of 22400   ]Group 2
  47. |> 45200    222222222     01/01/92                   ]Group 3
  48. |> 45202    222222222     07/01/92                   ]Group 3
  49. |> 55600    333333333     04/04/92              ]Group 4
  50. |> 
  51. |> I was thinking something like:
  52. |>     select *
  53. |>     group by round(Claim#/100,0)
  54. |>     order by Subscriber,Date desc
  55. |> but I guess this is incorrect usage of the "group by" clause.
  56. |> 
  57. |> Does anyone know how to "select" this? (Hope this explanation makes some sort
  58. |> of sense)
  59. |> 
  60. |> Thanks very much!
  61. |>   Stefan
  62. |> -- 
  63. On the assumption that the claim # is an integer, the following might work:
  64.  
  65.   select *
  66.    from table
  67.     order by subscriber, claim #, date
  68.  
  69. Patricia Wenzel
  70.