Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old August 12th, 2005, 09:07 AM
Martyn Martyn is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 1 Martyn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 39 m 28 sec
Reputation Power: 0
Question Difficult SQL Query

I've a bit of a nasty SQL question here - was wondering if anyone can help me cos i have no idea!

I have 2 tables

TableA:
UserID
Name
Company
jobTitle

TableB
ResultID
QuestionID
UserID
Result

I want a SQL statement that returns these results

TableA.UserID, TableA.Name, TableA.Company, TableA.jobTitle,
TableB.Result (Where TableB.userID = TableA.UserID AND
TableB.QuestionID = 4), TableB.Result (Where TableB.userID =
TableA.UserID AND TableB.QuestionID =5), TableB.Result (Where
TableB.userID = TableA.UserID AND TableB.QuestionID = 6),
TableB.Result (Where TableB.userID = TableA.UserID AND
TableB.QuestionID = 12)

And i have no idea of how to go about doing that.

can anyone help??

thank you

Reply With Quote
  #2  
Old August 14th, 2005, 11:36 AM
gurolerdogan gurolerdogan is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 1 gurolerdogan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 m 26 sec
Reputation Power: 0
not that nasty :)

hi,
You can use inner queries like :


-----------
SELECT TableA.*,
(SELECT Result FROM TableB WHERE TableB.userID = TableA.UserID AND TableB.QuestionID = 4) AS Q4Results ,
(SELECT Result FROM TableB WHERE TableB.userID = TableA.UserID AND TableB.QuestionID = 5) AS Q5Results ,
(SELECT Result FROM TableB WHERE TableB.userID = TableA.UserID AND TableB.QuestionID = 6) AS Q6Results ,
(SELECT Result FROM TableB WHERE TableB.userID = TableA.UserID AND TableB.QuestionID = 12) AS Q12Results

FROM TableA

------------

hope that helps

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMicrosoft SQL Server > Difficult SQL Query


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT