Steven Gotcher
Steven Gotcher

Reputation: 53

T-SQL Error on Join: An expression of non-boolean type specified in a context where a condition is expected

I have a data warehouse in Microsoft SQL Server with tables:

And I'm trying to run a simple query:

select user.name, co.firstname, co.lastname, acc.name
from contact co
join dbo.Account acc on co.accountid = acc.id
left join dbo.user on co.ownerid = user.id

It returns the error

Incorrect syntax near the keyword 'User'

And when I mouse over the word 'User', it says

An expression of non-boolean type specified in a context where a condition is expected.

It doesn't matter if I switch the order of the joins, it has a problem joining to the user table. What's the issue?

Upvotes: 1

Views: 2160

Answers (2)

Steven Gotcher
Steven Gotcher

Reputation: 53

Fixed it.

select

    u1.name 'Contact Owner',
    co.firstname 'Contact First Name',
    co.lastname 'Contact Last Name',
    u2.name 'Account Owner',
    acc.name 'Account Name'
from Contact co

    join dbo.Account acc on co.accountid = acc.id
    left join dbo.[user] u1 on co.ownerid = u1.id
    left join dbo.[user] u2 on acc.OwnerId = u2.id

Upvotes: 2

George
George

Reputation: 702

You missed alias for dbo.User. I called it usr

select usr.name, co.firstname, co.lastname, acc.name

from contact co

join dbo.Account acc on co.accountid = acc.id

left join dbo.user usr on co.ownerid = usr.id

Upvotes: 0

Related Questions