Click here to monitor SSC
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 Journeyman
SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)

Group: General Forum Members
Points: 99 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
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2093 Visits: 22800
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-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24206 Visits: 37966
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-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45113 Visits: 39916
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

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

Group: General Forum Members
Points: 99 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