Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to generate rownumber in sql query in sequence like 1,2,4,8,16....n


What I have tried:

how to generate rownumber in sql query in sequence like 1,2,4,8,16....n
Posted
Updated 8-Nov-21 20:01pm

1 solution

See here: SQL Server POWER() Function[^] But ... remember that SQL integers are 32 bit, so if you have more than 31 records it will fail with an arithmetic overflow error, and that you need to raise by the row number minus one.
 
Share this answer
 
Comments
Sachin Kulkarni B 9-Nov-21 2:47am    
Thankyou for the quick response
OriginalGriff 9-Nov-21 3:26am    
You're welcome!
Richard Deeming 9-Nov-21 5:41am    
NB: ROW_NUMBER[^] returns a bigint. POWER[^] works on floats, including bigint.

So long as you use POWER(CAST(2 As bigint), ..., you can get up to 63 rows before the overflow error. :)

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