SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Simple derived table with multiple rows


Simple derived table with multiple rows

Author
Message
memymasta
memymasta
SSC Veteran
SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)

Group: General Forum Members
Points: 209 Visits: 402
I'm having trouble creating a simple derived table. It is so simple to create a derived table with multiple columns with:
SELECT 1,2,3 


Feels like it shouldn't be much more difficult to create a derived table with one column and multiple tuples/rows.


Is there a way to get following result without creating a temp table or specifying the whole DDL:

derived table
1
2
3

Thanks
Mark Cowne
Mark Cowne
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3113 Visits: 24089
Two possibilities, I'm sure there are more


SELECT 1 AS col
UNION ALL
SELECT 2
UNION ALL
SELECT 3




SELECT col
FROM (VALUES (1), (2), (3)) x(col)



____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39493 Visits: 38553
memymasta (5/10/2013)
I'm having trouble creating a simple derived table. It is so simple to create a derived table with multiple columns with:
SELECT 1,2,3 


Feels like it shouldn't be much more difficult to create a derived table with one column and multiple tuples/rows.


Is there a way to get following result without creating a temp table or specifying the whole DDL:

derived table
1
2
3

Thanks


There is a limit to how many rows you can do this way, but here is one:



select * from
(select n from (values (1),(2),(3))dt(n))dt1;




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)

Group: General Forum Members
Points: 86376 Visits: 41098
memymasta (5/10/2013)
I'm having trouble creating a simple derived table. It is so simple to create a derived table with multiple columns with:
SELECT 1,2,3 


Feels like it shouldn't be much more difficult to create a derived table with one column and multiple tuples/rows.


Is there a way to get following result without creating a temp table or specifying the whole DDL:

derived table
1
2
3

Thanks


Now that the others have answered, let me ask...

I notice the numbers are sequential. Is that just by coincidence or do you really need a derived table of sequential numbers?

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
memymasta
memymasta
SSC Veteran
SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)

Group: General Forum Members
Points: 209 Visits: 402
Thanks for the answers, seems like Mark-101232 had the shortest one.
SELECT col FROM (VALUES (1), (2), (3)) x(col)



@Jeff Moden - Not really, they just happens to be sequential, we use a fixed number of periods (3), that are not in the DB. I needed to join those with another table to get a desired result.

Your interest in sequential numbers, were you thinking of a tally table?
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search