Solved

Identify if record exists in database

Posted on 2006-06-27
7
358 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
[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
  • 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
Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

 
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

617 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