I want to print or display 1 to 10 or any max number in two column format using MS Sql-Server query. Just like below attached screen shot image.
So please give any suggestion.
I want to print or display 1 to 10 or any max number in two column format using MS Sql-Server query. Just like below attached screen shot image.
So please give any suggestion.
5条答案
按热度按时间k2fxgqgv1#
I like to use recursive queries for this:
You control the maximum number with the inequality condition in the recursive member of the cte.
If you need to generate more than 100 rows, you need to add
option(maxrecursion 0)
at the very end of the query.gudnpqoy2#
Using a couple of inline tallies would be way faster than a
WHILE
. This version will go up to 1000 integers (500 rows):An alternative way that looks less messy with the
CASE
andTOP
would be to use a couple of CTEs:d6kp6zgx3#
Assuming you are starting with a table with one column, you can use:
vcudknz34#
Alternatively, set-based solution using window functions:
the intermediate result of
sample_data_split
is:and then to get the resultset into a desired format:
Those CTEs can be merged into a single SELECT:
The positive side of a such approach, that it scales well and has no upper boundary on how much rows to be processed
uurv41yg5#
So I got this solution on it as below...