Bouncing Between Multi-Part Identifier Could Not Be Bound & Ambiguous Column Name - sql-server

I keep getting two error messages depending on what I try to do to fix them.
Firstly here is my code:
SELECT
ConsltNum AS 'Consultant Number',
COUNT(ConsltNum) AS 'Client Count',
AVG(Balance) AS 'Average'
FROM
Client Cl
INNER JOIN
Consultant Cn ON Cl.ConsltNum = Cn.ConsltNum
GROUP BY
Cn.LastName
Upon running it, I get this.
Msg 209, Level 16, State 1, Line 3
Ambiguous column name 'ConsltNum'.
Msg 209, Level 16, State 1, Line 3
Ambiguous column name 'ConsltNum'.
Now, I know it is ambiguous because the ConsltNum exists in both tables I've included. Normally I'd try and dial into that database by changing the code to this:
SELECT
Client.ConsltNum AS 'Consultant Number',
COUNT(Client.ConsltNum) AS 'Client Count',
AVG(Balance) AS 'Average'
FROM
Client Cl
INNER JOIN
Consultant Cn ON Cl.ConsltNum = Cn.ConsltNum
GROUP BY
Cn.LastName
Upon running this, I get:
Msg 4104, Level 16, State 1, Line 3
The multi-part identifier "Client.ConsltNum" could not be bound.
Msg 4104, Level 16, State 1, Line 3
The multi-part identifier "Client.ConsltNum" could not be bound.
A few other things worth mentioning: I've tried dialling to dbo.Client.ConsltNum and it throws the same error.

Just use the right alias:
SELECT cl.ConsltNum AS Consultant_Number,
COUNT(*) AS Client_Count, AVG(?.Balance) AS Average
FROM Client Cl INNER JOIN
Consultant Cn
ON Cl.ConsltNum = Cn.ConsltNum
GROUP BY cl.ConsltNum;
The ? is for the the alias of the table where balance comes from.
Notes:
You need to use the table alias assigned for the column.
The GROUP BY should match the SELECT column.
Only use single quotes for string and date constants. Give columns names that don't need to be escaped.
You might was well use COUNT(*), because you know ConsltNum is never NULL.

Related

Error: The multi-part identifier could not be bound

select teamid,
a.playerid,
dbo.FullName(a.playerid) as fullName,
Total_Hits,
Total_At_Bats,
Totals_At_Bats,
Batting_Avg,
Team_Batting_Rank,
All_Batting_Rank
FROM batting
Error message received:
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "a.playerid" could not be bound.
Simply remove the a alias cause there no a alias/table in the FROM clause, that's why you get this error message, because SQL Server can't find playerid column in a table named/aliased a
select teamid,
playerid,
dbo.FullName(playerid) as fullName,
Total_Hits,
Total_At_Bats,
Totals_At_Bats,
Batting_Avg,
Team_Batting_Rank,
All_Batting_Rank
FROM batting
There is no column a.playerid there, unless you add an alias to your table as
FROM batting a

SQL Server syntax error identifier could not be bound

Why is this SQL getting a syntax error and how can it be fixed?
UPDATE s
SET s.modified_date = l.action_date
FROM
(SELECT l.action_date, l.user_id FROM item_audit_log) l
WHERE l.user_id = s.staff_id
Error:
Msg 4104, Level 16, State 1, Line 4
The multi-part identifier "l.action_date" could not be bound.
Msg 4104, Level 16, State 1, Line 4
The multi-part identifier "l.user_id" could not be bound.
You are getting the error because your subquery references "l" in the subquery, but it is not defined inside the subquery (it is defined in the outer scope). However, you don't need the subquery:
UPDATE s
SET modified_date = l.action_date
FROM staffs s JOIN
item_audit_log l
ON l.user_id = s.staff_id;
This assumes you have a table called s. I imagine that this really should be an alias, also defined in the FROM clause.
As your request is "WHY" this occurs, let me clarify it :
This error usually occurs when an alias is used when referencing a
column in a SELECT statement and the alias used is not defined
anywhere in the FROM clause of the SELECT statement.

