Solved

Binary query to see if a date exists in a table

Posted on 2014-04-10
3
289 Views
Last Modified: 2014-04-10
I have to tables

table1 and table2

each of these tables has date1 and date2 respectively.

I have a process where I append the daily records from table2 to table1. I want to make sure they are not duplicated.

to do this I was thinking on checking and seeing that any of the dates in table2 exists in table 1

If any of the dates in table2.date2 exists in table1.date1

(Block select statement 1)
else
(block select statement2)

endif

I just need some help with the first if statement. How do I write the query so that it checks if any of the values in date2 exists in date1 giving me a true or false value.

thanks
0
Comment
Question by:damixa
  • 2
3 Comments
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 39992175
This code will INSERT only rows that don't already exist.

INSERT INTO dbo.table1 (
    date1, ...
    )
SELECT
    t2.date2, ...
FROM dbo.table2 t2
LEFT OUTER JOIN dbo.table1 t1 ON
    t1.date1 = t2.date2
WHERE
    t1.date1 IS NULL AND
    t1.date1 >= '...' AND
    t2.date2 >= '...'
0
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 500 total points
ID: 39992183
INSERT INTO table1 (date1)
SELECT t2.date2
FROM table2 AS t2
LEFT OUTER JOIN table1 AS t1
   ON t2.date2 = t1.date1
WHERE t1.date1 IS NULL

Obviously the column lists need to be adjusted to include any additional columns which you have not mentioned.
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 39992751
Interesting ... I thought my earlier-posted answer was more complete.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

910 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

24 Experts available now in Live!

Get 1:1 Help Now