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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem With Join 1

Status
Not open for further replies.

kshanes

Programmer
Jan 9, 2002
21
0
0
US
I'm querying against 3 tables to get a count by type and year. The problem is that my counts are off as there are values in dbo_t_goal.assignedTo that are not in table ConsolidateGroups.user. I was attempting to do a left join to pick up those values in dbo_t_goal.assignedTo, but I keep getting syntax errors. I've read a bunch of posts, but every time I attempt to mimic what the other posts are doing, I still get syntax errors. The query is below. Any help would be greatly appreciated.

Code:
SELECT COUNT(*) AS Total, 
             GC.goalType AS [Goal Cat], CGR.ACL,
             DATEPART("yyyy",G.startDate) AS [Year]
FROM   ((dbo_t_goal as G left JOIN ConsolidateGroups as CGR on G.assignedTo = CGR.user)
             Left Join dbo_t_Goal_Category as GC on G.fk_GoalCatId = GC.pk_GoalCatId),
where   LEN(GC.validSQL) > 1
AND      GC.active = 'Y'
AND     G.status = 'C'
and     G.deleted = 'n'
and GC.goalType <> 'bogus value'
GROUP BY GC.goalType, CGR.ACL, DATEPART("yyyy",G.startDate)
ORDER BY GC.goalType, DATEPART("yyyy",G.startDate);
 
The problem is due to the selection on GC.
You need to do the selection first in a subquery and the join on the result of the selection. Remove the corresponding criteria from the main query.

..... Left Join (Select * from dbo_t_Goal_Category
where LEN(GC.validSQL) > 1
AND GC.active = 'Y'
and GC.goalType <> 'bogus value')as GC
on G.fk_GoalCatId = GC.pk_GoalCatId),
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top