Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Identify if record exists in database

Posted on 2006-06-27
7
Medium Priority
?
360 Views
Last Modified: 2012-08-14
Hard to describe initially in title, but a little background- I have a simple web site setup that allows SSNs and Email addresses to be entered and fed to a SQL Server instance (a date/time stamp is included as well).  I am now working on the reporting portion of it which uses linked tables in Access to access the information itself.  The intent is to have someone be able to see all the ssns/emails for a given day or time period (I'll handle this portion with VBA in a form), but it should omit any results where the same ssn was already entered at a time earlier than the date range.  

Thanks for the help and let me know if anything didn't make sense!
0
Comment
Question by:Edvard_Grieg
  • 4
  • 2
7 Comments
 
LVL 12

Expert Comment

by:Einstine98
ID: 16995495
From the top of my head, you need something like:

SELECT *
FROM SSNTABLE
WEHERE EntryDATE BETWEEN x AND Y
AND SSNID NOT IN ( SELECT SSNID
                               FROM SSNTABLE
                               WHERE EntryDATE < x )

SSNTABLE : The table where you are storing your ssn number
SSNID : The ID for each row
...etc

I can think of couple of other ways of doing it... but this should hopefully do
0
 
LVL 2

Author Comment

by:Edvard_Grieg
ID: 16996318
Thanks!  The only other thing I'm thinking, is if the SSN occurs more than once within that initial time frame...
0
 
LVL 12

Accepted Solution

by:
Einstine98 earned 900 total points
ID: 16996436
I'm guessing that you want to have it appear once in this case...

SELECT *
FROM SSNTABLE
WEHERE EntryDATE BETWEEN x AND Y
AND SSNID NOT IN ( SELECT SSNID
                               FROM SSNTABLE
                               WHERE EntryDATE < x )
AND SSNID = (SELECT TOP 1 SSNID
                      FROM SSNTABLE AS A
                      WHERE A.SSNNUMBER = SSNTABLE.SSNNUMBER
                      ORDER BY SSNID DESC)

Chagne the order by as you please (desc or asc)... the other alternative is to use MIn/Max aggregate function on the same sub query, however I found the top 1 on large tables to be better!

0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 100 total points
ID: 16996457
in general use the EXISTS/NOT EXISTS syntax in place of IN /NOT IN

if you only want the "First" row displayed if it within the timeframe...

then you probably want this...

SELECT a.*
FROM SSNTABLE as a
WEHERE EntryDATE BETWEEN x AND Y
AND entrydate = (select min(entrydate)
                               FROM SSNTABLE as x
                               WHERE a.ssnid = x.ssnid )
0
 
LVL 12

Expert Comment

by:Einstine98
ID: 16996680
LowFatspread...

SELECT a.*
FROM SSNTABLE as a
WEHERE EntryDATE BETWEEN x AND Y
AND entrydate = (select min(entrydate)
                               FROM SSNTABLE as x
                               WHERE a.ssnid = x.ssnid )

will not exclude the values that already exist out of the date scope... if I got it right the requirement is to get the SSNs that are new and were not entered before, and if there are duplicate entries within the period then one of them should be picked...

My experience with SELECT MIN(...etc is that it works slower than top 1... but both do the job...
0
 
LVL 2

Author Comment

by:Edvard_Grieg
ID: 16996682
Thanks both of you!  What is the difference between exists/not exists and in/not in?
0
 
LVL 12

Expert Comment

by:Einstine98
ID: 16996817
Exists/Not Exists stops processing when the first matching occurence is found,

so basically if there are a million similar entries in the table and you are trying to make sure you are selecting values that do not exist within those million entries... you simple use

NOT EXISTS (SELECT...)

Which performs far better than in/not in as the server will have to first get the whole million records and then do the join and try to find out whether an entry exist or not.

Look them both up on books online, good examples are provided.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

773 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