1

Hi Wonder if someone can help me with this query. I have a data set within which I have two columns. Column A/Column B; lets call it Table UpdateData.

There are also two Access tables with standing data that are mapping tables for these codes. One table is pre-update codes (oldCodes) and the other is post-system update codes (NewCode). In my data set Column A has pre-update codes and column B has post-update codes. My idea was to combine these two Access tables new and old codes into one table in SQL Server (2005) like this To Create Table combinedCodes:

Oldcode    display 
=====     =======
RTYX45    No
GHYUN6    No
BYUER5    Yes

Newcode    display 
=====      =======
VUJNG6     Yes
LERWS8     No
XCRYU7     Yes

code      display 
=====     =======
RTYX45    No
GHYUN6    No
BYUER5    Yes
VUJNG6    Yes
LERWS8    No
XCRYU7    Yes

Note this is only an example of data and there is a lot more rows than is displayed.

So if I join updateData and Combined codes how do I set it so that one field (display) can show the match from either column A/B (or not). When I joined on only one column lets say Column A with left join to my updateData table from combinedCodes table I got the correct data in my combinedCodes display field and nulls for the ones that didn't match.

But then I introduced another join from Column B using left join to my UpdateData table from CombinedCodes and the combinedCodes display showed Old Codes where before nulls were, which I would want, but also showed old codes where before the new codes were used. I was getting a bit mixed up with what was actually happening so this might not be 100% right however when comparing each query result with individual join the query results with the joins to column A and Column B was different although same count of data when adding up individual count from separate queries.

select c.Fields
--,f.Display
,pt.Display
 from UpdateData c 
INNER JOIN PRE_CODES pt
ON c.columnA = pt.code -- only for pre-codes
--INNER JOIN POST_CODES f
--ON c.columnB = f.code

The join and field commented out are for the post-codes and the other is for pre-codes. If i include left outer join to show nulls for post-codes then introduce 2nd join and f.display plus left join for field it doesn't show the same results as if I run each individually and combine them.

this kind of join is going beyond my understanding of joins and I do not know exactly what I should do here so over to any of you who can help me with this

Thanks

Andrew

4

1 回答 1

1

如果我了解您要做什么,我可以看到几个选项。

一种是编写两个查询,一个针对每个代码表(每种情况下都有一个内部连接,因此您没有任何空值),然后使用UNION组合结果并为组合表生成数据。

另一种是编写一个查询,该查询使用左外连接连接到每个表,这将导致一个表的一列具有值或空值,而另一列具有值或空值其他表。然后将另一列添加到使用COALESCE将这两列组合成一个非空列的结果中。

于 2013-04-03T16:01:01.500 回答