Solved

How to exclude customers from showing if they have a specific package

Posted on 2011-02-10
11
246 Views
Last Modified: 2012-05-11
Ok I will try and be as clear as possible.  I have two tables one is pp and one is cp.  I have one entry field is called classnum in the PP table.  The CP table is my customer number.  For some reason the code I have now is still pulling the wrong results and I am sure I have something wrong.  

The results I am trying to obtain:  Show me all customers (custnum) that does not have any packages that have a class of 17.

Each package in this PP table has different classnum.  

The results I am getting:  It is showing the unique customer numbers but it is showing me ALL unique customer numbers.  Currently it is pulling 1558 rows which is my customer count.  If the query is correct it should only pull two.  As I know for a fact there are two unique customers with a package class of 17.  

I have attached the query that I have now.
SELECT DISTINCT TOP (100) PERCENT cp.custnum
FROM         KC_Freeside.dbo.cust_pkg AS cp INNER JOIN
                      KC_Freeside.dbo.part_pkg AS pp ON pp.pkgpart = cp.pkgpart
WHERE     (cp.cancel IS NULL) AND (pp.classnum <> 17) AND (pp.freq <> '0')
ORDER BY cp.custnum

Open in new window

0
Comment
Question by:binaryman101
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 22

Expert Comment

by:Thomasian
ID: 34867984

SELECT cp.custnum
FROM KC_Freeside.dbo.cust_pkg AS cp LEFT JOIN
     KC_Freeside.dbo.part_pkg AS pp ON pp.pkgpart = cp.pkgpart AND pp.classnum = 17
WHERE cp.cancel IS NULL AND pp.pkgpart IS NULL
ORDER BY cp.custnum

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 34868048
Just a small addition to the previous solution (no points please):
SELECT  DISTINCT cp.custnum
FROM    KC_Freeside.dbo.cust_pkg AS cp
        LEFT JOIN KC_Freeside.dbo.part_pkg AS pp ON pp.pkgpart = cp.pkgpart AND pp.classnum = 17 AND pp.freq <> '0'
WHERE   cp.cancel IS NULL
        AND pp.pkgpart IS NULL
ORDER BY cp.custnum

Also, notice how the TOP(100) PERCENT should not be used as it makes no sense.  The SQL QO is smart enough to see that you have TOP(100) PERCENT and promptly ignore the ORDER BY clause, so your results will not not sorted.
0
 

Author Comment

by:binaryman101
ID: 34868319
I am currently get 1682 rows now.  And the two that shouldn't be in the results are showing up in the results.  I also try and remove the TOP 100 PERCENT and it comes right back.  I am using SQL server management studio.  I am really confused why I can't get this to work.  
0
 
LVL 22

Expert Comment

by:Thomasian
ID: 34868360
And the two that shouldn't be in the results are showing up in the results.

Can you post all the records from tables cp and pp of the two results you mentioned?
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 34868424
ok from your original statement....


this query will return
those custnums which don't have any classnum 17 's


select distinct custnum
  from KC_Freeside.dbo.cust_pkg AS cp
 where not exists (select  
                     from KC_Freeside.dbo.part_pkg AS pp
                    where classnum=17
                      and cp.custnum=pp.custnum)
 order by 1


would you now like to explain what the other two conditions you where trying to apply mean in relation to the problem?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:binaryman101
ID: 34874478
I have attached the data in tables to this spreadsheet.  Each tab is the data.

Custnum 3654 and 4306 should not show up because they are the only ones with classnum of 17.  Let me know if you need something else.  Thank you!
CP-PP.xls
0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 500 total points
ID: 34875393
i think part of your problem is that the cancel column appears to contain the literal 'NULL'
and not an actual null


also there is an entry for 3654 with cancel='NULL' and the freq is 1


freq comes over as a float in the import...
0
 

Author Comment

by:binaryman101
ID: 34875604
How do I compensate for that?
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 34875830
what is the intention ?...

if all literal 'Nulls' should be database nulls then that is a trivial correction

 vis   update table
           set [cancel] = nullif([cancel],'Null')
        where [cancel] ='Null'


however as i indicated the other table for 3654 at least does have a valid classnum=17 entry...

didn't check the other one...

i'm not sure what datatype you intend for freq since you show a character test <> '0' but at least from
an import point of view that came over as a float...


testing on a smaller sample set  is probably advisable initially at least.

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 34876384
>>I also try and remove the TOP 100 PERCENT and it comes right back.  I am using SQL server management studio.  <<
That is because you are using the Design mode.  Don't do that or you will be frustrated to say the least.  Use the Query window instead and type in the SQL Statement there.
0
 

Author Closing Comment

by:binaryman101
ID: 34998485
Nulls were a problem.  I found a work around.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

760 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

22 Experts available now in Live!

Get 1:1 Help Now