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

  1. Path: sparky!uunet!spool.mu.edu!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: <27661@sybase.sybase.com>
  6. Date: 6 Jan 93 20:09:10 GMT
  7. References: <1993Jan4.164204.21989@bmw.mayo.edu>
  8. Sender: news@Sybase.COM
  9. Organization: Entities & Beer
  10. Lines: 102
  11.  
  12. In article <1993Jan4.164204.21989@bmw.mayo.edu>, roder@mayo.edu (Mark N. Roder) writes:
  13. |> 
  14. |>     I need help on a SQL query. I am working on a application to track  
  15. |> SMTP mail aliases and need to convert a table of aliases to "/etc/aliases"  
  16. |> file format.
  17. |> 
  18. |> What I have:
  19. |> 
  20. |>     an = alias name.  alphanumeric(i.e. last.first, sas_users,etc)
  21. |>     pn = person name. alphanumeric(i.e. login_name@host, "|lpr  
  22. |> 
  23. |> What I want:("/etc/aliases" format)
  24. |> 
  25. |> a1:    p1
  26. |> a2:    p2,p1,p3
  27. |> a4:    p3,p6
  28. |> a5:    p8
  29. |> 
  30. |> 
  31. |>     Any thoughts on this?  I would like to keep this straight SQL on  
  32. |> the Sybase server since it would make distribution of the information a  
  33. |> lot easier.
  34.  
  35. With flow of control in Transact-SQL, it could be done like this:
  36.  
  37.     SELECT    an, pn
  38.     FROM aliases
  39.     ORDER BY an, pn
  40.  
  41.      an           pn           
  42.      ------------ ------------ 
  43.      monkees      dave         
  44.      monkees      michael      
  45.      monkees      mickey       
  46.      monkees      peter        
  47.      outs         ghb          
  48.      outs         jdq          
  49.      zak_r        pauls        
  50.  
  51.  
  52.     SELECT    a.an,
  53.         last_added = MIN(a.pn),
  54.         list = CONVERT(varchar(255), MIN(a.pn))
  55.     INTO #exp_aliases
  56.     FROM aliases a
  57.     GROUP BY a.an
  58.  
  59.     DECLARE    @curr_an    varchar(30),
  60.         @nm_addn    varchar(30)
  61.  
  62.     SELECT    @curr_an = MIN(an)
  63.     FROM #exp_aliases
  64.  
  65.     WHILE ( @curr_an IS NOT NULL )
  66.     BEGIN
  67.  
  68.         SELECT @nm_addn = MIN(a.pn)
  69.         FROM aliases a, #exp_aliases e
  70.         WHERE a.pn > e.last_added
  71.           AND a.an = e.an
  72.           AND a.an = @curr_an
  73.           AND e.an = @curr_an
  74.  
  75.         WHILE ( @nm_addn IS NOT NULL )
  76.         BEGIN
  77.  
  78.         UPDATE #exp_aliases
  79.         SET last_added = @nm_addn,
  80.             list = list + ", " + @nm_addn
  81.         WHERE an = @curr_an
  82.  
  83.         SELECT @nm_addn = MIN(a.pn)
  84.         FROM aliases a, #exp_aliases e
  85.         WHERE a.pn > e.last_added
  86.           AND a.an = e.an
  87.           AND a.an = @curr_an
  88.           AND e.an = @curr_an
  89.  
  90.         END
  91.  
  92.         SELECT @curr_an = MIN(an)
  93.         FROM #exp_aliases
  94.         WHERE an > @curr_an
  95.  
  96.     END
  97.  
  98.     SELECT    an, list = SUBSTRING(list, 1, 40)
  99.     FROM #exp_aliases ORDER BY an
  100.  
  101.      an           list                                     
  102.      ------------ ---------------------------------------- 
  103.      monkees      dave, michael, mickey, peter             
  104.      outs         ghb, jdq                                 
  105.      zak_r        pauls                                    
  106.  
  107.  
  108.  
  109. -- 
  110. Robert Garvey robert@sybase.com {sun,lll-tis,pyramid,pacbell}!sybase!robert
  111.           Sybase, Inc   6475 Christie Ave   Emeryville, CA   94608-1010
  112.  
  113. Any opinions to be attributed solely to poster and are unrelated to Sybase.
  114.