Skip to content
Advertisement

Querying a cte – The multi-part identifier could not be bound. How do I fix this so I can query the table? New to SQL accounting background

I am trying to extract a dataset which joins 3 tables

  • Employee E
  • AbsenceTypes AT
  • AbsenceBalance AB

In the data set I need the most recent record from AB.BalanceTime for each AB.EmployeeUID by AB.AbsenceTypesUID.

The data set is correct with the output I need, where it fails is when I query the CTE.

Error

Msg 4104, Level 16, State 1, Line 45
The multi-part identifier “E.FullName” could not be bound.

I have googled the problem & from what I understand the joined tables do not interact with the CTE which is why it fails with the condition below.

How can I alter the script so I can query the table?

Advertisement

Answer

Table alias E is defined inside the CTE only, not in the outer query. In that scope, there is only one (derived) table, that is called cte, and that has all column names that the CTE returns.

In other words, you can just do:

If you really want to use aliases, then alias the CTE, and then:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement