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
 General SQL Server Forums
 New to SQL Server Programming
 Changing a row to a column

Author  Topic 

LOLCatLady
Starting Member

24 Posts

Posted - 2012-02-24 : 14:55:43
I would like to pull data from the same field with different criteria into two columns, not rows.

I have this select:
SELECT memid, memamount
FROM memories
WHERE memname = 'FYCt'
AND memamount > '3'
UNION

SELECT memid, memamount
FROM memories
WHERE memname = 'CFYT'
ORDER BY memid

Which gives me data that looks like this:
memid memamount
0000000072 6.00
0000000072 1300.00
0000000097 6.00
0000000097 200.00
0000000393 25.00
0000000539 100.00
0000000550 150.00
0000000550 500.00
0000000582 100.00
0000000590 30.00

Is there a way to have two columns: FYCt and CFYT with the data per record? Like this:
memid FYCt CFYT
72 6 1300
97 6 200
393 25
539 100
550 150 500
582 100
590 30

I appreciate any help! Thanks.

X002548
Not Just a Number

15586 Posts

Posted - 2012-02-24 : 15:21:31
[code]

SELECT m1.memid, m2.memamount, m3.memamount
FROM (SELECT DISTINCT memid FROM memories WHERE memname IN ('CFYT', 'FYCt')
LEFT JOIN memories m2
ON m1.memid = m2.memid
LEFT JOIN memories m3
ON m1.memid = m3.memid
WHERE m2.memname = 'FYCt'
AND m2.memamount > '3'
AND m3.memname = 'CFYT'

[/code]

????



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

LOLCatLady
Starting Member

24 Posts

Posted - 2012-02-24 : 15:42:15
[quote]Originally posted by X002548



SELECT m1.memid, m2.memamount, m3.memamount
FROM (SELECT DISTINCT memid FROM memories WHERE memname IN ('CFYT', 'FYCt')
LEFT JOIN memories m2
ON m1.memid = m2.memid
LEFT JOIN memories m3
ON m1.memid = m3.memid
WHERE m2.memname = 'FYCt'
AND m2.memamount > '3'
AND m3.memname = 'CFYT'



????



Brett

8-)

I get an Incorrect sytnax near the keyword 'LEFT' error. I added a closed parens at the end of the select statement, but that didn't help. I'm continuing to ponder what you sent to see if I can figure out what's wrong with the syntax. THANKS!
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-02-24 : 15:46:27
D'oh


SELECT m1.memid, m2.memamount, m3.memamount
FROM (SELECT DISTINCT memid FROM memories WHERE memname IN ('CFYT', 'FYCt') AS m1
LEFT JOIN memories m2
ON m1.memid = m2.memid
LEFT JOIN memories m3
ON m1.memid = m3.memid
WHERE m2.memname = 'FYCt'
AND m2.memamount > '3'
AND m3.memname = 'CFYT'


Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

LOLCatLady
Starting Member

24 Posts

Posted - 2012-02-24 : 15:50:54
That was it! D'oh is right.

Thanks so much for your help, Brett. I hope you have a great weekend!

Charleen
Go to Top of Page
   

- Advertisement -