Link to home
Start Free TrialLog in
Avatar of ASIADMIN
ASIADMINFlag for Afghanistan

asked on

SQL search three table columns for three variable

I have a situation where I have three table columns, we will call them Part1, Part2, and Part3.
A user will input three part numbers into a program and I will need to search for that combination of part numbers but the part numbers could be in any order and exist in any one of those columns listed above.  all three of the parts entered by the user must exist in the three DB columns to be true and they can not be repeated unless the user enters the same part number twice.  Is there any way to perform a search for this in SQL?

I understand this may be kind of vague so please ask questions if you have any.
Avatar of DOSLover
DOSLover
Flag of United States of America image

Esssentially you are saying, user enters three partnumbers which should exist but in any order, right? In such case, the following where condition may help, assuming No NULLs:

Where ...
     AND ( (Part1 in (UserEnteredValue1,UserEnteredValue2,UserEnteredValue3)
               AND (Part2 in (UserEnteredValue1,UserEnteredValue2,UserEnteredValue3)
               AND (Part3 in (UserEnteredValue1,UserEnteredValue2,UserEnteredValue3)
             )

Open in new window

Avatar of ASIADMIN

ASKER

this would normally work but the values can not be repeated.  for example:

table1
value1   value2   value3
    x             y            z
    x             x            x
    y             y            z
    z             y            x
    y             x            x


users enters  x, y, z respectively

I only want it to return

value1   value2   value3
    x             y            z
    z             y            x

in the code you suggested it would return all the rows.

if I'm looking at your code incorrectly please let me know.
Avatar of didnthaveaname
didnthaveaname

try adding:

Where 
     ...
     AND ( (Part1 in (UserEnteredValue1,UserEnteredValue2,UserEnteredValue3)
               AND (Part2 in (UserEnteredValue1,UserEnteredValue2,UserEnteredValue3)
               AND (Part3 in (UserEnteredValue1,UserEnteredValue2,UserEnteredValue3)
             )
     and part1 <> part2
     and part1 <> part3
     and part2 <> part3

Open in new window

it is possible for the user to enter x, x, y
I see what you are saying. Actually it should be the other way around:
Where ...
     AND 
     ( UserEnteredValue1 in (Part1,Part2, Part3)
     AND UserEnteredValue2 in (Part1,Part2, Part3)
     AND UserEnteredValue3 in (Part1,Part2, Part3)
     ) 

Open in new window

CODE-Madness, try this one...!!! please send feedback...
with urTable (A, B, C) as (
  -- YOUR TABLE
  SELECT 'A', 'A', 'B'
  UNION ALL
  SELECT 'B', 'A', 'B'
  UNION ALL
  SELECT 'A', 'C', 'B'
  UNION ALL
  SELECT 'C', 'A', 'B'
  UNION ALL
  SELECT 'A', 'B', 'B'
  UNION ALL
  SELECT 'B', 'B', 'B'
  UNION ALL
  SELECT 'B', 'C', 'B'
  UNION ALL
  SELECT 'B', 'B', 'B'
  UNION ALL
  SELECT 'B', 'B', 'B'
), SearchParts (A, B, C) as (
  -- USER ENTER
  SELECT 'B', 'B', 'C'
)
select T.A, T.B, T.C,
-- this just to show which values are compared -BEGIN
  checksum(T.A, T.B, T.C) as T1,
  checksum(P.A, P.B, P.C) as P1,
  checksum(P.A, P.C, P.B) as P2,
  checksum(P.B, P.A, P.C) as P3,
  checksum(P.B, P.C, P.A) as P4,
  checksum(P.C, P.A, P.B) as P5,
  checksum(P.C, P.B, P.A) as P6
-- this just to show which values are compared -END
from urTable T inner join SearchParts P
on (
  checksum(T.A, T.B, T.C) In (
    checksum(P.A, P.B, P.C),
    checksum(P.A, P.C, P.B),
    checksum(P.B, P.A, P.C),
    checksum(P.B, P.C, P.A),
    checksum(P.C, P.A, P.B),
    checksum(P.C, P.B, P.A)
   )
)

Open in new window

Regards
SOLUTION
Avatar of PortletPaul
PortletPaul
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you! It was an interesting question too. Cheers, Paul.
hey Paul, if a make changes in sql Fiddle, do you see them?
Be glad you got the points...
sure do,
I actually just had a look at yours (I tweaked it as more comparable)
http://sqlfiddle.com/#!3/ffb08/8

It's an interesting approach you took
https://www.experts-exchange.com/questions/28155538/SQL-search-three-table-columns-for-three-variable.html?anchorAnswerId=39242552#a39242552
by: ASIADMINPosted on 2013-06-12 at 22:22:02ID: 39242552

it is possible for the user to enter x, x, y

look closer the input data and the output data

http://sqlfiddle.com/#!3/cae45/2
I see, you are right, the impact of sequence is better addressed by your method

@ASIADMIN
by extending the possible data variations BAKADY had identified flaws in my approach
You may want to look at http://sqlfiddle.com/#!3/cae45/2

He has also simplified his code, so please do re-consider.

@BAKADY
whilst sqlfiddle is cool - we prefer that the code gets included here - I'd suggest you post that latest iteration. (btw: by making the code simpler there is a better chance of it being understood - I did briefly look at yours today - but got confused by all those single chars ;)

Nice job.

('y', 'x', 'x') <= twice x, once y
('x', 'y', 'x'), <= twice x, once y
('y', 'y', 'x'), <= once x, twice y
('x', 'y', 'y') <= once x, twice y
set @UserEnteredValue1 = 'x'
set @UserEnteredValue2 = 'x'
set @UserEnteredValue3 = 'y'

Open in new window

it mean user is looking for all matches with twice x and once y...
or am i wrong???
no you are right that is exactly what im looking for

let me take some time and rethink this and get back to you.  

thanks for all your help guys
Bakady
your solution seems to be correct, and I do see the flaw in pauls code now.

I guess there isn't anyway for me to split the points between the two of you is there
hit request attention up at the top and ask a mod to re-open the question so you can close with the point split.  i've thoroughly enjoyed seeing these solutions =)
BEFORE the solution can be re-awarded, the new solution code should be posted
@BAKADY
whilst sqlfiddle is cool - we prefer that the code gets included here - I'd suggest you post that latest iteration.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks again for all your help guys