home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / database / oracle / 2962 < prev    next >
Encoding:
Internet Message Format  |  1993-01-28  |  5.0 KB

  1. Path: sparky!uunet!portal!lll-winken!roxanne.llnl.gov!hudek
  2. From: hudek@polaris.llnl.gov (David Hudek)
  3. Newsgroups: comp.databases.oracle
  4. Subject: SQL*Plus Puzzle
  5. Message-ID: <147754@lll-winken.LLNL.GOV>
  6. Date: 26 Jan 93 18:53:08 GMT
  7. Sender: usenet@lll-winken.LLNL.GOV
  8. Organization: Lawrence Livermore National Labs, Livermore CA
  9. Lines: 108
  10. Nntp-Posting-Host: roxanne.llnl.gov
  11. Originator: hudek@roxanne.llnl.gov
  12.  
  13. <||>
  14.  
  15. Here's a puzzle of sorts.
  16.  
  17. I was asked to create a little report based on a pre-existing table.
  18. After some thought, I decided to do it in C using Pro*C, but since
  19. my background is more from underlying systems software than applications 
  20. (I'm still fairly new at the database game), I wondered if I was suffering
  21. from the "when you have a hammer, all problems look like a nail"
  22. syndrome and used C when a clever SQL/SQL*Plus solution might
  23. have been more appropriate. I've already written the code with Pro*C
  24. and it works fine, so I probably won't change it, but for grins
  25. (and the opportunity to learn from the "gurus" out there) can anyone
  26. out there think of a clean way to do the following using SQL*Plus?
  27.  
  28.  
  29. Relevant columns (names changed to protect the innocent) are:
  30.     aThingAMaJig    NOT NULL    CHAR(12)
  31.     aDeelyBopper    NOT NULL    NUMBER
  32.     aFlag        NOT NULL    CHAR(1)
  33.  
  34. aThingAMaJig, though a char, holds numbers which have the pattern:
  35.     {MajorNumber}{MinorNumber}
  36.         where MajorNumber is > 0 and generally two-digit ASCII with
  37.         leading zero (e.g., "01" to "99")
  38.         and MinorNumber is > 0 ASCII up to 10 digits long, but
  39.         by convention is usually two-digits with leading zero
  40.         To confuse matters, unfortunately, the Major Number in
  41.         one instance took the form {MajorNumber}/{MajorNumber}
  42.         (a la "05/06") which also restricted its MinorNumber to 
  43.         7 digits.
  44.         examples of valid aThingAMaJig's are:
  45.         "0101", "0102", "05/0603", "05/0606"
  46. aDeelyBopper is a number > 0
  47. aFlag is a single character that usually holds upper-case letters
  48.  
  49.  
  50. What is desired is for the user to input a MajorNumber and to get back
  51. a report concerning all aDeelyBoppers that have that MajorNumber in their
  52. corresponding aThingAMaJig and also have aFlag of, let's say, "X";
  53. the report should list the ranges of *consecutive* aDeelyBoppers that satisfy
  54. the criteria, grouped by aThingAMaJigs, a count of each range, and a
  55. grand total of all.
  56. The users wanted the format to look something like this:
  57.  
  58.  
  59.        Date            BlahBlah MajorNumber Title
  60.  
  61.  
  62.        (aThingAMaJig)    Range Of Blah (aDeelyBoppers)    (# this range)
  63.        -----------    ---------------------------    --------------
  64.         05/0601         001501 - 001800             300
  65.  
  66.         05/0602         000151 - 000175             25
  67.                         000951 - 001100             150
  68.  
  69.         05/0603         000401 - 000525             125
  70.                         000776 - 000825             50
  71.                         000876 - 001175             300
  72.                         001551 - 001800             250
  73.  
  74.         05/0604         000351 - 000400             50
  75.                         000551 - 000600             50
  76.  
  77.         05/0605         000801 - 001800             1000
  78.                                                     -------------------
  79.                                                     2300  - Grand Total
  80.                                                     -------------------
  81.  
  82.  
  83. In this case, showing the case of the {MajorNumber}/{MajorNumber}{MinorNumber}
  84. form of aThingAMaJig. The top of the page has the date, and a title containing
  85. the MajorNumber they input. The first column of the output only prints
  86. out aThingAMaJig when it's different from the previous, and skips a line
  87. when it does print. The second column contains the range of consecutive
  88. aDeelyBoppers that correspond to the aThingAMaJig and have aFlag "X". The
  89. third column lists the number of aDeelyBoppers in each range and at the
  90. end has the grand total of all aDeelyBoppers that have aFlag "X" and a
  91. aThingAMaJig that contains the specified MajorNumber.
  92.  
  93. Is there an easy way to do this using SQL*Plus? I got hung up on the
  94. requirement to list the ranges of *consecutive* aDeelyBoppers and decided
  95. it would be easy to just do it in C using Pro*C with a CURSOR declared as 
  96.     SELECT        aThingAMaJig, aDeelyBopper, aFlag
  97.     FROM        theTable
  98.     WHERE            (aThingAMaJig LIKE :whatMajorNum||'%')
  99.             AND    (aFlag = 'X')
  100.     ORDER BY    aThingAMaJig, aDeelyBopper;
  101. and just merrily fetch along, detecting changes in aThingAMaJig and/or
  102. non-consecutive aDeelyBoppers, keeping a couple running sums, 
  103. and printing out as appropriate. (with a little special processing
  104. for the unusual 05 or 06 major number case).
  105.  
  106. It works fine as a C/Pro*C, but I wonder at its efficiency, fetching all
  107. those rows just to do comparisons, etc. that the database system should
  108. do for you. Is there a clean way to do it in SQL*Plus?
  109. It seems like there ought to be, using mins and maxes and carefully
  110. worded clauses, but being rushed for time, I went with a way I knew
  111. would work.  Any "gurus" out there wish to share a better way?  :-)
  112.  
  113.  
  114. Thanks in advance,
  115. dave hudek
  116. djh@llnl.gov
  117.  
  118. <disclaimer:  These are personal opinions only. >
  119. < I do not speak for any group or organization. >
  120.  
  121.