Solved

Identify if record exists in database

Posted on 2006-06-27
7
354 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 225 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 25 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
Creating and Managing Databases with phpMyAdmin in cPanel.
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…

929 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

16 Experts available now in Live!

Get 1:1 Help Now