|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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 |
![]() |
| Viewing: Dev Articles Community Forums > Databases > Microsoft SQL Server > Difficult SQL Query |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|