Solved

How to auto generate the numbers based on field values.

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
average of calculation (TSQL) 4 33
Using this function 4 39
Can I skip a node in XML? 9 31
SQL works but want to get the XML node data separately 11 26
     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 …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
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…

740 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