home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / database / sybase / 642 < prev    next >
Encoding:
Internet Message Format  |  1993-01-08  |  3.1 KB

  1. Path: sparky!uunet!wupost!cs.utexas.edu!sun-barr!ames!agate!ucbvax!mtxinu!sybase!robert
  2. From: robert@sybase.com (Robert Garvey)
  3. Newsgroups: comp.databases.sybase
  4. Subject: Re: Help on SQL query
  5. Message-ID: <27727@sybase.sybase.com>
  6. Date: 7 Jan 93 21:36:12 GMT
  7. References: <1993Jan4.164204.21989@bmw.mayo.edu> <27661@sybase.sybase.com>
  8. Sender: news@Sybase.COM
  9. Organization: Emeryville Tupleware, Party Planning Division
  10. Lines: 93
  11.  
  12. In article <27661@sybase.sybase.com>, I wrote:
  13. |> [...method of expanding out an alias stored as alias/member pairs...]
  14.  
  15. An alternative is to handle all aliases in one loop, going through the
  16. loop n-1 times where n is the maximum number of members in the aliases.
  17.  
  18.     SELECT    an, pn
  19.     FROM aliases
  20.     ORDER BY an, pn
  21.  
  22.      an                             pn                             
  23.      ------------------------------ ------------------------------ 
  24.      dwarves                        bashful                        
  25.      dwarves                        doc                            
  26.      dwarves                        dopey                          
  27.      dwarves                        grumpy                         
  28.      dwarves                        happy                          
  29.      dwarves                        sleepy                         
  30.      dwarves                        sneezy                         
  31.      twain                          clemens                        
  32.      weapons                        fear                           
  33.      weapons                        surprise                       
  34.  
  35.  
  36.     SELECT    a.an,
  37.         pn_cnt = COUNT(*),
  38.         cur_cnt = CONVERT(int, 1),
  39.         last_added = MIN(a.pn),
  40.         list = CONVERT(varchar(255), MIN(a.pn))
  41.     INTO #exp_aliases
  42.     FROM aliases a
  43.     GROUP BY a.an
  44.  
  45.     DECLARE    @cur_count    int,
  46.         @max_count    int
  47.  
  48.     SELECT    @cur_count = 2,
  49.         @max_count = MAX(pn_cnt)
  50.     FROM #exp_aliases
  51.  
  52.     SELECT    an,
  53.         pn
  54.     INTO #next_pns
  55.     FROM aliases
  56.     WHERE 1 = 0
  57.  
  58.     WHILE ( @cur_count <= @max_count )
  59.     BEGIN
  60.  
  61.         INSERT INTO #next_pns ( an, pn )
  62.         SELECT    a.an,
  63.             MIN(a.pn)
  64.         FROM aliases a, #exp_aliases e
  65.         WHERE a.pn > e.last_added
  66.           AND a.an = e.an
  67.           AND e.pn_cnt >= @cur_count
  68.         GROUP BY a.an
  69.         HAVING MIN(a.pn) IS NOT NULL
  70.         
  71.         INSERT INTO #exp_aliases ( an, pn_cnt, cur_cnt, last_added, list )
  72.         SELECT    e.an,
  73.             e.pn_cnt,
  74.             @cur_count,
  75.             n.pn,
  76.             e.list + ", " + n.pn
  77.         FROM #exp_aliases e, #next_pns n
  78.         WHERE e.an = n.an
  79.  
  80.         DELETE #next_pns
  81.  
  82.         DELETE #exp_aliases
  83.         WHERE cur_cnt < pn_cnt
  84.           AND cur_cnt < @cur_count
  85.  
  86.         SELECT @cur_count = @cur_count + 1
  87.  
  88.     END
  89.  
  90.     SELECT    an = SUBSTRING(an, 1, 8),
  91.         list = SUBSTRING(list, 1, 50)
  92.     FROM #exp_aliases ORDER BY an
  93.  
  94.      an       list                                               
  95.      -------- -------------------------------------------------- 
  96.      dwarves  bashful, doc, dopey, grumpy, happy, sleepy, sneezy 
  97.      twain    clemens                                            
  98.      weapons  fear, surprise                                     
  99.  
  100. -- 
  101. Robert Garvey robert@sybase.com {sun,lll-tis,pyramid,pacbell}!sybase!robert
  102.           Sybase, Inc   6475 Christie Ave   Emeryville, CA   94608-1010
  103.  
  104. I'm the only one talking here.  Opinions are not necessarily those of Sybase.
  105.