Solved

How to auto generate the numbers based on field values.

Posted on 2013-06-23
5
296 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
  • 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
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…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now