Solved

Query to return missing values

Posted on 2008-10-28
3
198 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Format Date fields 11 64
SQL - Curser to do an insert based on a select 2 29
What does "Between" mean? 6 48
learning MS SSIS 13 24
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

752 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