Query to update subscriber status to a list in salesforce marketing cloud - sql

I have a data extension that I will be populating my subscriber information into (I have to do this due to my API set up). However, I want this information to be pasted to my publication list in salesforce. If people unsubscribe, a field in my data extension called 'Subscribed' will be updated to 'False' (and vica-versa). I want to set up a query to run everday that scans this data extension and updates all the false records to unsubscibed in my publication list. I built the query below, but I am getting an error that say
"Incorrect syntax near the keyword 'INNER'."
SELECT b.EMAIL_ADDRESS, 'Unsubscribed' AS Status
FROM Welcome b
WHERE b.Subscribed = 'False'
AND Date_Changed >= DATEADD(day, -1, GETDATE())
INNER JOIN _ListSubscribers a
ON a.EmailAddress = b.EMAIL_ADDRESS
WHERE a.ListID = '112'
Thanks!

Where clause always goes after joins
SELECT b.EMAIL_ADDRESS, 'Unsubscribed' AS Status
FROM Welcome b
INNER JOIN _ListSubscribers a ON a.EmailAddress = b.EMAIL_ADDRESS
WHERE b.Subscribed = 'False' and
a.ListID = '112' and
b.Date_Changed >= DATEADD(day, -1, GETDATE());

Related

multi part indentifier could not be bound?

I'm trying to use two tables in a stored procedure but am getting this error on the final line
''multi part identifier T.HireDate could not be bound''
I'm guessing its to do with the joining of the tables but am a little lost. Heres my code:
CREATE PROC spPayIncreaseCheck
AS
SELECT T.ID,FName,LName,HireDate,Payrate
FROM Assignment.dbo.Payments pay
join Assignment.dbo.Teachers T
ON pay.ID = T.ID
UPDATE Assignment.dbo.Payments
SET Payrate = 'High'
where T.HireDate < dateadd(year, -3, GETDATE())
Are you trying to update, or are you trying to select?
That is the wrong form for an update using a join in sql server.
The correct form would be like so:
update pay
set Payrate = 'High'
from Assignment.dbo.Payments pay
inner join Assignment.dbo.Teachers T
on pay.ID = T.ID
where T.HireDate < dateadd(year, -3, getdate());
Also, are you sure on pay.ID = T.ID is the correct join clause? Normally you would expect to see something like on pay.TeacherID = T.ID instead.
You are updating the Assignment.dbo.Payments table and in the WHERE condition you have a t.HireDate but t is not an alias that is used in the UPDATE statement.
In UPDATE you cannot use aliases.
Also, please notice that the SELECT and UPDATE statements are completely non-related inside your stored procedure. It is not clear though if you need them to be related.
You need an UPDATE FROM to do a set-based update:
UPDATE Assignment.dbo.Payments
SET Payrate = 'High'
FROM
SELECT T.ID,FName,LName,HireDate,Payrate
FROM Assignment.dbo.Payments pay
JOIN Assignment.dbo.Teachers T ON pay.ID = T.ID
WHERE T.HireDate < DATEADD(YEAR, -3, GETDATE())
The simplest way to achieve this, is using an exists clause to check if the payment record matches the teacher record:
update Assignment.dbo.Payments
set Payrate = 'High'
where exists ( select 1
from Assignment.dbo.Teachers
where Payments.ID = Teachers.ID
and Teachers.HireDate < dateadd(year, -3, getdate()) )

SSIS Lookup Transformation - SQL query not working

