Solved

Identify if record exists in database

Posted on 2006-06-27
7
353 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
Comment Utility
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
Comment Utility
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 225 total points
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 25 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks both of you!  What is the difference between exists/not exists and in/not in?
0
 
LVL 12

Expert Comment

by:Einstine98
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Read about achieving the basic levels of HRIS security in the workplace.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 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

11 Experts available now in Live!

Get 1:1 Help Now