-1

I am trying to inner join two copies of the table ActivtiyDay so I was assuming I could go ahead and join on these columns. The syntax that I am trying to use:

SELECT ds.id, ds.ActivityDay, ds.StepTotal, dc.id, dc.ActivityDay, dc.Calories
FROM public."dailySteps" as ds
LEFT JOIN public."dailyCalories" as dc
on public.dailySteps.ActivityDay = public.dailyCalories.ActivityDay

I have declared the correct columns from each table and I have declared each table that I am going to use.

Why is it throwing this error?

***ERROR:  missing FROM-clause entry for table "dailysteps"
LINE 8: on public.dailySteps.ActivityDay = public.dailyCalories.Acti...
           ^
SQL state: 42P01
Character: 158***
philipxy
  • 14,867
  • 6
  • 39
  • 83

1 Answers1

1

Once you declare a table alias, you must, from that point on, always refer to columns from that table via the table alias:

SELECT
      ds.id
    , ds.ActivityDay AS dsActvityDay
    , ds.StepTotal
    , dc.id
    , dc.ActivityDay AS dcActvityDay
    , dc.Calories
FROM PUBLIC.dailySteps AS ds
LEFT JOIN PUBLIC.dailyCalories AS dc ON ds.ActivityDay = dc.ActivityDay

nb: use the aliases in the join conditions also.

Paul Maxwell
  • 33,002
  • 3
  • 32
  • 51
  • I tried using this code and it throws me this error: "ERROR: relation "public.dailysteps" does not exist LINE 8: FROM public.dailySteps AS ds". This doesnt make any sense to me because this table clearly exist – Brandon Schultz Apr 19 '23 at 04:53
  • 1
    The ActivityDay columns mustn't have the same value, dc.ActivityDay can be null. – jarlh Apr 19 '23 at 06:58
  • @BrandonSchultz we cannot resolve issues such as the table does not exist. Check the spelling and or check that it isn't in a different schema. – Paul Maxwell Apr 19 '23 at 11:50