views:

35

answers:

2

This query works:

SELECT     u.UserId, u.GroupId, u.username,
            gp.PreferenceId, gp.Properties, gp.Override
        FROM          dbo.UserTable u 
        Inner JOIN
            dbo.GroupPreferences gp ON gp.GroupID = u.GroupId
        WHERE      (u.UserName = 'myUserId')

But the same query inside another query doesn't. I get the errors "Invalid column name 'GroupId'" and "Invalid Column Name 'UserId'". I imagine there is an extra ( or ) somewhere; please help me find it!

The outer query:

SELECT     coalesce(t1.PreferenceId,up.preferenceId) 
        as preferenceId, CASE t1.override WHEN 1 THEN COALESCE 
        (up.properties, t1.properties) 
        When 0 then t1.properties   
        ELSE up.properties END AS Properties
    FROM         
        (SELECT     u.UserId, u.GroupId, u.username,
            gp.PreferenceId, gp.Properties, gp.Override
        FROM          dbo.UserTable u 
        Inner JOIN
            dbo.GroupPreferences gp ON gp.GroupID = u.GroupId
        WHERE      (u.UserName = 'myUserId')) t1 
    full OUTER JOIN
        (select preferenceId, properties from UserPreferences u
        inner join userTable t on u.userId = t.userId and u.GroupId = 
        t.GroupId where userName = 'myUserId') up 
    ON t1.GroupId = up.GroupID AND t1.UserId = up.UserId
        AND t1.PreferenceId = up.PreferenceId
+5  A: 

You are referencing columns GroupId and UserId from the inline view aliased up, but it does not include those columns in its select list.

Change the 14th line to include those columns:

(select t.GroupId, t.userId, preferenceId, properties from UserPreferences u
Dave Costa
+3  A: 

This

(select preferenceId, properties from UserPreferences u
        inner join userTable t on u.userId = t.userId and u.GroupId = 
        t.GroupId where userName = 'myUserId') up 

doesn't have up.GroupID or up.UserId

ON t1.GroupId = up.GroupID AND t1.UserId = up.UserId
SQLMenace