Monday, January 4, 2021

SQL12 Generate duplicate records as per the count of sequence

Want to generate dupkciate ID in sequence, 

For 1 , generate 1 record
For 2 , generate 2 records
For 3 , generate 3 records
For 4 , generate 4 records
and so on

these records will be generated all in sequence.

Output Needed
id
1
2
2
3
3
3
4
4
4
4


--Oracle
WITH SQL12
AS (
	SELECT LEVEL col
	FROM dual connect BY LEVEL < 5
	)
SELECT a.*
FROM SQL12 a
JOIN SQL12 b ON b.col <= a.col;

--SQL Server
WITH SQL12 (N)
AS (
	SELECT 1
	
	UNION ALL
	
	SELECT N + 1
	FROM SQL12
	WHERE N < 4
	)
SELECT a.*
FROM SQL12 a
JOIN SQL12 b ON b.N <= a.N
ORDER BY 1;

No comments:

Post a Comment

web stats