Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Getting 2 rows of output out of one row

Author  Topic 

mpruett
Starting Member

1 Post

Posted - 2005-07-22 : 16:47:25
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

3246 Posts

Posted - 2005-07-22 : 16:53:43
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

22864 Posts

Posted - 2005-07-25 : 01:22:09
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

3246 Posts

Posted - 2005-07-29 : 13:13:47
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

22864 Posts

Posted - 2005-08-01 : 00:59:23
>>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

3246 Posts

Posted - 2005-08-01 : 17:36:44
Yep! That's how I learned about it.

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

- Advertisement -