|
I've had a request for a query that combines information from two tables. For the result I need not only the records which link, but also the records from each table which do not have a corresponding link in the other table. (The user wants this to use with PowerPivot.) I've come up with a query which gives me the results EXCEPT for a couple of entries which have a NULL where I'm expecting a name. As you can see, I've used a couple of CTEs to get the data I need from each table. I used the employee id in those, and then joined the personnel table at the last select in order to get the name. Can this be corrected to give me all the names for the ids? Is there another way to get this information? Or is it impossible, as I've been told? Here is the query:
(comments are locked)
|
|
Try ISNULL. eg: will return will return the I think I can make this work.
Mar 23 '11 at 01:08 PM
lostinaquery
If it's just the odd field that's missing, then it could be the way to go... if it's whole rows missing, then you'll be needing the correct join.
Mar 23 '11 at 01:24 PM
ThomasRushton ♦
(comments are locked)
|
|
Here's an example which re-worked your query using some sample data. I created temporary tables instead of using the table names in your CTE, but this should give you a pretty good idea of what's going on regardless. The data set I ginned up will show two distinct types of "missing record" pieces of information. The first is if you have a record in B but not in A and the information exists in P. In that case, the query will end up returning NULL for the P information (employee code and employee name). The easiest way to correct this is to convert your inner join on A to a left outer join on A. In the example above, this is the record where the budget hours is 30. The second type of "missing information" problem is when you have an Employee UNO in expression B but no record in table P (the personnel table). In that case, you have bad data and unless the employee information is stored somewhere else, you won't be able to display it. In the example above, this is the one where the budget hours is 60. There is no employee UNO of 5 in the personnel table, so we're out of luck trying to display that information. The first type of 'missing information' is exactly my situation. The difficulty with using the left outer join to P is that there are lots of records in P which aren't needed for this query.
Mar 23 '11 at 12:59 PM
lostinaquery
Ah, okay. Your situation's still salvagable, then. :-) I was afraid that you had some out-and-out bad data in set B. I have updated my query to include a where clause to see if the person is in data set A or B. Then, using the test data, you won't see Diane (who was not in either A or B) but you will see everyone else. I don't believe that performance will be miserable with this, especially if P has an index which includes empl_uno. Also, if there are no empl_uno values in A or B which don't have corresponding matches in P, you could even change the full outer join to be a left outer join. I left it a full outer join because of the possibility of the second type of missing information.
Mar 23 '11 at 01:03 PM
Kevin Feasel
Beautiful! I had to go with the other answer to get the user started on her project, but I came back to it and this is just what I needed. Thank you so much. That makes this my preferred answer now.
Mar 31 '11 at 12:43 PM
lostinaquery
(comments are locked)
|


I'm not sure I've made myself clear. Each CTE will return a column with an employee id. However, there may be employees in each result set that are not in both result sets. I need all of the results, and would like to have the name instead of the uno, as the uno doesn't relate to anything the user would recognize. I thought joining on the personnel table after I got the results would give me that. Instead, I get the employee name for all the results which are in A, whether or not they have any matching entries in B, but the results from B show NULL instead of the employee name.