home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!wupost!cs.utexas.edu!sun-barr!ames!agate!ucbvax!mtxinu!sybase!robert
- From: robert@sybase.com (Robert Garvey)
- Newsgroups: comp.databases.sybase
- Subject: Re: Help on SQL query
- Message-ID: <27727@sybase.sybase.com>
- Date: 7 Jan 93 21:36:12 GMT
- References: <1993Jan4.164204.21989@bmw.mayo.edu> <27661@sybase.sybase.com>
- Sender: news@Sybase.COM
- Organization: Emeryville Tupleware, Party Planning Division
- Lines: 93
-
- In article <27661@sybase.sybase.com>, I wrote:
- |> [...method of expanding out an alias stored as alias/member pairs...]
-
- An alternative is to handle all aliases in one loop, going through the
- loop n-1 times where n is the maximum number of members in the aliases.
-
- SELECT an, pn
- FROM aliases
- ORDER BY an, pn
-
- an pn
- ------------------------------ ------------------------------
- dwarves bashful
- dwarves doc
- dwarves dopey
- dwarves grumpy
- dwarves happy
- dwarves sleepy
- dwarves sneezy
- twain clemens
- weapons fear
- weapons surprise
-
-
- SELECT a.an,
- pn_cnt = COUNT(*),
- cur_cnt = CONVERT(int, 1),
- last_added = MIN(a.pn),
- list = CONVERT(varchar(255), MIN(a.pn))
- INTO #exp_aliases
- FROM aliases a
- GROUP BY a.an
-
- DECLARE @cur_count int,
- @max_count int
-
- SELECT @cur_count = 2,
- @max_count = MAX(pn_cnt)
- FROM #exp_aliases
-
- SELECT an,
- pn
- INTO #next_pns
- FROM aliases
- WHERE 1 = 0
-
- WHILE ( @cur_count <= @max_count )
- BEGIN
-
- INSERT INTO #next_pns ( an, pn )
- SELECT a.an,
- MIN(a.pn)
- FROM aliases a, #exp_aliases e
- WHERE a.pn > e.last_added
- AND a.an = e.an
- AND e.pn_cnt >= @cur_count
- GROUP BY a.an
- HAVING MIN(a.pn) IS NOT NULL
-
- INSERT INTO #exp_aliases ( an, pn_cnt, cur_cnt, last_added, list )
- SELECT e.an,
- e.pn_cnt,
- @cur_count,
- n.pn,
- e.list + ", " + n.pn
- FROM #exp_aliases e, #next_pns n
- WHERE e.an = n.an
-
- DELETE #next_pns
-
- DELETE #exp_aliases
- WHERE cur_cnt < pn_cnt
- AND cur_cnt < @cur_count
-
- SELECT @cur_count = @cur_count + 1
-
- END
-
- SELECT an = SUBSTRING(an, 1, 8),
- list = SUBSTRING(list, 1, 50)
- FROM #exp_aliases ORDER BY an
-
- an list
- -------- --------------------------------------------------
- dwarves bashful, doc, dopey, grumpy, happy, sleepy, sneezy
- twain clemens
- weapons fear, surprise
-
- --
- Robert Garvey robert@sybase.com {sun,lll-tis,pyramid,pacbell}!sybase!robert
- Sybase, Inc 6475 Christie Ave Emeryville, CA 94608-1010
-
- I'm the only one talking here. Opinions are not necessarily those of Sybase.
-