Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Results from four tables even when one is empty

Status
Not open for further replies.

Ultradiv

Programmer
Mar 2, 2006
16
0
0
GB
There are four tables, all contain info needed in the result

Subscribers table
---------------------
SubscriberID(PK)
ListID(fK)
Subscribers details


List table
----------------------
LitsID(PK)
List Details


Message table
----------------------
MessageID(PK)
ListID(fK)
Message details


Tracking table
----------------------
SubscriberID(fK)
MessageID(fK)
Tracking details


There needs to be a result set that includes:
Subscriber details
Message details
List details
AND Tracking details (when they are available)

**Where ListID and MessageID are supplied**

Subscribers will always in in a List (with ListID)

If there are subscribers with NO messages sent to them it still needs to return subscribers.

If there are messages sent to that list of subscribers and no tracking yet, then it still needs to display Subscribers (with NULLs for tracking fields)

And when there are tracking results; display all the info
including Tracking details

Any help will be much appreciated
Thanks
Andy
 
Look up INNER JOINs and OUTER JOINs in the help doc for your respective platform.

INNER JOINs require that data be in both tables.
OUTER JOINs require that data be in one of the two tables.

Denny
MCSA (2003) / MCDBA (SQL 2000)
MCTS (SQL 2005 / Microsoft Windows SharePoint Services 3.0: Configuration / Microsoft Office SharePoint Server 2007: Configuration)
MCITP Database Administrator (SQL 2005) / Database Developer (SQL 2005)

My Blog
 
Thanks for that Denny, but I still can't get it to work.
Even if I reduce it to two tables, the second (Tracked) may or may not have a row corresponding to the first (Subscribers) determined by the supplied MessageID and the first tables' SubscriberID

So this does not do the trick:
Code:
SELECT Subscribers.*, tracked.* 
FROM Subscribers LEFT OUTER JOIN tracked 
ON Subscribers.SubscriberID = tracked.SubscriberID 
WHERE Subscribers.ListID = 1 AND tracked.MessageID = 13
I get a list returned that includes only those Subscribers that have entries in the tracked table I guess because of the AND in the WHERE clause

So how can I get it to return all Subscribers plus data from Tracked if there is a row for that SubscriberID and MessageID?

Cheers :)

Andy
 
Oh I've done it :)

this is how:

Code:
SELECT Lists.ListName, b.campaignID, b.DateOpened, b.CountOpened, b.DateLastOpened AS DLO, a.SubscriberID, a.Name, a.Subscribed, a.Email, a.Format, a.ListID, a.NumClicks, a.MessageLastOpened, a.SubjectMLO 
FROM Subscribers a INNER JOIN Lists ON a.ListID = Lists.ListID 
LEFT OUTER JOIN (SELECT campaignID, SubscriberID, MessageID, DateOpened, CountOpened, DateLastOpened FROM tracked WHERE MessageID = @messageID) b ON a.SubscriberID = b.SubscriberID 
WHERE a.ListID = @listID

I got the ideas from another of your posts Denny - thanks a million
best
Andy

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top