Welcome guys..

This is programmer sharing his collection with all you ppl out there. This blog contains complete codes of java , c , c++ , unix , java script , applets , swing for learning purpose only. I try to add approx 10 new complete codes daily. Lets share out knowledge and materials in here. All your comments and votes are most welcomed.

Get your codes from..

Saturday, May 10, 2008

GET IDENTTITY USING CROSS JOIN AND SUB QUERY

Compatibility:- SQL Server 7,6.5 & earlier

set nocount on
go
create table #test (description char(5))
insert #test values('aaaaa')
insert #test values('baaab')
insert #test values('haaab')
insert #test values('mmmmm')
insert #test values('taaar')
insert #test values('xxxxz')
insert #test values('zzzza')
go
-- generate sequence using cross join. j
-- ust an example, may not be the best way
-- always
select t1.description,
sum(case when t2.description <= t1.description then 1 else 0 end) as sequence
from #test t1 cross join #test t2
group by t1.description
order by t1.description
-- using a sub-query...
select t1.description,
(select count(*) from #test t2 where t2.description <= t1.description) as sequence
from #test t1
order by t1.description

No comments:

Project Source Codes