Solved

SQL search three table columns for three variable

Posted on 2013-06-12
20
393 Views
Last Modified: 2013-06-20
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.
0
Comment
Question by:ASIADMIN
  • 5
  • 5
  • 5
  • +2
20 Comments
 
LVL 5

Expert Comment

by:DOSLover
ID: 39242382
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
 

Author Comment

by:ASIADMIN
ID: 39242529
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
 
LVL 8

Expert Comment

by:didnthaveaname
ID: 39242542
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
 

Author Comment

by:ASIADMIN
ID: 39242552
it is possible for the user to enter x, x, y
0
 
LVL 5

Expert Comment

by:DOSLover
ID: 39242597
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
 
LVL 4

Expert Comment

by:BAKADY
ID: 39242615
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
 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 250 total points
ID: 39243354
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
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39244312
Thank you! It was an interesting question too. Cheers, Paul.
0
 
LVL 4

Expert Comment

by:BAKADY
ID: 39244499
hey Paul, if a make changes in sql Fiddle, do you see them?
Be glad you got the points...
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 48

Expert Comment

by:PortletPaul
ID: 39244547
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
 
LVL 4

Expert Comment

by:BAKADY
ID: 39244557
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
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39244610
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
 
LVL 4

Expert Comment

by:BAKADY
ID: 39244613

('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
 

Author Comment

by:ASIADMIN
ID: 39244807
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
 

Author Comment

by:ASIADMIN
ID: 39244850
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
 
LVL 8

Expert Comment

by:didnthaveaname
ID: 39244862
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
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39246388
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
 
LVL 4

Accepted Solution

by:
BAKADY earned 250 total points
ID: 39246775
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
 

Author Comment

by:ASIADMIN
ID: 39248005
Thanks again for all your help guys
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

29 Experts available now in Live!

Get 1:1 Help Now