home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!wupost!howland.reston.ans.net!sol.ctr.columbia.edu!hamblin.math.byu.edu!yvax.byu.edu!cunyvm!dlvgc
- Newsgroups: comp.databases.sybase
- Subject: Re: SQL select distinct problem
- Message-ID: <92347.185350DLVGC@CUNYVM.BITNET>
- From: Dimitri Vulis <DLVGC@CUNYVM.BITNET>
- Date: Saturday, 12 Dec 1992 18:53:50 EST
- References: <1992Dec9.150551.3636@ctp.com>
- Organization: City University of New York/ University Computer Center
- Lines: 35
-
- In article <1992Dec9.150551.3636@ctp.com>, sreit@ctp.com (Stefan Reitshamer)
- says:
- >I have a bunch of rows with sex/rel codes and names from which I want
- >to select a distinct set of sex/rel codes and the name associated with
- >each of the codes.
- >
- >For example:
- >
- >create table #tmp
- > (
- > sex_rel varchar(2) NULL,
- > name varchar(10) NULL
- > )
- >insert #tmp values("A","Jim")
- >insert #tmp values("B","Sue")
- >insert #tmp values("B","Susie")
- >insert #tmp values("C","Bobby")
- >
- >select distinct sex_rel from #tmp <-- For this distinct list
- >("A","B","C")
- > I want the name associated with the
- > first occurrence of the code
- > ("Jim","Sue","Bobby").
- >
- >Is this possible? What would the select statement look like? Some kind of
- >join? Help!
- Here's yet another solution:
- Create table #t1 (sex_rel char(1),name(...)
- insert #t1 select distinct sex_rel,"" from #tmp
- update #t1 from #tmp set #t1.name=#tmp.name where
- #t1.sex_rel=#tmp.sex_rel
- on the plus side, it doesn't use WHILE, on the minus side,
- the name won't be first (but this is counter-SQL anyway).
-
- Dimitri
-