Solved

How to auto generate the numbers based on field values.

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

856 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