SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Getting 2 rows of output out of one row
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mpruett
Starting Member

USA
1 Posts

Posted - 07/22/2005 :  16:47:25  Show Profile  Reply with Quote
I have a problem- I have data that looks something like this:

ID#, lastname1, firstname1, lastname2, firstname2

and I'd like output similar to:

ID#1 lastname1, firstname1
ID#1 lastname2, firstname2
ID#2 lastname1, firstname1
ID#2 lastname2, firstname2

How can I do this without moving my data into new tables or using temp-tables?

Thanks,

Mark

AjarnMark
SQL Slashing Gunting Master

USA
3246 Posts

Posted - 07/22/2005 :  16:53:43  Show Profile  Visit AjarnMark's Homepage  Reply with Quote
With a UNION...

SELECT ID, lastname1, firstname1 FROM myTable
UNION ALL
SELECT ID, lastname2, firstname2 FROM myTable
ORDER BY ID

---------------------------
EmeraldCityDomains.com
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22761 Posts

Posted - 07/25/2005 :  01:22:09  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
Small modification of Ajarn code
(Select * from (SELECT ID, lastname1, firstname1 FROM myTable
UNION ALL
SELECT ID, lastname2, firstname2 FROM myTable ) T
ORDER BY ID


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

AjarnMark
SQL Slashing Gunting Master

USA
3246 Posts

Posted - 07/29/2005 :  13:13:47  Show Profile  Visit AjarnMark's Homepage  Reply with Quote
Madhivan, why the modification? The ORDER BY in my clause does not get applied until after the union is performed anyway. Is this a difference in versions? Or just for clarity of code?

---------------------------
EmeraldCityDomains.com
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22761 Posts

Posted - 08/01/2005 :  00:59:23  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
>>Or just for clarity of code?

Yes

If he use Order by in First statement, he will get error

SELECT ID, lastname1, firstname1 FROM myTable
ORDER BY ID
UNION ALL
SELECT ID, lastname2, firstname2 FROM myTable


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

AjarnMark
SQL Slashing Gunting Master

USA
3246 Posts

Posted - 08/01/2005 :  17:36:44  Show Profile  Visit AjarnMark's Homepage  Reply with Quote
Yep! That's how I learned about it.

---------------------------
EmeraldCityDomains.com
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000