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
Solved

Identify if record exists in database

Posted on 2006-06-27
7
356 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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…

856 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