tags:

views:

48

answers:

4

I have two tables, ProblemChildren and DailyTable. ProblemChildren consists of an ID and a name. DailyTable consists of a ID, name, and date_hit.

Right now, I'm displaying all ProblemChildren.name where it exists in DailyTable. So if the name "Mike" in ProblemChildren does not have a record in DailyTable, then his name is not outputted.

Again right now, it's diplaying the ProblemChildren.name and the sum of amount times the name can be found in DailyTable. If "Mike" is not found within DailyTable, from ProblemChildren.name, then I still want the output to reflect "Mike" as 0.

How can I do this?

TRANSFORM Sum(DT.[Page Views]) AS [SumOfPage Views]
SELECT PC.CA_NTID AS ntid, PC.CA_Name
FROM tbl_ProblemChildren AS PC 
LEFT JOIN [Daily Table] DT 
ON DT.ntid = PC.CA_NTID 
WHERE DT.[report date] > #6/1/2010#
GROUP BY PC.CA_NTID, PC.CA_Name
PIVOT DT.[report date];
A: 

I'm thinking LEFT JOIN would help here.

Detect
Wouldn't that drop some of the entries that are meant to be kept?
mvrak
@mvrak An outer join (left/right) can't output less results than the equivalent inner join. The inner join means "If I can't fit the join, drop the lonely record". Left/Right outer means "If the lonely record is on the left/right table, keep him.". Full outer join takes all the records from both tables, and match them if it fits.
Tipx
+2  A: 
SELECT p.id, p.name, COUNT(d.date_hit)
FROM ProblemChildren p 
LEFT JOIN DailyTable d
ON d.id = p.id AND d.report_date > mm/dd/yyyy
GROUP BY  p.id, p.name

Or following the update to the question and feedback in the comments maybe

TRANSFORM Sum(DT.[Page Views]) AS [SumOfPage Views]
SELECT 
     PC.CA_NTID AS ntid, 
     PC.CA_Name
FROM tbl_ProblemChildren AS PC 
LEFT JOIN
(
SELECT [Page Views], [report date], ntid
FROM [Daily Table]  
WHERE [report date] > #6/1/2010#
) DT
ON DT.ntid = PC.CA_NTID 
GROUP BY PC.CA_NTID, PC.CA_Name
PIVOT DT.[report date];

Or Tipx's solution

TRANSFORM Sum(DT.[Page Views]) AS [SumOfPage Views]
SELECT PC.CA_NTID AS ntid, PC.CA_Name
FROM tbl_ProblemChildren AS PC 
LEFT JOIN [Daily Table] DT 
ON DT.ntid = PC.CA_NTID 
WHERE DT.[report date] > #6/1/2010# OR DT.[report date] IS NULL
GROUP BY PC.CA_NTID, PC.CA_Name
PIVOT DT.[report date];
Martin Smith
This works, but as soon as I put a WHERE clause in (such as WHERE d.report_date > mm/dd/yyyy) it goes back to only outputting any names that appear in the DailyTable.
Mik0r
@Mik0r - See edit. You need to put the predicate in the join.
Martin Smith
Hm. Access is throwing "JOIN expression not supported".
Mik0r
@Mik0r The thing you mentioned about the where clause is because you actually request for d.report_date to be there. You might have to check the syntax since I don't know what kind of SQL your server is, but you can use something like : WHERE (d.report_Date > mm/dd/yyyy OR d.report_Date = null)
Tipx
@Tipx - Good point that should do it if Access doesn't support these conditions in the `on` clause. It would need to be `is null` though.
Martin Smith
@Martin Smith -- THANK YOU! I'll accept this answer when I get home this evening.
Mik0r
Thank you both for your input -- both solutions work.
Mik0r
A: 
select *, count(*) from ProblemChildren pc 
right outer join DailyTable dt on pc.ID=dt.ID group by pc.ID

The code might not be perfect, but the point is that you need an outer join in order to keep values from both. Is setting the output = 0 instead of NULL necessary at the database level? That might mean the same thing to you.

mvrak
I think LEFT JOIN perhaps.
Larry Lustig
+1  A: 

I'm going to hazard a guess that you're using Access' query generator (the graphic interface that lets you build queries).

If so, right click on the line connecting the two tables and choose "Show all rows from ProblemChildren and only those rows from DailyTable that have a match" (or something similar, I don't have Access in front of me) then retry the query.

Larry Lustig