Solved

Conditional Sql Insert

Posted on 2014-07-30
2
180 Views
Last Modified: 2014-07-31
Here is my situation

I have a table named CustomerOrderDate with the following fields (sql server 2012)

CustomerID        int
OrderDate          Date

Every night  CustomerOrderDate  table gets updated with a listing of customers that have placed orders during that day
(this table has like the last 5 years of order dates)

We would like to send an email to the customer  once every 30 days (regardless of number of orders placed)

Once an email is sent an entry is written to a table named CustomerSentEmail with following fields

CustomerID   int
EmailSentDate  date

I need to write a query that  writes an entry (with customerid and current date) to a table named CustomerScheduleEmailSchedule for either of the following

 if there is a customer id match between CustomerOrderDate and  CustomerSentEmail tables that has a max date that is 30 days or earlier than current date

if there is not a customer id match at between CustomerOrderDate and  CustomerSentEmail tables
(this means no email has been sent)


For example
If  customerorderdate  table has following  data,

CustomerID    OrderDate
       1                  4/30/2014
       1                  5/30/2014
       1                  6/30/2014
       1                  7/30/2014
       2                  7/30/2014
       3                  7/15/2014
       3                  7/30/2014

If  CustomerSentEmail  table has following  data,
CustomerID    EmailSentDate
1                  4/30/2014
1                   5/30/2014
1                   6/30/2014
3                   7/15/2014

Assuming current date is 7/30/2014

The query should insert the following into CustomerScheduleEmailSchedule

CustomerID     EmailScheduleDate
1                         7/30/2014 (because last email was sent 30 days ago)
2                         7/30/2014  (because no customer id match)

(no Entry for customer id 3 because last email was sent 15 days ago so not time)
0
Comment
Question by:johnnyg123
2 Comments
 
LVL 39

Expert Comment

by:Kyle Abrahams
ID: 40230217
-- get the max email sent per customer
; with CTE as
(
select
CustomerID , max(EmailSentDate) ESD
CustomerSentEmail  
group by customerID
)

--insert the customerId and today
insert into CustomerScheduleEmailSchedule
select cod.customerid, getdate()
from customerorderdate   cod
left join CTE  c on cod.CustomerId = c.CustomerID
where
--where there is no match found
c.CustomerID is null or
-- or the greatest date is less than 30 or equal to 30 days ago.
c.ESD  <= dateadd(d, getdate(), -30)
0
 
LVL 11

Accepted Solution

by:
John_Vidmar earned 500 total points
ID: 40230428
INSERT	CustomerSentEmail
(	CustomerID
,	EmailSentDate
)
SELECT	a.CustomerID
,	current_timestamp
FROM	(	SELECT	CustomerID
		,	maxdate	=	MAX(OrderDate)
		FROM	CustomerOrderDate
		GROUP
		BY	CustomerID
	)	a
LEFT
JOIN	(	SELECT	CustomerID
		,	maxdate	=	MAX(EmailSentDate)
		FROM	CustomerSentEmail
		GROUP
		BY	CustomerID
	)	b	ON	a.CustomerID = b.CustomerID
WHERE	b.CustomerID IS NULL
OR	b.maxdate < dateadd(d, current_timestamp, -30)

Open in new window

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

19 Experts available now in Live!

Get 1:1 Help Now