0

A few days back I wrote a question here that got answered and helped me resolve what I thought at the time was my project:

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

This is what worked earlier and had no issues:

SELECT DISTINCT Product.Category, Analytics.Unique, Product.ID, 
Product.Family
FROM Analytics INNER JOIN Product ON Analytics.Search = IIF(Product.Category 
= Analytics.Search, Product.Category, IIF(Product.Field4 = Analytics.Search, 
Product.Field4,));

At the time, I only thought I only am required to look for exact matches, I have since found out that I need to join on partial matches as well where there is at least 1 full common word to both the strings that are being joined on. For example, if we are joining on "Beakers" vs "Huge Big Beakers" they should join because they have "Beakers" in common....

I have tried modifying my previous query from the other question to account for this based on some links I found here on stackoverflow, I keep getting Syntax Error and it does not really point to where the problem is. Here is my Query (it is only for 1 column, have not copy pasted it to all of them because it still does not work for some reason), I only am showing a few columns because they are repetitive anyways and to make it easier to read:

SELECT DISTINCT Product.Category, Analytics.Unique, Product.ID, 
Product.Family
FROM Analytics 
INNER JOIN Product ON Analytics.Search = IIF((SELECT DISTINCT 
Product.Category 
FROM Product, Analytics 
WHERE ' ' &  Product.Category & ' ' 
Like '* Product.Category *'), Product.Category, 
IIF(Product.Field4 = Analytics.Search, Product.Field4,));

something is wrong in this part that I tried to add because it worked before for the full match per the other question I had:

