home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / database / ingres / 1930 < prev    next >
Encoding:
Text File  |  1992-11-18  |  2.5 KB  |  77 lines

  1. Newsgroups: comp.databases.ingres
  2. Path: sparky!uunet!ukma!asuvax!ncar!uchinews!gsbacd.uchicago.edu!cs_mj
  3. From: cs_mj@gsbacd.uchicago.edu (Mark Jaeger)
  4. Subject: Re: Is it possible to get the first "n" records of a table or view ?
  5. Message-ID: <1992Nov18.135025.1@gsbacd.uchicago.edu>
  6. Lines: 65
  7. Sender: news@uchinews.uchicago.edu (News System)
  8. Organization:     
  9. References: <C4A55D13881F2037B8@ichcmns.cmns.mnegri.it>
  10. Date: Wed, 18 Nov 1992 19:50:25 GMT
  11.  
  12. In article <C4A55D13881F2037B8@ichcmns.cmns.mnegri.it>, 
  13. Marco Olivieri <OLIVIERI@ICHCMNS.CMNS.MNEGRI.IT> writes:
  14.  
  15. > Is it possible to get the first "n" records of a table or view ?
  16. > I have a table with about 230000 records, through query, I want
  17. > to read only the first 50 records, ignoring the other ones.
  18. > Does any SQL function exist ? (I don't thinks, but I'm not sure)
  19.  
  20. I've attached a similar question and my response.  You can do it
  21. directly in SQL if your table has the necessary design; otherwise, you
  22. can do it in a cursor or select loop in the host language (4GL, C,
  23. FORTRAN, etc.).
  24.  
  25. --Mark Jaeger                internet: cs_mj@gsbvax.uchicago.edu
  26. Graduate School of Business        yellnet:  (312) 702-0328
  27. University of Chicago            faxnet:   (312) 702-0233
  28. Disclaimer: My opinions are my own and not those of my employer.
  29. Ich bin ein Virus.  Mach' mit und kopiere mich in Deine .signature.
  30.  
  31. ================================================================================
  32. In article <1992Aug17.142452.24812@itm.uucp>, danny@itm.uucp (Danny Cox) writes:
  33. > We get many requests for the 'top N' rows from
  34. > a table.  How does one accomplish this in SQL?  Sorting is no problem,
  35. > but how does one request the first N rows?  Obviously one could do an
  36. > 'endloop' to stop once N is reached in 4gl/ESQLC, but is there a better way?
  37.  
  38. You can do this directly in a single SQL statement, if your table has a
  39. column of unique values that defines the order.  I've attached a simple
  40. example.  I won't say it's better, and in fact using a select or cursor
  41. loop will probably give better performance in general.
  42.  
  43. ================================================================================
  44. select * from t order by id ;
  45. +------+
  46. |id    |
  47. +------+
  48. |    10|
  49. |    20|
  50. |    30|
  51. |    40|
  52. |    50|
  53. |    60|
  54. |    70|
  55. |    80|
  56. +------+
  57.  
  58. select * from t a where 3 >= ( select count(*) from t b where b.id <= a.id ) ;
  59. +------+
  60. |id    |
  61. +------+
  62. |    10|
  63. |    20|
  64. |    30|
  65. +------+
  66.  
  67. select * from t a where 3 >= ( select count(*) from t b where b.id >= a.id ) ;
  68. +------+
  69. |id    |
  70. +------+
  71. |    60|
  72. |    70|
  73. |    80|
  74. +------+
  75.  
  76.