Solved

How to auto generate the numbers based on field values.

Posted on 2013-06-23
5
303 Views
Last Modified: 2013-06-23
The sample data is attached.
I have a field course, if i add on the field id bigint identity(1,1) and insert the records all the data will get autonumbering.

But my requirement is the numbering should restart at every course change.
For example the course list is
BBA   1
BCA   1
BBA   2
B.SC   1
BBA   3
BCA    2

The order should continue from the last number for every course.
Test-Data.xlsx
0
Comment
Question by:searchsanjaysharma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39269203
that is not the purpose of an identity, which will produce a unique number for every row

what is the intended purpose for this number field?

try this on the table holding that information, do these produce the number series you want?
select
  course
, dense_rank() over (partition by course order by SrNo) as by_rank
, row_number() over (partition by course order by SrNo) as by_rownumber
from table1
order by
  course

Open in new window

see it at http://sqlfiddle.com/#!3/a20d2/3
0
 

Author Comment

by:searchsanjaysharma
ID: 39269254
Excellent.

Now if i add additional column adno of type varchar(200), where the adno would be
adno=Course+str(rank)

How do i update it
update testdata set adno=course+str( ? )
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 39269293
I would recommend using row_number()

& I would also recommend using a view instead, but if you really want to add an additional column yes you would use update like this
update table1
set adno = (
          select
          course + convert(varchar,by_rownumber)
          from (
                select
                  course
                , row_number() over (partition by course order by SrNo) as by_rownumber
                , srno
                from table1
               ) as t
          where t.srno = table1.srno
        )
;

Open in new window

0
 
LVL 25

Expert Comment

by:jogos
ID: 39269934
I support PortletPaul in its row_number sollution.

For the update itself I suggest a little other formulation (but 90% PortletPauls' update)
update t set t.adno = 
          t.course + convert(nvarchar,t2.by_rownumber)
from table1 as t
inner join  (
                select
                  course
                , row_number() over (partition by course order by SrNo) as by_rownumber
                , srno
                from table1
               ) as t2
          on t2.srno = t.srno
;

Open in new window

Why? If it's complex and particular when you don't are that strong in sql it is easier to start with just building the select and then change it to an update
So previous update came from the update with changing only 1 line
select t.*, 
          t.course + convert(nvarchar,t2.by_rownumber)
from table1 as t
inner join  (
                select
                  course
                , row_number() over (partition by course order by SrNo) as by_rownumber
                , srno
                from table1
               ) as t2
          on t2.srno = t.srno
;

Open in new window

0
 

Author Closing Comment

by:searchsanjaysharma
ID: 39269999
tx
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question