Product ON Analytics.Search = IIF((SELECT DISTINCT Product.Category FROM 
Product, Analytics WHERE ' ' &  Product.Category & ' ' Like '* 
Product.Category *'),
bernando_vialli
  • 947
  • 4
  • 12
  • 27
  • Since you did not post the full syntax of what you got to work earlier, when I compare earlier to this, I see there are more changes to your new query than just altering your selection criteria. I suggest you simplify the syntax by removing anything not necessary for a simple test, then adding slowly back. – Wayne G. Dunn Aug 07 '17 at 19:30
  • ok, I updated my queries that I can to make it clearer as to what I did originally – bernando_vialli Aug 07 '17 at 19:42
  • Now that you posted the before and current, please note you have a big difference between OLD: FROM Analytics INNER JOIN ... = IIF(Product.Category ... and NEW: FROM Analytics INNER JOIN ... = IIF((SELECT DISTINCT Product.Category – Wayne G. Dunn Aug 07 '17 at 20:27
  • OK, I thought that difference was trivial because it's just the same thing being repeated over and over... but I updated it again for them to be exactly the same now, sorry guess I should have done that the first time – bernando_vialli Aug 07 '17 at 20:31
  • Does that mean it now works, or do you still get an error? – Wayne G. Dunn Aug 07 '17 at 20:50
  • oh no, sorry I guess I should be more clear. No I keep getting a syntax error, I tried playing around with different small variations and keep on getting the same syntax error regardless of what I try. The syntax error does not exactly show where the problem is, so I am not sure what/how to fix it – bernando_vialli Aug 07 '17 at 20:57
  • It would help to have some examples using your values "Beakers" vs "Huge Big Beakers". Please read and answer ALL THE FOLLOWING: (1) In field Product.Category, what value is in there? (2) In field Analytics.Search,what value is in there? (3) Anything else you can clearly state that may help? – Wayne G. Dunn Aug 07 '17 at 22:57
  • OK, basically I am comparing search terms that people are using to search on a website vs how everything is hierarchically structured on the website. So for example, think of Amazon. Say you are searching for a book and type "Tom Sawyer" and in Amazon's system it appears as "The Adventures of Tom Sawyer" so in this case, I want that to be a join because it contains 2 common words (even though 1 would be sufficient). So for 1)Analytics.Search, its the "Tom Sawyer" that you are searching for. For Product Category, its the "The Adventures of Tom Sawyer" that's labeled in the system. – bernando_vialli Aug 08 '17 at 00:03
  • The reason there are many columns is because the product category has many synonyms so for my example lets say there are 2 other synonyms "The Adventures of Tom" and the "Adventures of Sawyer" so basically what I want to do is do a join for "Tom Sawyer" with any that match (if any) of "the Adventures of Tom Sawyer", "The Adventures of Tom" and "The Adventures of Sawyer" as long as 1 of the words matches, I want to spit out the output "The Adventures of Tom Sawyer". I am making up the actual words here, just trying to illustrate the concept – bernando_vialli Aug 08 '17 at 00:06

2 Answers2

1

EDIT #1: Added sample of SQL that pads the fields to be searched with a space so it will look for whole words.

Not sure what your volume of data is, but the SQL I have included below works somewhat like you want it to. One flaw in your approach is the fact that when you are trying to locate 'words within a string', you can't pad a space on the front or back unless you pad both the search terms AND the string to be searched (normally, there are no trailing spaces in a text field).

I only did Field4 thru Field7, so you need to adjust.

NOTE: The first part of the criteria in the WHERE clause looks for an exact match; the second criteria looks for the padded word; and the third criteria looks for matches in your 'Fieldxx'

Here is SQL that pads each of the fields you search:

SELECT DISTINCT ' ' & [Category] & ' ' AS MyCat, Analytics.Unique, Product.ID, Product.Family
FROM Product, Analytics
WHERE (((' ' & [Category] & ' ')=[Analytics]![Search] 
Or (' ' & [Category] & ' ') Like "* " & [Analytics]![Search] & " *")) 
OR (((' ' & [Field4] & ' ') Like "* " & [Analytics]![Search] & " *")) 
OR (((' ' & [Field5] & ' ') Like "* " & [Analytics]![Search] & " *")) 
OR (((' ' & [Field6] & ' ') Like "* " & [Analytics]![Search] & " *")) 
OR (((' ' & [Field7] & ' ') Like "* " & [Analytics]![Search] & " *"));

Or, a really simple example:

SELECT DISTINCT Product.Category, Analytics.Unique, Product.ID, Product.Family
FROM Product, Analytics
WHERE (((Product.Category)=[Analytics]![Search])) 
    OR (((Product.Category) Like "* " & [Analytics]![Search] & " *")) 
    OR (((Product.Field4)=[Analytics]![Search])) 
    OR (((Product.Field5)=[Analytics]![Search])) 
    OR (((Product.Field6)=[Analytics]![Search])) 
    OR (((Product.Field7)=[Analytics]![Search]));
Wayne G. Dunn
  • 4,282
  • 1
  • 12
  • 24
  • I just tried running this as follows and get exactly the same error message about the syntax error: SELECT DISTINCT Product.Category, Analytics.Unique, Product.ID, Product.Family FROM Analytics INNER JOIN Product ON Analytics.Search = IIF(SELECT DISTINCT Product.Category, Analytics.Unique, Product.ID, Product.Family FROM Product, Analytics WHERE (((Product.Category)=[Analytics]![Search])) OR (((Product.Category) Like "* " & [Analytics]![Search] & " *")) OR (((Product.Field4)=[Analytics]![Search])) – bernando_vialli Aug 08 '17 at 12:07
  • OR (((Product.Field5)=[Analytics]![Search])) OR (((Product.Field6)=[Analytics]![Search])) OR (((Product.Field7)=[Analytics]![Search])); So I am not sure what the problem is. Also, I guess I don't fully understand what you wrote. Isn't if there is a Like match should be the only type of match because it also incorporates the full match? – bernando_vialli Aug 08 '17 at 12:08
  • 1
    First, try the SQL I provided. If it works, then there is hope. Second, look at my SQL using the query designer - not just looking at the SQL. That will explain it a lot better. Regarding you saying there should only be a 'Like' match, yes, you can change my SQL to use the example I provided that does use the LIKE. However, as mentioned earlier, you will need to pad with spaces on the front and back of 'Product.Category' and all of the 'Fieldxxx' (ie. SELECT DISTINCT ' ' & [Category] & ' ' AS XX, Analytics.Unique, Product.ID, Product.Family...) – Wayne G. Dunn Aug 08 '17 at 13:06
  • 1
    I added another example in my answer that pads the fields with spaces and used 'LIKE' – Wayne G. Dunn Aug 08 '17 at 13:19
  • Thank you very much Wayne, I really appreciate it!!! Your query seems to do exactly what I want. But I am still not sure I fully understand it. I tried looking at the design view, it's not really telling me much. My main question is how come this line: Or (' ' & [Category] & ' ') Like "* " & [Analytics]![Search] & " *")) is the same as the previous line: (((' ' & [Category] & ' ')=[Analytics]![Search]. So does that mean that when you are essentially comparing two tables you can use a LIKE and avoid using an "=" sign? – bernando_vialli Aug 08 '17 at 15:29
  • 1
    The two lines you mention ARE different - note that the second line uses 'LIKE' and the first does NOT. That means the first is looking for an EXACT match - which you can remove if that is never what you want to do. The design view should show you visually what all of the criteria is about. Note that most criteria are on different lines? That means you are using an "OR" (i.e. If A = 1 or B = 2 or ...). Re 'LIKE' vs '=', they are not the same if you use 'If 'Wayne' = 'W' versus 'If 'Wayne' LIKE '*y*'...' – Wayne G. Dunn Aug 08 '17 at 15:58
  • but in this context the LIKE is equivalent to "=" right? Also, what is the reason we need 3 parenthesis in this line: OR (((' ' & [Field4] & ' ')? It's pretty cool that you can use a WHERE instead of a JOIN to do this – bernando_vialli Aug 08 '17 at 16:29
  • You need to qualify 'LIKE' and '='. If you are asking if "' ' & [Category] & ' ')=[Analytics]![Search]" is the same as "' ' & [Category] & ' ') Like "* " & [Analytics]![Search] & " *"", then the answer is not at all the same. The asterisk is used as a wildcard - and is NOT present in the first statement. – Wayne G. Dunn Aug 08 '17 at 19:00
0

There are issue with your first IIF statement. IIF statement has 3 parts.

IIf ( experession , truepart , falsepart )

Expression is something you want to evaluate. it usually contains = ,> or < signs

lets take your first IIF statement:

iif((SELECT DISTINCT product.category FROM product, analytics WHERE ' ' & product.category & ' ' LIKE '* Product.Category *') , product.category,IIF(.....))

So your expression part in that contains this query:

( 
SELECT DISTINCT product.category 
FROM            product, analytics 
WHERE           ' ' & product.category & ' ' LIKE '* Product.Category *'
)

But you are not evaluating a result of that query, What are you trying to do with the result of that query? There are No =, > or < to evaluate result

you are also missing the false statement in last if statement. For More Explanation, Please visit: https://support.office.com/en-us/article/IIf-Function-32436ecf-c629-48a3-9900-647539c764e3

Kashif Qureshi
  • 1,460
  • 2
  • 13
  • 20
  • I think I see what you mean so I just made an update with a "=" sign but get the exact same syntax error message, Syntax Error in query Expression Analytics.Search =IIF(.... :: SELECT DISTINCT Product.Category, Analytics.Unique, Product.ID, Product.Family FROM Analytics INNER JOIN Product ON Analytics.Search = IIF((SELECT DISTINCT Product.Category FROM Product, Analytics WHERE ' ' & Product.Category & ' ' Like '* Product.Category *') = Analytics.Search, Product.Category, IIF(Product.Field4 = Analytics.Search, Product.Field4)); – bernando_vialli Aug 07 '17 at 19:52