Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Please help me in this scenario.

table 1
id|name|age|status
1 |red |10 | m
2 |ben |30 | s
3 |app |90 | s
4 |low |10 | s
5 |son |50 | g


table 2
id|master|status
10|chuka | s
20|pogi  | b
30|bell  | t


If I will create a new table/query it looks like this?

SQL
DECLARE @power INT
SET @power = 40;
SELECT @power AS Level5, name, status, age 
FROM table1 inner join table2 on table2.status = table1.status
WHERE table1.status = 's'

The output is looks like this
table 3

Level5|name|status|age
40|ben | s    |30
40|app | s    |90
40|low | s    |10


My problem is, I want to make the column Level5 will increment by 10 for every row

Expected OUTPUT I want to display

Level5|name|status|age
50|ben | s    |30
60|app | s    |90
70|low | s    |10


Please Help me! Thank you!
Posted
Updated 28-Jul-13 19:13pm
v2

You can do this with a help of recursive CTE[^]

SQL
;WITH CTE AS(
  SELECT @power AS Level5, 1 AS rn
  UNION ALL
  SELECT Level5 + 10, rn + 1 FROM cte WHERE rn < 100
  )
SELECT CTE.Level5, t.name, t.status, t.age FROM
(SELECT name, table2.status, age,
ROW_NUMBER() OVER (ORDER BY table1.id) AS rn
FROM table1 inner join table2 on table2.status = table1.status
WHERE table2.status = 's') AS t
INNER JOIN CTE ON t.rn=cte.rn


SQL Fiddle[^]
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900