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 |
|
Tommm83
Starting Member
4 Posts |
Posted - 2011-05-10 : 06:39:00
|
| Hi,I am having real trouble creating a script to create a unique code for a product.I have created a four letter prefix using a substring and added this to my table, but I want to append a four digit number after this to make the code unique.My data now looks like the following:CODE DESCAPPL Apple GreenAPPL Apple RedORAN Orange BigORAN Orange SmallHowever I want to make it look like this:CODE DESCAPPL0001 Apple GreenAPPL0002 Apple RedORAN0001 Orange BigORAN0002 Orange SmallI obviously need to iterate through somehow to do this depending on the number of duplicate codes, but I can't work out how to do it!Can anyone help?Thanks,Tom |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2011-05-10 : 07:16:20
|
| This looks like homework. What have you tried so far?JimEveryday I learn something that somebody else already knew |
 |
|
|
Tommm83
Starting Member
4 Posts |
Posted - 2011-05-10 : 07:37:21
|
| Definately not homework! I've just simplified it all down to make the question a little clearer.The bones of what I have done is below. I just can't work out the iteration part to make it append the numbers. The actual data looks a bit like this:#MATERIALPREFIX:Prefix Desc7ORM 7 or moreADDI Additional WCADEQ AdequateALLT All typesALLT All types (except decs)ALLT All types (HH)#COUNTOFDUPLICATES:Prefix TotalCountALLT 3ALRE 4BLOC 2BRIC 6I also have the issue of only being able to create my own temporary tables as I am not allowed write access to the main tables.Hope this makes sense,Tom------------------------------select * into #countofduplicates from( SELECT Prefix, COUNT(*) as TotalCount FROM #materialprefix GROUP BY Prefix HAVING COUNT(*) > 1) as tdeclare @i intdeclare @rows intset @i = 1set @rows = @@rowcountwhile @i < @rowsbegin update #countofduplicates set totalcount = totalcount *2 where @@rowcount = @i set @i = @i + 1endselect * from #countofduplicates |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2011-05-10 : 07:39:29
|
| For your original 'bare bones' version, try this. we'd need better sample data and expected output to make any further changes.DECLARE @Table TABLE(Code varchar(6),[Desc] varchar(50))INSERT INTO @TableSELECT 'APPL','Apple Green' UNION ALLSELECT 'APPL','Apple Red' UNION ALLSELECT 'ORAN','Orange Big' UNION ALLSELECT 'ORAN','Orange Small'SELECT CODE + RIGHT('0000'+convert(varchar(10),rank() over(partition by code order by [Desc])),4)FROM @TableEveryday I learn something that somebody else already knew |
 |
|
|
Tommm83
Starting Member
4 Posts |
Posted - 2011-05-10 : 07:48:01
|
| Based on the data sets I provided above (I hope those are better), I have done the following:SELECT Prefix + RIGHT ( '0000'+convert(varchar(10),rank() over(partition by Prefix order by Prefix)),4 )FROM #materialprefixThis has appended '0001' to the end of everything. It's definately heading in the right direction, its just making the unique entries now.The resulting dataset looks like this:7ORM0001ADDI0001ADEQ0001ALLT0001ALLT0001ALLT0001ALRE0001 |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2011-05-10 : 07:57:57
|
| You partitioned by the prefix and ordered by the prefix, so everything is ranked #1. Try partitioning by the prefix and ordering by the desc.JimEveryday I learn something that somebody else already knew |
 |
|
|
Tommm83
Starting Member
4 Posts |
Posted - 2011-05-10 : 08:03:39
|
| My mistake!That's perfect! Never used rank() and partitioning before! Fantastic stuff.Thanks a lot,Tom |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2011-05-10 : 08:10:49
|
| The over() clause and the windowed functions are pretty cool. Also look at dense_rank() and row_number().JimEveryday I learn something that somebody else already knew |
 |
|
|
|
|
|
|
|