Query only pulling in output from one column instead of all columns

心已入冬 提交于 2019-12-11 15:22:31

问题


I actually first tried doing this in excel with a bunch of nested vlookups and encountered the same error so I tried doing it in access thinking I would resolve the problem, but I get the exact same issue.

All I am trying to do is join two tables by an inner join with 1 column in the one table and one of many columns in the other table (and I want the output to be (the one that is causing my issue) to be one particular column. Here is a small sample of the data and what I tried to get followed by my query.

Table 1:

Search    unique

gloves    5000
beaker    3000
tea       1000
timer     2000

Table 2:

Name   Field1   Field 2    Field3 ....
gloves hello    goodbye
Time   timer    clock
hi     tea

when I do an inner join I get:

gloves 5000

instead of getting:

gloves 5000
tea    1000
timer  2000

Hence it is only joining it on the 1st column, no idea why? Here is the Query I wrote in Access:

SELECT DISTINCT Product.Category, Analytics.Unique
FROM Product INNER JOIN Analytics ON IIF(Analytics.Search = Product.Category 
Is Not Null,Analytics.Search = Product.Category, IIF(Analytics.Search = 
Product.Field4 Is Not Null, Analytics.Search = Product.Field4, 
IIF(Analytics.Search = Product.Field5 Is Not Null, Analytics.Search = 
Product.Field5, IIF(Analytics.Search = Product.Field6 Is Not Null, 
Analytics.Search = Product.Field6, IIF(Analytics.Search = Product.Field7 Is 
Not Null, Analytics.Search = Product.Field7, IIF(Analytics.Search = 
Product.Field8 Is Not Null, Analytics.Search = Product.Field8,  
IIF(Analytics.Search = Product.Field9 Is Not Null, Analytics.Search = 
Product.Field9, IIF(Analytics.Search = Product.Field10 Is Not Null, 
Analytics.Search = Product.Field10))))))));

回答1:


I believe that your expressions are not testing what you think they are testing. The partial expression

Analytics.Search = Product.Category Is Not Null

is equivalent to

(Analytics.Search = Product.Category) Is Not Null

In other words, testing whether a boolean value is not null. And boolean values never are null.

You probably want something more like this:

... ON Analytics.Search = IIF(Product.Category Is Not Null, Product.Category, 
                          IIF(Product.Field4 Is Not Null, Product.Field4, ...



回答2:


select table2.search, table1.unique
from table1 t 
inner join table2 t2 on t2.name = t.search
inner join table2 t3 on t3.field1 = t.search
inner join table2 t4 on t4.field2 = t.search
inner join table2 t5 on t5.field3 = t.search


来源:https://stackoverflow.com/questions/45491567/query-only-pulling-in-output-from-one-column-instead-of-all-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!