SQL number-sequence generation

When I do exercise in http://www.sql-ex.ru/learn_exercises.php#answer_ref 104(2)

table classes:

class type country numGuns bore displacement
Bismarck bb Germany 8 15 42000
Iowa bb USA 9 16 46000
Kongo bc Japan 8 14 32000
North   Carolina bb USA 12 16 37000
Renown bc Gt.Britain 6 15 32000
Revenge bb Gt.Britain 8 15 29000
Tennessee bb USA 12 14 32000
Yamato bb Japan 9 18 65000

Questions:

For each cruiser class, make numbering (sequentially from 1) of available
guns.
Output: class name, number of a gun in the format 'bc-N'.

Answer:

the right sql:

with cte_n(num) as

(select 1  

union all  

select num+1  

from cte_n  

where num<100 )

select cs.class ,type+'-'+cast(cte_n.num as varchar(10)) as num

from classes cs join

cte_n on cte_n.num<=cs.numGuns

where type='bc'

原文地址:https://www.cnblogs.com/lxxlovekang/p/3268027.html