Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
SQLNewbie writes "I have 2 tables.Table 1 is like this:Col1ABCTable 2 is like this:Col2123How do I write a query to get a result like this?Col1 Col2A 1B 2C 3"
nr
SQLTeam MVY
12543 Posts
Posted - 2005-11-03 : 08:19:53
select a.Col1, b.Col2from (select Col1, seq = (select count(*) from tbl1 t1 where t1.Col1 <= t.Col1) from tbl1 t) ajoin (select Col2, seq = (select count(*) from tbl2 t1 where t1.Col2 <= t.Col2) from tbl2 t) bon a.seq = b.seq==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy.
Hippi
Yak Posting Veteran
63 Posts
Posted - 2005-11-03 : 21:25:49
For this question, you don't need the second table .Select t1.Col1, (select count(*) from yourTable t2 where t2.Col1<=t1.Col1) as Col2from yourTable t1Hippi
madhivanan
Premature Yak Congratulator
22864 Posts
Posted - 2005-11-04 : 00:54:15
quote:Originally posted by Hippi For this question, you don't need the second table .Select t1.Col1, (select count(*) from yourTable t2 where t2.Col1<=t1.Col1) as Col2from yourTable t1Hippi
If table2 has values like234then yours wont work MadhivananFailing to plan is Planning to fail