Solved

T-SQL Simple Not Equal Question

Posted on 2008-10-21
4
357 Views
Last Modified: 2010-05-18
Problem Column: dc.itemclass tinyint (not null)
Problem: I want to display a result set that excludes itemclasses assigned the numbers 1, 9, and 20. I am not able to exclude them with my current code and need some help with the correct syntax.

Ways I have unsuccessfully attempted:
example 1
and di.itemclass not like '1'
and di.itemclass not like '9'
and di.itemclass not like '20'
example 2
and not di.itemclass = (1)
and not di.itemclass = (9)
and not di.itemclass = (20)
example 3
and di.itemclass <> (1)
and di.itemclass <> (9)
and di.itemclass <> (20)



Current Code Below:
select
dc.customernk,
dc.customername,      
di.itemclass,
di.itemclassdesc,
      di.ItemSubClass,
di.itemsubclassdesc,
      di.itemnk,
      di.itemdesc,
      sum(f.quantitysold)as 'Net Qty Sold',
Sum((ExtendedPrice - ExtendedPromotions) + (MarketingAllowances + Deposits +
SalesTax + FreightServiceFees + TelxonFees + Othercharges))as  'Net Sales',
case when argroup in (' ') then ('35145-8') else argroup end
FROM
      DimItem as di
JOIN      FactCustSales as f
ON       di.ItemWK = f.ItemWK
JOIN      DimCustomer as dc
ON      f.CustomerWK = dc.CustomerWK
JOIN      DimCalendar as dcal
ON      f.SalesDateWK = dcal.datewk
WHERE       dc.argroup in ('H04', 'H05')
or customernk = ('351458')
AND      dcal.DateNK >= 'Jan 1 2008'
AND di.itemclass not in (1,9,20)
AND      di.Chargetype = 'ITEM'
and f.quantitysold > (0)
and f.creditinvoiceflag <> 'RB'
GROUP BY

dc.customernk,
dc.customername,
argroup,
      di.ItemClass,
      di.ItemClassDesc,
      di.ItemSubClass,
      di.ItemSubClassDesc,
      di.Itemnk,
      di.ItemDesc
ORDER BY
dc.customernk,
dc.customername,      
argroup,
      di.ItemClass asc,
      di.ItemSubClass asc,
      sum(f.quantitysold) desc
0
Comment
Question by:SasDev
  • 2
4 Comments
 
LVL 32

Accepted Solution

by:
bhess1 earned 500 total points
ID: 22769521
Your WHERE clause is definitely a problem.  Let's examine the logic.  

The basic problem, I believe, is that you have effectively two WHERE clauses - one before the OR and one after.  To solve this, you will need to include the OR'd items within a set of parentheses, like this:

WHERE
    (
        dc.argroup in ('H04', 'H05')
        or customernk = ('351458')
    )
    AND      dcal.DateNK >= 'Jan 1 2008'
    AND di.itemclass not in (1,9,20)
    AND      di.Chargetype = 'ITEM'
    and f.quantitysold > (0)
    and f.creditinvoiceflag <> 'RB'
0
 
LVL 1

Author Closing Comment

by:SasDev
ID: 31508417
How right you are! Thank you very much for helping me examine my logic.
Take Care,

Traci V.
0
 
LVL 3

Expert Comment

by:ScottyNewSuits
ID: 22769591
and di.itemclass NOT IN (1,9,20) should work...

you can try
and (di.itemclass <> 1 AND di.itemclass <> 9 AND di.itemclass <> 20).

Please note that OR syntax ignore indexes.
0
 
LVL 1

Author Comment

by:SasDev
ID: 22770518
Thank you ScottyNewSuits. I already awarded the points before your suggestion came in. Thanks anyhow!

Traci
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

Title # Comments Views Activity
Stored procedure query with if 27 36
MS SQL order by with "over" statement and row_number() 11 38
Two tables - Sum of values - What is the difference 31 48
Restrict result set 1 33
Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
A short film showing how OnPage and Connectwise integration works.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

930 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

16 Experts available now in Live!

Get 1:1 Help Now