![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
That was what I tried. But I don't seem to get all of them. If I do a UNION, I get 11 records. If I do each separately, I get 6 for employees and 5 for customers. But if I do the left Joins, I am only getting 7 records. Can't figure out why that is not getting the other 4. |
#3
| |||
| |||
|
|
That was what I figured but was curious if there was a better way, performance wise. I have 2 queries I need to do. One like the one I just did and one that also shows the rows that didn't have any answers. If I was doing it without the UNION, I would just look at rows with nulls in both sets of columns. But what is the best way using a UNION? |
#4
| ||||
| ||||
|
|
"tshad" <tfs (AT) dslextreme (DOT) com> wrote in message news:q5gxo.6227$yw.2591 (AT) newsfe18 (DOT) iad... That was what I figured but was curious if there was a better way, performance wise. I have 2 queries I need to do. One like the one I just did and one that also shows the rows that didn't have any answers. If I was doing it without the UNION, I would just look at rows with nulls in both sets of columns. But what is the best way using a UNION? Do you mean questions without any answers, or answers that have not been chosen? By the former, I assume you mean questions where none of the answers have |
|
If the former, here's one way of doing it. Select the union of all questions that have been answered by customer or empoyee Then exclude those questions from the result SELECT q.QuestionID, q.QuestionText FROM Questions q LEFT JOIN ( SELECT a.QuestionID FROM Answers a INNER JOIN EmployeeAnswerMap AS eam ON a.AnswersID = eam.AnswerID UNION SELECT a.QuestionID FROM Answers a INNER JOIN CustomerAnswerMap AS cam ON a.AnswersID = cam.AnswerID) AS ans ON q.QuestionID = ans.QuestionID WHERE (ans.QuestionID IS NULL) You could simplify things a lot if you had just one answermap table. |
|
Then either 1. have two columns for customerid and employeeid, and join on one or the other as required to get the customer/employee names or 2. have one table for both customers and employees, and add a field to tell which is which Or have a 2nd UNION where this query is added to the original union. |
|
-- Roger |
#5
| |||
| |||
|
|
"Roger" <mothland (AT) btopenworld (DOT) com> wrote in message news:v_SdnZAQYr2PAljRnZ2dnUVZ8sCdnZ2d (AT) bt (DOT) com... "tshad" <tfs (AT) dslextreme (DOT) com> wrote in message news:q5gxo.6227$yw.2591 (AT) newsfe18 (DOT) iad... That was what I figured but was curious if there was a better way, performance wise. I have 2 queries I need to do. One like the one I just did and one that also shows the rows that didn't have any answers. If I was doing it without the UNION, I would just look at rows with nulls in both sets of columns. But what is the best way using a UNION? Do you mean questions without any answers, or answers that have not been chosen? By the former, I assume you mean questions where none of the answers have been chosen. |
|
So in my example, I would get an empty set. But if I add the following records: INSERT Questions(QuestionText) VALUES ('Which is your favorite sport?'); INSERT Answers (QuestionID,AnswerText) VALUES (1,'Football'); INSERT Answers (QuestionID,AnswerText) VALUES (1,'Baseball'); INSERT Answers (QuestionID,AnswerText) VALUES (1,'Basketball'); INSERT Answers (QuestionID,AnswerText) VALUES (1,'Other'); Then I get "Which is your favorite sport?", which would be correct. Answers that no one has chosen (in my case that would have been Blue for the original question and the all 4 answers for the new question since no one had answered it) would also be useful. |
|
You could simplify things a lot if you had just one answermap table. I agree - but the second table was added by someone else. |
#6
| |||
| |||
|
|
That does work as well. Not sure why. I will have to look closer at it. Especially since I am not quite sure how the cross join works here. |
|
1) The way I written the LEFT JOINs is the "right" way of doing an inner join from a left-joined table. Not sure what you mean here. |
![]() |
| Thread Tools | |
| Display Modes | |
| |