Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
rb1373
Yak Posting Veteran
93 Posts |
Posted - 2005-01-06 : 15:56:45
|
| When I have duplicates, how do I mark the originals with a 1 and duplicates with a zero? Preferrably I would like the last duplicate in a series of duplicates to be 1. Thanks! - raydesired results:MyCode OriginalA 1B 0B 1C 0C 0C 1CREATE TABLE [dbo].[MyTable1] ( [MyCode] [char] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Original] [int] NULL ) ON [PRIMARY]GOINSERT mytable1 VALUES('A', null) INSERT mytable1 VALUES('B', null) INSERT mytable1 VALUES('B', null) INSERT mytable1 VALUES('C', null) INSERT mytable1 VALUES('C', null) INSERT mytable1 VALUES('C', null) |
|
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2005-01-06 : 16:25:40
|
| Let's back up a bit. Some of these items may appear to be overly pedantic or nit-picky but they should help going forward.1) In the Relational Model every table has a Primary Key which is by definition unique. This prevents the situation that you have.2) There is no "first" or "last" in a table or series. Don't think of a table as a spreadsheet with data neatly arranged in chronological order. Instead, think of a table as a bag of marbles. Note that the marbles are not in a particular order and if you pour the bag out, the marbles can come out in a different order every time.3) If you want to impose an ordering to the data, it must be based on the data itself. since you don't have a unique way to identify a specific row, you can't do this.How to correct this situation?1)You could add an additional column to the table that was either unique by itself or was unique when combined with the MyCode column.OR 2) You could remove the duplicate rows but it does sound like this is what you want to end up with.HTH=================================================================A faith that cannot survive collision with the truth is not worth many regrets. -Arthur C Clarke, science fiction writer (1917- ) |
 |
|
|
ehorn
Master Smack Fu Yak Hacker
1632 Posts |
Posted - 2005-01-06 : 16:27:20
|
| Nicely done Steve. |
 |
|
|
rb1373
Yak Posting Veteran
93 Posts |
Posted - 2005-01-07 : 13:31:02
|
| My mistake. I oversimpled my example. For each code, I want to flag one record with a 1 and all the rest w/ 0's. Thanks. - raydesired results:MyCode Original AddressA 1 A1B 0 B2B 1 B1C 0 C3C 0 C2C 1 C1CREATE TABLE [dbo].[MyTable1] ( [MyID] [int] IDENTITY (1, 1) NOT NULL , [MyCode] [char] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Original] [int] NULL , [Address] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]GOINSERT mytable1 VALUES('A', null, 'A1') INSERT mytable1 VALUES('B', null, 'B2') INSERT mytable1 VALUES('B', null, 'B1') INSERT mytable1 VALUES('C', null, 'C3') INSERT mytable1 VALUES('C', null, 'C2') INSERT mytable1 VALUES('C', null, 'C1') |
 |
|
|
VIG
Yak Posting Veteran
86 Posts |
Posted - 2005-01-07 : 17:05:54
|
| select t.mycode ,case when t2.address is null then 0 else 1 end Original ,t.addressfrom mytable1 tleft join(select mycode,min(address) address from mytable1 group by mycode ) t2on t.mycode=t2.mycode and t.address =t2.address |
 |
|
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2005-01-07 : 19:33:12
|
| VIG,Where is this "address" column coming from?EHorn,Thank-you.HTH=================================================================A faith that cannot survive collision with the truth is not worth many regrets. -Arthur C Clarke, science fiction writer (1917- ) |
 |
|
|
|
|
|
|
|