Solved

Query to return missing values

Posted on 2008-10-28
3
196 Views
Last Modified: 2012-05-05
I am creating a database to track verifications on welds. The user is free to enter welds in whatever order they would like as welds are not always verified in order. I would like a query that would show all welds less than the greatest weld # that do not have a corresponding record in the database so that they can be listed for the user when they close the database to remind them that they still need to enter them. So far I have the attached code below however if I have records 1,2,3,9, and 10 it will only tell me that I am missing 4 and 11 instead of 4,5,6,7,and 8.
SELECT [Weld #]+1
FROM tblWeldLog AS T1
WHERE (((Exists (select * from tblWeldLog T2 where T2.[Weld #] = T1.[Weld #] + 1))=False));

Open in new window

0
Comment
Question by:FINGERHUT
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
jfmador earned 250 total points
ID: 22828597
There is no way to do that using a single table in a single query, because your database don't know which weld need to be done, also it cannot create rows that doesn't exists in your database. For example you have the tblWeldLog with two distinct entries 1 and 100, you want to get a dataset of 98rows.

To do it you need to have a table listing the welds to do like a Job table with column JobID, WeldID
where you can find your 10 rows representing the weld to do (from 1 to 10)

Then you will be able to do a query on your two table

SELECT * FROM tblJob j LEFT JOIN tblWeldLog w on j.WeldID = w.WeldID where w.WeldID is null

it will return you the job that don't have any log.

0
 
LVL 1

Author Comment

by:FINGERHUT
ID: 22831248
Could I perhaps create a table that just holds the values 1 - 9999 and create a query that finds all records that do not exist in the tblWeldLog but exist in NewTable and is not greater than the MAX value in tblWeldLog? If that could work could you help me write that query?

Thanks,

- FINGERHUT
0
 
LVL 1

Author Closing Comment

by:FINGERHUT
ID: 31511051
Sorry, I was confused when I originally read your response but in the end it was exactly what I needed. Thanks.
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
export sql results to csv 6 40
How to count the days a record spends in a step 21 51
SQL Select in Access 2003 3 26
MYSQL responding very slow 3 24
As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …

829 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