Avatar of Ben
Ben
 asked on

remove invalid data

I have records for 2 fields (orderno and planningjob) that are held in the same table (binding).

I have attached a spreadsheet that is the result of:

select orderno, planningjob from binding
order by orderno

As you can see, some orderno are duplicated and some have a planningjob record associated.
Some orderno have a planningjob associated for one record but then not for another. (See Orderno 17. There are 5 records for it but only 2 have a planningjob associated).

I need to be able to only show records that don't have any planningjob associated at all
Binding.xls
Microsoft SQL ServerDB Reporting ToolsSQL

Avatar of undefined
Last Comment
Ben

8/22/2022 - Mon
Jim Horn

>I need to be able to only show records that don't have any planningjob associated at all

SELECT orderno, planningjob
FROM binding
WHERE planningjob IS NULL
ORDER BY orerno
Ben

ASKER
Hi,

I wish it was that simple. There are some orderno that are duplicated 4 or 5 times. 2 of those records could have a null value and the others would have a proper value. So using your code returns orderno that do in fact have a record associated, if that makes sense?

Ben
mbizup

Ben,

You need to get a count of records that contain non- null planningJobs.   If that count is zero for any given OrderNo, then all of the planningJobs for that order number are Null (meaning that ordernumber should be selected).

- Create a new query in design view and open the binding table.
- Select OrderNo
- Add the following expressio as a new column:
   JobNull: IIf(IsNull([PlanningJob]),0,1)
   This will select 0 when PlanningJob is Null, and 1 when it is not null.
   You need to sum this column and group by OrderNo to get a "count of non-nulls per order".
- Click the Summation sign on the toolbar of the query builder.
- Select "SUM" for the Total box for the JobNull column
- Select "Group By" for the total box in the OrderNo column
- Add the following criteria for JobNull:
    = 0
  This will select all orders that only have null planning jobs.

The resulting SQL will look something like this:

SELECT Orderno, Sum(IIf(IsNull([PlanningJob]),0,1)) AS JobNull
FROM binding
GROUP BY Orderno
HAVING Sum(IIf(IsNull([PlanningJob]),0,1))=0;
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Ben

ASKER
Hi,

I am actually using Query Anlayzer in SQL 2000. Sorry, perhaps I loaded this in the wrong area.

ASKER CERTIFIED SOLUTION
appari

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Ben

ASKER
That still brings through jobs that have planningjob associated.

There can be multiple records per orderno but only some have planningjob associated. It's a pickle. I only want to see the orderno that have no planningjob associated at all