Solved

pivot table sql 2000 case when statement

Posted on 2010-11-11
8
375 Views
Last Modified: 2012-06-22
SELECT     contactdetails_id, CASE WHEN contactdetials_typeid = 8 THEN contactdetails_value as website, CASE WHEN contactdetials_typeid = 7 THEN contactdetails_value as mobile
FROM         tbl_contactdetails
GROUP BY contactdetails_id

why wont this work?

I am trying to get a list of values (contactdetails_value is varchar) into a pivot table
so that the mobile and website associated with the contactid is on the same row

help




0
Comment
Question by:chrismichalczuk
8 Comments
 

Author Comment

by:chrismichalczuk
Comment Utility
sorry spelling wrong as well in sample !!!

SELECT     contactdetails_id, CASE WHEN contactdetails_typeid = 8 THEN contactdetails_value as website, CASE WHEN contactdetails_typeid = 7 THEN contactdetails_value as mobile
FROM         tbl_contactdetails
GROUP BY contactdetails_id
0
 
LVL 7

Expert Comment

by:mkobrin
Comment Utility
When you say this won't work, are you getting errors with your syntax, or is the result nor what you are expecting?
0
 

Author Comment

by:chrismichalczuk
Comment Utility
SELECT     contactdetails_id, CASE WHEN contactdetails_typeid = 8 THEN contactdetails_value AS Website,
                      CASE WHEN contactdetails_typeid = 7 THEN contactdetails_value AS mobile
FROM         tbl_contactdetails
GROUP BY contactdetails_id

error is INCORRECT SYNTAX near AS

if I remove AS Website get  INCORRECT SYNTAX near ','


heres a sample of the data

ContactDetails_ID      ContactDetails_LinkID      ContactDetails_LinkTypeID      ContactDetails_Value      ContactDetails_TypeID
60547      13234      1      07917154653              7
60548      15675      1      joe@blogs.com      8
                        


0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:chrismichalczuk
Comment Utility
SELECT     contactdetails_Linkid, CASE WHEN contactdetails_typeid = 8 THEN contactdetails_value AS Website,
                      CASE WHEN contactdetails_typeid = 7 THEN contactdetails_value AS mobile
FROM         tbl_contactdetails
GROUP BY contactdetails_Linkid
0
 
LVL 7

Expert Comment

by:mkobrin
Comment Utility
Try adding the END keyword before the AS, as I have shown here

SELECT     contactdetails_id, CASE WHEN contactdetails_typeid = 8 THEN contactdetails_value END AS Website,
                      CASE WHEN contactdetails_typeid = 7 THEN contactdetails_value END AS mobile
FROM         tbl_contactdetails
GROUP BY contactdetails_id
0
 
LVL 51

Accepted Solution

by:
Mark Wills earned 500 total points
Comment Utility
And they will be on two different lines if you simply comment out the group by. So, you also need to then use aggregate functions when using the group by


SELECT     contactdetails_Linkid, max(CASE WHEN contactdetails_typeid = 8 THEN contactdetails_value else ''end) AS Website,
                      max(CASE WHEN contactdetails_typeid = 7 THEN contactdetails_value else '' end) AS mobile
FROM         tbl_contactdetails
GROUP BY contactdetails_Linkid
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
This question has been classified as abandoned and is being closed as part of the Cleanup Program.  See my comment at the end of the question for more details.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now