Solved

SQL search three table columns for three variable

Posted on 2013-06-12
20
391 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
it is possible for the user to enter x, x, y
0
 
LVL 5

Expert Comment

by:DOSLover
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you! It was an interesting question too. Cheers, Paul.
0
 
LVL 4

Expert Comment

by:BAKADY
Comment Utility
hey Paul, if a make changes in sql Fiddle, do you see them?
Be glad you got the points...
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility

('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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks again for all your help guys
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Dynamics crm 2011 8 43
SQL Backup skipping a few tables 7 25
SQL Connection (Error 18456) 14 29
Sort by Month and Year - SQL 3 21
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

762 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

11 Experts available now in Live!

Get 1:1 Help Now