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.
ASIADMINAsked:
Who is Participating?
 
BAKADYCommented:
OK, Here botj propsals

http://sqlfiddle.com/#!3/cae45/2

this one is mine
-- Test 2
declare @UserEnteredValue1 as varchar(1)
declare @UserEnteredValue2 as varchar(1)
declare @UserEnteredValue3 as varchar(1)

-- USER ENTER
set @UserEnteredValue1 = 'x'
set @UserEnteredValue2 = 'x'
set @UserEnteredValue3 = 'y'

select *
from Table1
Where (
  checksum(Part1,Part2, Part3) In (
    checksum(@UserEnteredValue1, @UserEnteredValue2, @UserEnteredValue3),
    checksum(@UserEnteredValue1, @UserEnteredValue3, @UserEnteredValue2),
    checksum(@UserEnteredValue2, @UserEnteredValue1, @UserEnteredValue3),
    checksum(@UserEnteredValue2, @UserEnteredValue3, @UserEnteredValue1),
    checksum(@UserEnteredValue3, @UserEnteredValue1, @UserEnteredValue2),
    checksum(@UserEnteredValue3, @UserEnteredValue2, @UserEnteredValue1)
   )
)
;

Open in new window


regards
0
 
DOSLoverCommented:
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

0
 
ASIADMINAuthor Commented:
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.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
didnthaveanameCommented:
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

0
 
ASIADMINAuthor Commented:
it is possible for the user to enter x, x, y
0
 
DOSLoverCommented:
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

0
 
BAKADYCommented:
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
0
 
PortletPaulfreelancerCommented:
I think you need to evaluate in both directions (variable IN fields) and (fields IN variables)
user1,2,3 = x,y,z
PART1 PART2  PART3
x      y      z
z      y      x

user1,2,3 = x,x,y
PART1 PART2  PART3
y      x      x

Open in new window

see this working at http://sqlfiddle.com/#!3/ffb08/1 
the code:
declare @UserEnteredValue1 as varchar(1)
declare @UserEnteredValue2 as varchar(1)
declare @UserEnteredValue3 as varchar(1)

set @UserEnteredValue1 = 'x'
set @UserEnteredValue2 = 'y'
set @UserEnteredValue3 = 'z'

select
*
from table1
Where  
     (
           @UserEnteredValue1 in (Part1,Part2, Part3)
       AND @UserEnteredValue2 in (Part1,Part2, Part3)
       AND @UserEnteredValue3 in (Part1,Part2, Part3)
     ) 
and (
          part1 in (@UserEnteredValue1, @UserEnteredValue2, @UserEnteredValue3)
      and part2 in (@UserEnteredValue1, @UserEnteredValue2, @UserEnteredValue3)
      and part3 in (@UserEnteredValue1, @UserEnteredValue2, @UserEnteredValue3)
    )

Open in new window

0
 
PortletPaulfreelancerCommented:
Thank you! It was an interesting question too. Cheers, Paul.
0
 
BAKADYCommented:
hey Paul, if a make changes in sql Fiddle, do you see them?
Be glad you got the points...
0
 
PortletPaulfreelancerCommented:
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
0
 
BAKADYCommented:
http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/Q_28155538.html#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
0
 
PortletPaulfreelancerCommented:
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.
0
 
BAKADYCommented:

('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???
0
 
ASIADMINAuthor Commented:
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
0
 
ASIADMINAuthor Commented:
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
0
 
didnthaveanameCommented:
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 =)
0
 
PortletPaulfreelancerCommented:
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.
0
 
ASIADMINAuthor Commented:
Thanks again for all your help guys
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.