Link to home
Start Free TrialLog in
Avatar of Mike Orther
Mike OrtherFlag for United States of America

asked on

Need help with a Where clause in SQL Server query

I have a SQL Query where I need to limit the results set.  I know the WHERE statement is incorrect, but gives an idea what I am looking for.  I tried doing this in a CASE statement but I just could not get it.  I am hoping someone can help me with this.

SELECT        TOP (100) PERCENT dbo.WO010032.ITEMNMBR AS [Make Item], dbo.PK010033.ITEMNMBR AS [Item No], dbo.PK010033.QTY_ALLOWED_I AS PickQty, dbo.IV00300.BIN
FROM            dbo.WO010032 RIGHT OUTER JOIN
                         dbo.PK010033 ON dbo.WO010032.MANUFACTUREORDER_I = dbo.PK010033.MANUFACTUREORDER_I LEFT OUTER JOIN
                         dbo.IV00101 ON dbo.PK010033.ITEMNMBR = dbo.IV00101.ITEMNMBR LEFT OUTER JOIN
                         dbo.IV00300 ON dbo.PK010033.ITEMNMBR = dbo.IV00300.ITEMNMBR AND dbo.PK010033.LOCNCODE = dbo.IV00300.LOCNCODE

WHERE      IF dbo.WO010032.ITEMNMBR LIKE '60%' THEN dbo.IV00300.BIN NOT LIKE 'MFG%'
                   ELSE dbo.IV00300.BIN NOT LIKE 'BUF%'

Open in new window

SOLUTION
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

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
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
Avatar of Mike Orther

ASKER

I actually do not want to show any Bin that is LIKE 'MFG%' when the dbo.WO010032.ITEMNMBR LIKE '60%'  but this part of the query will include Bins LIKE 'BUF%'

But, If dbo.WO010032.ITEMNMBR NOT LIKE '60%' then exclude Bins "NOT LIKE 'BUF%'," but include Bins "LIKE 'MFG%'"
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
You can see when I run this query for Item Number 10.121b, The results show Bins that begin with MFG for a 60 series part.
SELECT        TOP (100) PERCENT dbo.WO010032.ITEMNMBR AS [Make Item], dbo.PK010033.ITEMNMBR AS [Item No], dbo.PK010033.QTY_ALLOWED_I AS PickQty, dbo.IV00300.BIN
FROM            dbo.WO010032 RIGHT OUTER JOIN
                         dbo.PK010033 ON dbo.WO010032.MANUFACTUREORDER_I = dbo.PK010033.MANUFACTUREORDER_I LEFT OUTER JOIN
                         dbo.IV00101 ON dbo.PK010033.ITEMNMBR = dbo.IV00101.ITEMNMBR LEFT OUTER JOIN
                         dbo.IV00300 ON dbo.PK010033.ITEMNMBR = dbo.IV00300.ITEMNMBR AND dbo.PK010033.LOCNCODE = dbo.IV00300.LOCNCODE
WHERE        (dbo.WO010032.ITEMNMBR LIKE '60%') AND (dbo.IV00300.BIN NOT LIKE 'MFG%') AND (dbo.PK010033.ITEMNMBR = '10.121b') OR
                         (dbo.IV00300.BIN NOT LIKE 'BUF%') AND (dbo.PK010033.ITEMNMBR = '10.121b')

Open in new window

User generated image
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
I think what you added above might have worked for me.
WHERE (dbo.WO010032.ITEMNMBR LIKE '60%' AND dbo.IV00300.BIN NOT LIKE 'MFG%') OR
    (dbo.WO010032.ITEMNMBR NOT LIKE '60%' AND dbo.IV00300.BIN NOT LIKE 'BUF%')

I will do a bit more testing and report back.
I was trying to make this harder than it actually was.  Thanks for this lesson.
You actually had it right with your original IF: I just translated that IF into the corresponding WHERE conditions.