home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!spool.mu.edu!agate!ucbvax!mtxinu!sybase!robert
- From: robert@sybase.com (Robert Garvey)
- Newsgroups: comp.databases.sybase
- Subject: Re: Help on SQL query
- Message-ID: <27661@sybase.sybase.com>
- Date: 6 Jan 93 20:09:10 GMT
- References: <1993Jan4.164204.21989@bmw.mayo.edu>
- Sender: news@Sybase.COM
- Organization: Entities & Beer
- Lines: 102
-
- In article <1993Jan4.164204.21989@bmw.mayo.edu>, roder@mayo.edu (Mark N. Roder) writes:
- |>
- |> I need help on a SQL query. I am working on a application to track
- |> SMTP mail aliases and need to convert a table of aliases to "/etc/aliases"
- |> file format.
- |>
- |> What I have:
- |>
- |> an = alias name. alphanumeric(i.e. last.first, sas_users,etc)
- |> pn = person name. alphanumeric(i.e. login_name@host, "|lpr
- |>
- |> What I want:("/etc/aliases" format)
- |>
- |> a1: p1
- |> a2: p2,p1,p3
- |> a4: p3,p6
- |> a5: p8
- |>
- |>
- |> Any thoughts on this? I would like to keep this straight SQL on
- |> the Sybase server since it would make distribution of the information a
- |> lot easier.
-
- With flow of control in Transact-SQL, it could be done like this:
-
- SELECT an, pn
- FROM aliases
- ORDER BY an, pn
-
- an pn
- ------------ ------------
- monkees dave
- monkees michael
- monkees mickey
- monkees peter
- outs ghb
- outs jdq
- zak_r pauls
-
-
- SELECT a.an,
- last_added = MIN(a.pn),
- list = CONVERT(varchar(255), MIN(a.pn))
- INTO #exp_aliases
- FROM aliases a
- GROUP BY a.an
-
- DECLARE @curr_an varchar(30),
- @nm_addn varchar(30)
-
- SELECT @curr_an = MIN(an)
- FROM #exp_aliases
-
- WHILE ( @curr_an IS NOT NULL )
- BEGIN
-
- SELECT @nm_addn = MIN(a.pn)
- FROM aliases a, #exp_aliases e
- WHERE a.pn > e.last_added
- AND a.an = e.an
- AND a.an = @curr_an
- AND e.an = @curr_an
-
- WHILE ( @nm_addn IS NOT NULL )
- BEGIN
-
- UPDATE #exp_aliases
- SET last_added = @nm_addn,
- list = list + ", " + @nm_addn
- WHERE an = @curr_an
-
- SELECT @nm_addn = MIN(a.pn)
- FROM aliases a, #exp_aliases e
- WHERE a.pn > e.last_added
- AND a.an = e.an
- AND a.an = @curr_an
- AND e.an = @curr_an
-
- END
-
- SELECT @curr_an = MIN(an)
- FROM #exp_aliases
- WHERE an > @curr_an
-
- END
-
- SELECT an, list = SUBSTRING(list, 1, 40)
- FROM #exp_aliases ORDER BY an
-
- an list
- ------------ ----------------------------------------
- monkees dave, michael, mickey, peter
- outs ghb, jdq
- zak_r pauls
-
-
-
- --
- Robert Garvey robert@sybase.com {sun,lll-tis,pyramid,pacbell}!sybase!robert
- Sybase, Inc 6475 Christie Ave Emeryville, CA 94608-1010
-
- Any opinions to be attributed solely to poster and are unrelated to Sybase.
-