SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Arrays


Arrays

Author
Message
gofigurefw
gofigurefw
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 2
I am kinda new to querying.

Scenario:
I have two columns. (Item-Column1) and (Position-Column2).
The Item column has many different duplicate records.

I am trying to insert value in multiples of 10, in the Position column as it searches for each duplicate record (in column1).

I remember doing this in college, but I need a little reminder... Thanks for any help!
Example is below.

Example:
~~~~~~~~~~
Current:

Column1 Column2
-------- --------
ABC {null}
ABC {null}
XYZ {null}
ABC {null}
XYZ {null}
XYZ {null}
ABC {null}
HIJ {null}
ABC {null}
==================================
After:

Column1 Column2
-------- --------
ABC 10
ABC 20
ABC 30
ABC 40
ABC 50
XYZ 10
XYZ 20
XYZ 30
HIJ 10
karthik M
karthik M
SSCertifiable
SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)

Group: General Forum Members
Points: 6741 Visits: 2586
Hey,

Try this code.

Create table #name
(
Name varchar(5),
Value int null
)
go
insert into #name (Name)
select 'ABC'
union all
select 'ABC'
union all
select 'ABC'
union all
select 'ABC'
union all
select 'ABC'
union all
select 'ABC'
union all
select 'ABC'
union all
select 'ABC'
union all
select 'ABC'
union all
select 'ABC'
union all
select 'ABC'
union all
select 'ABC'
union all
select 'ABC'
union all
select 'ABC'
union all
select 'ABC'
union all
select 'XYZ'
union all
select 'XYZ'
union all
select 'XYZ'
union all
select 'XYZ'
union all
select 'MNO'
union all
select 'PQR'
union all
select 'PQR'

go

select Name,count(Name) as Cnt
into #Cnt
from #name
group by Name

Select Name,N*10
from #Cnt,Tally
where N <= Cnt

karthik
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (212K reputation)SSC Guru (212K reputation)SSC Guru (212K reputation)SSC Guru (212K reputation)SSC Guru (212K reputation)SSC Guru (212K reputation)SSC Guru (212K reputation)SSC Guru (212K reputation)

Group: General Forum Members
Points: 212507 Visits: 41977
You can get by without creating the extra temp table...


SELECT d.Name,t.N*10 AS DeciCount
FROM (SELECT Name,COUNT(*) AS TheCount
FROM #Name
GROUP BY Name) d
INNER JOIN dbo.Tally t
ON t.N <= d.TheCount



GoFigureFW... if you don't know what a Tally table is, please see the following link...

http://www.sqlservercentral.com/articles/TSQL/62867/

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
karthik M
karthik M
SSCertifiable
SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)

Group: General Forum Members
Points: 6741 Visits: 2586
Thanks Jeff !

karthik
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search