I have an SSIS package for deployment into a SQL Server 2012 SSISDB and it uses a Lookup transformation. I am using a result from a SQL query to perform the lookup comparison.
This does not work and I get all rows as "No matched".
The query is the following:
DECLARE #LastJobDate DATETIME
SELECT #LastJobDate = COALESCE(MIN(S.LastImportDate), DATEADD(DAY, -2, GETDATE()))
FROM Stations S INNER JOIN
Lines L ON S.ID_Line = L.ID_Line
WHERE L.Name LIKE 'lineType%' AND S.ImportData = 1 AND S.Active = 1
SELECT J.ID_Line, J.ID_Job, J.SerialNumber
FROM [Jobs] J INNER JOIN
[Lines] L ON J.ID_Line = L.ID_Line
WHERE L.Name LIKE 'lineType%'AND J.TimeStamp >= DATEADD(HOUR, -1, #LastJobDate)
By accident, I found that if I place a [SET NOCOUNT ON] at the beggining of the query, it will work.
DECLARE #LastJobDate DATETIME
SET NOCOUNT ON
SELECT #LastJobDate = COALESCE(MIN(S.LastImportDate), DATEADD(DAY, -2, GETDATE()))
FROM Stations S INNER JOIN
Lines L ON S.ID_Line = L.ID_Line
WHERE L.Name LIKE 'lineType%' AND S.ImportData = 1 AND S.Active = 1
SELECT J.ID_Line, J.ID_Job, J.SerialNumber
FROM [Jobs] J INNER JOIN
[Lines] L ON J.ID_Line = L.ID_Line
WHERE L.Name LIKE 'lineType%'AND J.TimeStamp >= DATEADD(HOUR, -1, #LastJobDate)
Am I missing something? Why this behavior?
Why this behavior?
An SSIS Lookup Component can only consider the first result returned by a multi-statement query such as yours.
When you don't have SET NOCOUNT ON, the first result returned by your query will be the message "1 row(s) affected" or something like that. The Lookup Component will not be able to look at the result set returned by the second half of your query.
This is why setting NOCOUNT ON fixes the problem. The "row(s) affected" message will not be returned by the first part of the query, and the only thing returned will be the resultset of the second part of the query, which the Lookup Component will then process.

Create Stored procedure for setting reminder for document created

I want to create a Stored procedure for the
documents whose NStatusFlag should not be equal to 5 and 14
AND
also it should send Reminder to the respective user if the Document is with them for more than 3 days and if it is for more than 5 days then it should send it to its senior also
The date should be calculated with the U_datetime of the table and with the SYSTEM date from the system.
My NStatusFlag suggest which all documents are opened.
I tried below.
ALTER PROCEDURE GET_INWARD_REMINDER_REPORT
AS
BEGIN
Select * from inward_doc_tracking_trl
where NStatus_flag <> 5
and NStatus_flag <> 14
END
GO
SO I got all the documents which are opened, like below
But I am stuck how to get the all user by comparing with the date and send the reminders.
Also, I will get the Name of the USer with which the document is assigned from the inward_doc_tracking_hdr table.
NOTE inward_doc_tracking_hdr table mkey is related to ref_mkey from the inward_doc_tracking_trl table.
kindly help me out.
UPDATE
I get all the User from this query
ALTER PROCEDURE GET_INWARD_REMINDER_REPORT
AS
BEGIN
Select distinct a.mkey, b.mkey, a.first_name + ' ' + a.last_name from user_mst a
inner join inward_doc_tracking_hdr b
on a.mkey = b.User_Id
END
GO
UPDATE to get SEnior name
select * from inward_doc_tracking_hdr order by mkey desc -- here I get To_User
select * from user_mst where mkey = 187 -- here I get To_User
select Reporting_To,* from emp_mst where mkey = 122 -- here I get Senior
This query gets the tracking lines of people with documents for 3 days or more (you might need to be careful with the time component). Please test and see if this gets what you want.
Select TL.*
from inward_doc_tracking_trl TL
where TL.NStatus_flag NOT IN (5,14)
and TL.U_datetime <= DATEADD(d, -3, GETDATE())
This query then works out if its 3 or 5 days overdue and populates a second column if it's 3 days overdue:
Select
TL.*,
U.UserName,
CASE
WHEN TL.U_datetime <= DATEADD(d, -5, GETDATE())
THEN M.Reporting_To
ELSE NULL
END SeniorName
from inward_doc_tracking_trl TL
INNER JOIN inward_doc_tracking_hdr TH
ON TH.mkey = TL.ref_mkey
INNER JOIN user_mst U
ON TH.User_Id = U.mkey
INNER JOIN emp_mst M
ON M.mkey = U.employee_mkey
where TL.NStatus_flag NOT IN (5,14)
and TL.U_datetime <= DATEADD(d, -3, GETDATE())
You need to run that every day, I suggest inside a scheduled SQL Agent Job
With regards to 'sending a reminder', then assuming this is email, I suggest you first set up SQL Mail: https://msdn.microsoft.com/en-au/library/hh245116.aspx
Then use sp_send_dbmail https://msdn.microsoft.com/en-au/library/ms190307.aspx to send the email message
You'll also need to use the above query in a cursor and feed that into your sp_send_dbmail How to send multiple emails from sql server database using sp_send_dbmail procedure
Have a crack at that and ask more questions if you like

Redshift SQL Case Statement and WHERE Clause not working

I'm running the following SQL script to create a table with information on which marketing efforts are driving people to my site for form fills.
The case statement for marketing source stamp does not work. My results still have values for search-brand-whatever for the marketing_source column when those values should now just read "google-adwords".
I'm also getting results with a createddate of 9/15/2015 and after when my filter clearly states createddate >= '1/1/2016'.
Any ideas?
create table temp.roi_inqs as (
Select
a.ID,
cast(a.createddate as date) as date,
CASE
WHEN a.marketing_source_stamp__c = 'search%' then 'google-adwords'
ELSE a.marketing_source_stamp__c
END AS marketing_source,
CASE
when a.contactid is null then b.email
when a.contactid is not null then c.email
end as prospect_email
from rjm_current.sf_campaignmember a
left join rjm_current.sf_lead b on b.id = a.leadid
left join rjm_current.sf_contact c on c.id = a.contactid
where cast(a.createddate as date) >= '1/1/2016'
AND (
campaign_marketing_type__c in ('A','C'))
OR
(campaign_marketing_type__c = 'B'
AND a.status in ('Registered','Attended','No Show')));
Your entire predicate is of the form
A AND B OR C
If you don't put any parentheses, this means you're querying
(A AND B) OR C
When you really meant to write
A AND (B OR C)
As a general rule: Always put parentheses if you're mixing AND and OR
#Lukas Eder Answered my question for the date issue.
For the case statement I had to change the marketing_source_stamp__c = 'search%' to marketing_source_stamp__c LIKE 'search%' and that issue is solved.
CASE
WHEN a.marketing_source_stamp__c LIKE 'search%' then 'google-adwords'
ELSE a.marketing_source_stamp__c
END AS marketing_source,

Case Statement using Dates

I have a data set of clients. Each have a Start date and an End Date. A client can have multiple lines with different Start dates and End dates. I have another data set with Claims info and i want to know if they had claims during the time frame there state and end date.
how can i write this?
SELECT
M.[ID]
,EN.StartDate
,EN.EndDate
,[Has Cliams History] (Column to identify if yes or no)
FROM [Test].[dbo].[tblClients] M
left join [test].[dbo].[tblCliams] EN on EN.ID = M.ID
To illustrate the use of a case statement, the ClaimDate field represents the date of a claim. The query would look something like this.
SELECT
M.[ID]
,EN.StartDate
,EN.EndDate
,CASE
WHEN ClaimDate between EN.StartDate AND EN.EndDate THEN 'yes'
ELSE 'no'
END [Has Cliams History]
FROM [Test].[dbo].[tblClients] M
left join [test].[dbo].[tblClaims] EN on EN.ID = M.ID
Your question needs to provide more clarity on the definition of both tables and the relationship between the two. I am guessing you aren't actually joining a column called ID from both (at least I hope not). Here is a stab at it based on the assumption you have a ClientID on the tblClaims and also that you support ongoing clients via a NULL EndDate. Also assuming there are Start and End dates on tblClaims.
SELECT M.[ID]
, CASE WHEN MAX(claim.ID) IS NOT NULL THEN 1 ELSE 0 END AS HasClaimsHistory
FROM [Test].[dbo].[tblClients] client
LEFT JOIN [Test].[dbo].[tblCliams] claim on client.ID = claim.ClientID
AND claim.StartDate >= client.StartDate
AND (
client.EndDate IS NULL /* ongoing support? */
OR
claim.EndDate <= client.EndDate
)
GROUP BY M.[ID]
, CASE WHEN MAX(claim.ID) IS NOT NULL THEN 1 ELSE 0 END