SQL Server : Update with Where

I'm still learning SQL and I've been scavenging the internet for a solution.. For some reason I can't grasp the concept for this query 100%. I've been on this for a few days and getting no where. I do apologize in advance, I'm sure this is pretty simple for most of you.
I have a table called dbo.personnelUDF and dbo.personnel table in the same database. The dbo.personnelUDF references back to dbo.personnel.personid. Basically, I need to find the value located in dbo.personneludf.employee_id_ and update the dbo.personneludf.active_fueler_ = '1'
This is what am I'm getting.. what am I doing wrong??
UPDATE dbo.PersonnelUDF
SET Active_Fueler_ = '1'
FROM dbo.PersonnelUDF AS a
INNER JOIN dbo.Personnel ON dbo.Personnel.ObjectID = dbo.PersonnelUDF.ObjectID
WHERE dbo.PersonnelUDF.Employee_ID_ = 123456
Error:
Msg 4104, Level 16, State 1, Line 4
The multi-part identifier "dbo.PersonnelUDF.ObjectID" could not be bound.
Msg 4104, Level 16, State 1, Line 5
The multi-part identifier "dbo.PersonnelUDF.Employee_ID_" could not be bound.
You have aliased the table and then havent used it, you can do the below
UPDATE dbo.PersonnelUDF
set Active_Fueler_ = '1'
from dbo.PersonnelUDF INNER JOIN dbo.Personnel
ON dbo.Personnel.ObjectID=dbo.PersonnelUDF.ObjectID
where dbo.PersonnelUDF.Employee_ID_=123456
OR
UPDATE A
set Active_Fueler_ = '1'
from dbo.PersonnelUDF AS A INNER JOIN dbo.Personnel
ON dbo.Personnel.ObjectID = A.ObjectID
where A.Employee_ID_=123456
The error is because you have aliased PersonnelUDF in the FROM clause "AS a". If you get rid of that, it should work.

MDX+TSQL column name doesn't exist

I have to create a mixture of MDX and TSQL as follows:
select "[State].[Country].[Country].[MEMBER_CAPTION]" as State,
"[Measures].[someMeasure]" as [Sum]
from openquery(my_olap_server,
'select [Measures].[someMeasure] on 0,
filter([State].[Country].[Country],not isempty([Measures].[someMeasure])) on 1
from (select {[State].[Country].[Country].&[index_here]} on columns from [My Cube])')
If the MDX returns no value, than the [State].[Country].[Country].[MEMBER_CAPTION] does not exists, so the query fails with the message
Msg 207, Level 16, State 1, Line 2
Invalid column name '[State].[Country].[Country].[MEMBER_CAPTION]'
.
Is there a way to force either MDX or TSQL (but i'm guessing MDX) to provide this?
Thanks
select "[Measures].[StateName]" as State,
"[Measures].[someMeasure]" as [Sum]
from openquery(my_olap_server,
'with member [Measures].[StateName]
as [State].[Country].CURRENTMEMBER.MEMBER_CAPTION
select {[Measures].[StateName],[Measures].[someMeasure]} on 0,
filter([State].[Country].[Country],not isempty([Measures].[someMeasure])) on 1
from (select {[State].[Country].[Country].&[index_here]} on columns from [My Cube])')
is the solution. Simple enough :)

What is the error in DELETE query when using joins

Code is running fine when commenting DELETE statement and trying with SELECT statement.
Please help
DELETE FROM
--select * from
Site as s
join
(select SiteID,Code, Name, Dense_rank() over (partition by Code order by SiteID ) as Rank from Site
) as t
on s.SiteID = t.SiteID
WHERE t.Rank != 1
Getting following error message
Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'as'.
Msg 156, Level 15, State 1, Line 8
Incorrect syntax near the keyword 'as'.
You can't alias a delete table, but delete can refer to an alias. Instead of this:
delete from Site as s
...
Try:
delete from s
from Site as s
...

Resources