Solved

How to auto generate the numbers based on field values.

Posted on 2013-06-23
5
304 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 49

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 49

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
In this article I will describe the Copy Database Wizard 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.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

627 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