rrhandle8
asked on
Help writing a query
Table "Members" contains a list of subscribers over the last 10 years. Some subscribers cancelled their subscription, but subscribed again at a later date. In this case, there are two records for that subscriber. One record contains the date of the cancellation in the field "DateUnsubscribed", the other record as a null in the field "DateUnsubscribed".
Each record contains multiple fields, but the ones I believe are of importance are:
Email and DateUnsubscribed (which is null in the case of an active subscriber).
I want to send a special offer to those subscribers who are not currently subscribed.
The database is MS ACCESS.
Each record contains multiple fields, but the ones I believe are of importance are:
Email and DateUnsubscribed (which is null in the case of an active subscriber).
I want to send a special offer to those subscribers who are not currently subscribed.
The database is MS ACCESS.
ASKER
If Bob subscribed in 2013, then cancelled, then subscribed again in Nov of 2014, he is in the database twice. Using your query, I would return his current active subscription, but I would not know that he had previously subscribed. I need to find everyone who has cancelled, and not re-subscribed at a later date.
Select email, unsubscribedate from subscriptions where unsubscribedate is null AND email not in (select email from subscriptions where unsubscribedate is null)
Correction:
Robert's solution is good but I thing needs to move not from the outer query to the inner query:
Select email, unsubscribedate from subscriptions where unsubscribedate is null AND email in (select email from subscriptions where unsubscribedate is not null)
Select email, unsubscribedate from subscriptions where unsubscribedate is null AND email in (select email from subscriptions where unsubscribedate is not null)
You have not told us much about the table(s)
You say that "there are two records for that subscriber"
HOW do we recognize this? are you using the same subscriber number in both records?
I suggest you provide "sample data" and the "expected result" from that data
You say that "there are two records for that subscriber"
HOW do we recognize this? are you using the same subscriber number in both records?
I suggest you provide "sample data" and the "expected result" from that data
ASKER
Robert,
Your query returned 0 records.
Your query returned 0 records.
ASKER
Email DateUnsubscribed
-------- ------------
r@r.com 10/27/14
r@r.com
b@b.com 10/14/13
c@c.com 12/12/14
Everyone with a date after the email is a subscriber who cancelled.
If NULL in the DateUnsubscriber field, then the subscription is active
r@r.com previously subscriber, then cancelled his subscription, but re-subscribed at a later date.
I want a list of subscribers who do not have an active account.
The desired list would contain b@b.com and c@c.com
-------- ------------
r@r.com 10/27/14
r@r.com
b@b.com 10/14/13
c@c.com 12/12/14
Everyone with a date after the email is a subscriber who cancelled.
If NULL in the DateUnsubscriber field, then the subscription is active
r@r.com previously subscriber, then cancelled his subscription, but re-subscribed at a later date.
I want a list of subscribers who do not have an active account.
The desired list would contain b@b.com and c@c.com
did you try:
Select email, unsubscribedate from subscriptions where unsubscribedate is null AND email in (select email from subscriptions where unsubscribedate is not null)
Select email, unsubscribedate from subscriptions where unsubscribedate is null AND email in (select email from subscriptions where unsubscribedate is not null)
ASKER
eghtebas,
Yes, I tried: Select email, unsubscribedate from subscriptions where unsubscribedate is null AND email in (select email from subscriptions where unsubscribedate is not null)
I got a strange list of 10 subscribers. The number should be about 200.
Yes, I tried: Select email, unsubscribedate from subscriptions where unsubscribedate is null AND email in (select email from subscriptions where unsubscribedate is not null)
I got a strange list of 10 subscribers. The number should be about 200.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
sorry, thought the name of table is members
select a.email, a.DateUnsubscribed
from subscriptions as a inner join (select b.email, Count(b.email) as CountOfemail
from subscriptions as b
group by b.email
having Count(b.email)=1) as c ON a.email = c.email
where a.DateUnsubscribed is not null
select a.email, a.DateUnsubscribed
from subscriptions as a inner join (select b.email, Count(b.email) as CountOfemail
from subscriptions as b
group by b.email
having Count(b.email)=1) as c ON a.email = c.email
where a.DateUnsubscribed is not null
ASKER
Bingo! we have a winner.
Thank you Rey!!!
Thank you Rey!!!
ASKER
The table name was members.
rrhandle8,
You may wish to add another Many To Many for the subscription status and date.
For example, what happens if a person unsubscribes/subscribes multiple times?
tblSubscriptionStatus
ssID
ss_mID
ssStatus (Unsubscribe/Subscribe="U" or "S")
ssStatusDate
ssReason
Create a query that joins the member stable and this new table, and gets the max date for each member(qryMaxStatusDates):
SELECT tblSubscriptStatus.ss_mID, Max(tblSubscriptStatus.ssS tatusDate) AS MaxOfssStatusDate, tblMembers.mEmail, tblMembers.mName
FROM tblMembers INNER JOIN tblSubscriptStatus ON tblMembers.mID = tblSubscriptStatus.ss_mID
GROUP BY tblSubscriptStatus.ss_mID, tblMembers.mEmail, tblMembers.mName;
Then create a second query (based on this query) to get only the Unsuscribed members (qryEmailUnsubscribed):
SELECT qryMaxStatusDates.ss_mID, qryMaxStatusDates.mEmail, qryMaxStatusDates.MaxOfssS tatusDate, DLookUp("ssStatus","tblSub ScriptStat us","ss_mI D=" & [ss_mID] & " And " & "ssStatusDate=" & "#" & [MaxOfssStatusDate] & "#") AS Status, qryMaxStatusDates.mName
FROM qryMaxStatusDates
WHERE (((DLookUp("ssStatus","tbl SubScriptS tatus","ss _mID=" & [ss_mID] & " And " & "ssStatusDate=" & "#" & [MaxOfssStatusDate] & "#"))="u"));
Then loop this query to send your email:
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("q ryEmailUns ubscribed" )
rst.MoveFirst
Do Until rst.EOF
DoCmd.SendObject acSendNoObject, , , rst!mEmail, , , "Special Offer for " & rst!mName, "Here is your special offer.", True
rst.MoveNext
Loop
MsgBox "Done"
rst.Close
Set rst = Nothing
this woks for me, ...sample database is attached.
You can modify the code to send emails automatically, ...etc,
...but first tell me if something like this would work for you...
JeffCoachman
Database80.mdb
You may wish to add another Many To Many for the subscription status and date.
For example, what happens if a person unsubscribes/subscribes multiple times?
tblSubscriptionStatus
ssID
ss_mID
ssStatus (Unsubscribe/Subscribe="U"
ssStatusDate
ssReason
Create a query that joins the member stable and this new table, and gets the max date for each member(qryMaxStatusDates):
SELECT tblSubscriptStatus.ss_mID,
FROM tblMembers INNER JOIN tblSubscriptStatus ON tblMembers.mID = tblSubscriptStatus.ss_mID
GROUP BY tblSubscriptStatus.ss_mID,
Then create a second query (based on this query) to get only the Unsuscribed members (qryEmailUnsubscribed):
SELECT qryMaxStatusDates.ss_mID, qryMaxStatusDates.mEmail, qryMaxStatusDates.MaxOfssS
FROM qryMaxStatusDates
WHERE (((DLookUp("ssStatus","tbl
Then loop this query to send your email:
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("q
rst.MoveFirst
Do Until rst.EOF
DoCmd.SendObject acSendNoObject, , , rst!mEmail, , , "Special Offer for " & rst!mName, "Here is your special offer.", True
rst.MoveNext
Loop
MsgBox "Done"
rst.Close
Set rst = Nothing
this woks for me, ...sample database is attached.
You can modify the code to send emails automatically, ...etc,
...but first tell me if something like this would work for you...
JeffCoachman
Database80.mdb
Open in new window
revised...