How do I resolve multi-part identifier errors - sql

I am unable to figure out the issue with the following query:
select distinct
i.Supplr1, v.Name, s.InvtID, s.CuryCost, s.CurySlsPrice, s.OrdNbr,
Sum(s.CurySlsPrice - s.CuryCost) AS GrossProfit,
Sum((s.CurySlsPrice - s.CuryCost)/s.CurySlsPrice) AS GrossPer
from
SOLine s, Inventory i, Vendor v
Inner Join
Inventory on Inventory.InvtID = s.InvtID
Inner join
Vendor on Vendor.VendID = Inventory.Supplr1
Union All
select distinct
a.InvtID, s.OrdNbr, s.OrdNbr, s.CuryCost, s.CurySlsPrice, s.OrdNbr,
s.Status, a.PerPost
from
ARTran a, Inventory i, SOLine S
Inner join
Inventory on Inventory.InvtId = ARTran.InvtID
Inner join
ARTran on ARTran.OrdNbr = SOLine.OrdNbr
The errors are as follows:
Msg 4104, Level 16, State 1, Line 20
The multi-part identifier "s.InvtID" could not be bound.
Msg 4104, Level 16, State 1, Line 31
The multi-part identifier "ARTran.InvtID" could not be bound.
Msg 4104, Level 16, State 1, Line 33
The multi-part identifier "SOLine.OrdNbr" could not be bound.
I have tried everything I know (my brain may be a little foggy) yet continue to get the errors.
What is the issue with my query?

You should clean up your query to something like this:
use only the proper JOIN syntax - get rid of that old, crappy "comma-separated list of tables" in the FROM clause!
use table alias on the joined tables
Try this:
SELECT
i.Supplr1, v.Name, s.InvtID, s.CuryCost, s.CurySlsPrice, s.OrdNbr,
SUM(s.CurySlsPrice - s.CuryCost) AS GrossProfit,
SUM((s.CurySlsPrice - s.CuryCost)/s.CurySlsPrice) AS GrossPer
FROM
SOLine s
INNER JOIN
Inventory i ON i.InvtID = s.InvtID
INNER JOIN
Vendor v ON v.VendID = i.Supplr1
GROUP BY
i.Supplr1, v.Name, s.InvtID, s.CuryCost, s.CurySlsPrice, s.OrdNbr
UNION ALL
SELECT DISTINCT
a.InvtID, s.OrdNbr, s.OrdNbr, s.CuryCost, s.CurySlsPrice, s.OrdNbr,
s.Status, a.PerPost
FROM
SOLine s
INNER JOIN
Inventory i ON i.InvtId = a.InvtID
INNER JOIN
ARTran a ON a.OrdNbr = s.OrdNbr

The columns you're SELECTing don't exist. Check your database schema

Related

SQL Server error: the multi-part identifier "C.Name" could not be bound

I have this SQL query:
CREATE VIEW QueryV5
AS
SELECT DISTINCT
C.Name, P.Name, SUM(Duration.Time) AS TotalTime
FROM
cmpt354_starwars.dbo.Characters C,
cmpt354_starwars.dbo.Planets P,
(SELECT (T.[Time of Departure] - T.[Time of Arrival]) AS Time
FROM cmpt354_starwars.dbo.TimeTable T
WHERE T.CharacterName = C.Name AND T.PlanetName = P.Name) AS Duration
WHERE
P.Affiliation = 'neutral'
And I get the errors:
Msg 4104, Level 16, State 1, Procedure QueryV5, Line 3
The multi-part identifier "C.Name" could not be bound.
Msg 4104, Level 16, State 1, Procedure QueryV5, Line 3
The multi-part identifier "P.Name" could not be bound.
I don't understand what's going on, why it won't let me use Duration as an alias for the nested query. I've compared my query to other people's and I can't see any syntactic differences. What's going on?
C.Name, P.Name both are having the same column name, that causing the issue. Please provide different alias name for any one of the column will solve the issue.
So this can be
SELECT DISTINCT C.Name, P.Name, SUM(Duration.Time) AS TotalTime
replace to this:
SELECT DISTINCT C.Name, P.Name AS PlantName, SUM(Duration.Time) AS TotalTime
UPDATE:
Could you try with the JOIN approach
CREATE VIEW QueryV5 AS
SELECT DISTINCT
C.Name, P.Name AS PlanetName, SUM(T.[Time of Departure] - T.[Time of Arrival]) AS TotalTime
FROM cmpt354_starwars.dbo.TimeTable T
JOIN cmpt354_starwars.dbo.Characters C ON C.Name = T.CharacterName
JOIN cmpt354_starwars.dbo.Planets P ON P.Name = T.PlanetName
WHERE P.Affiliation = 'neutral'
GROUP BY C.Name, P.Name
Please, use ANSI syntax of JOIN:
CREATE VIEW QueryV5
AS
SELECT T.CharacterName,
T.PlanetName,
SUM(T.[Time of Departure] - T.[Time of Arrival]) AS TotalTime
FROM cmpt354_starwars.dbo.TimeTable T
INNER JOIN cmpt354_starwars.dbo.Planets P
ON T.PlanetName = P.Name
INNER JOIN cmpt354_starwars.dbo.Characters C
ON T.CharacterName = C.Name
WHERE P.Affiliation = 'neutral'
GROUP BY T.CharacterName, T.PlanetName
And there is no need for C.Name and P.Name - use columns from TimeTable.

This sql not compiling but not sure why

I am getting error here: Msg 207, Level 16, State 1, Line 3
Invalid column name 'FACILITY_NPI'.
not sure why the 'f' is plainly there.
SELECT f.FACILITY_ID, count(f.FACILITY_ID) C
FROM [PBM].[T_CHARGES] A
Inner join PBM.FACILITY f on A.FACILITYNPI = f.FACILITY_NPI
INNER JOIN PBM.USER_FACILITY uf ON f.FACILITY_ID = uf.FACILITY_ID
However it shouldn't compile because you are doing a count and a select on a column that is not in a GROUP BY or a MAX/MIN function (aggregate function)
Did you forget the AS for the aliases ?
SELECT column AS C FROM somewhere AS W

how to figure out syntax error?

I am trying to write a SQL query that keeps giving me a syntax error. The problem is, the error message is not helping me at all.
Here's the query:
SELECT *
FROM
(SELECT
dbo.ciqCompanyUltimateParent.ultimateParentCompanyId,
COUNT(DISTINCT dbo.ciqCompany.companyId) AS Subsidiaries_Count,
COUNT(DISTINCT dbo.ciqCountryGeo.countryId) AS Countries_Count
FROM
dbo.ciqCompanyUltimateParent
INNER JOIN
dbo.ciqCompany ON dbo.ciqCompanyUltimateParent.companyId = dbo.ciqCompany.companyId
INNER JOIN
dbo.ciqCountryGeo ON dbo.ciqCompany.countryId = dbo.ciqCountryGeo.countryId
GROUP BY
dbo.ciqCompanyUltimateParent.ultimateParentCompanyId
)
INNER JOIN
dbo.ciqCompany ON ultimateParentCompanyId = dbo.ciqCompany.companyId
The stuff in brackets works fine when I execute it (i.e. it executes and returns a table). However, the last INNER JOIN is giving me the following error:
Msg 156, Level 15, State 1, Line 10
Incorrect syntax near the keyword 'INNER'.
What's even worse, when I cut down the above statement to
SELECT *
FROM
(SELECT
dbo.ciqCompanyUltimateParent.ultimateParentCompanyId,
COUNT(DISTINCT dbo.ciqCompany.companyId) AS Subsidiaries_Count,
COUNT(DISTINCT dbo.ciqCountryGeo.countryId) AS Countries_Count
FROM
dbo.ciqCompanyUltimateParent
INNER JOIN
dbo.ciqCompany ON dbo.ciqCompanyUltimateParent.companyId = dbo.ciqCompany.companyId
INNER JOIN
dbo.ciqCountryGeo ON dbo.ciqCompany.countryId = dbo.ciqCountryGeo.countryId
GROUP BY
dbo.ciqCompanyUltimateParent.ultimateParentCompanyId
)
I get a similar error -
Msg 102, Level 15, State 1, Line 9
Incorrect syntax near ')'.
Why can't I select * from a query that is working fine?
Try aliasing your subquery. E.g.
SELECT * FROM
(
....
) SUB
INNER JOIN dbo.ciqCompany
ON SUB.ultimateParentCompanyId = dbo.ciqCompany.companyId
Given the dbo, you're on MS SQL Server? You need to embed multiple joins in brackets:
SELECT ..
FROM table1
JOIN (table2 ON ...
JOIN (table3 ON ...
JOIN table4 ON ...
etc...
)
)

The multi-part identifier "Reservation.ReservationID" could not be bound

I am trying an inner join like this.
select R.ReservationID, R.BookingNumber,P.FirstName, P.LastName
from Reservation R inner join PersonName P
on Reservation.ReservationID = PersonName.ResrvationID
I am getting the errors as:
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "Reservation.ReservationID" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "PersonName.ResrvationID" could not be bound.
The aliases "R" and "P" masks the actual table name.
Although it does not apply here, it makes sense generally. For example, when you have a self join
select R.ReservationID, R.BookingNumber,P.FirstName, P.LastName
from Reservation R inner join PersonName P
on R.ReservationID = P.ReservationID --use aliasas
Try instead
select
R.ReservationID
, R.BookingNumber
, P.FirstName
, P.LastName
from Reservation R
inner join PersonName P
on R.ReservationID = P.ResrvationID

Advanced SQL INSERT

I have this query that works fine. It selects rows into the SY.UserOptions table for the ‘Jeff’ user.
However, I created another query that I want to do the same thing, but for every user. So I added SY.Users to the query, which in effect mulplies the 2 tables together. However, it gives me an error that I do not understand.
--This works
SELECT ‘Jeff’, t.Application, t.Task, tl.Description
FROM SY.Tasks t
LEFT OUTER JOIN SY.TaskLevels tl
ON t.Application = tl.Application And t.Task = tl.Task AND t.DftAccessLevel = tl.AccessLevel
-- This does not work
SELECT u.[User], t.Application, t.Task, tl.Description
FROM SY.Tasks t, SY.Users u
LEFT OUTER JOIN SY.TaskLevels tl
ON t.Application = tl.Application And t.Task = tl.Task AND t.DftAccessLevel = tl.AccessLevel
--Here is the error
Msg 4104, Level 16, State 1, Procedure CreateUserOptions, Line 15
The multi-part identifier "t.Application" could not be bound.
Msg 4104, Level 16, State 1, Procedure CreateUserOptions, Line 15
The multi-part identifier "t.Task" could not be bound.
Msg 4104, Level 16, State 1, Procedure CreateUserOptions, Line 15
The multi-part identifier "t.DftAccessLevel" could not be bound.
Can I not multiply tables together like that and include a join?
You need a field to join the users table to the Tasks table.
SELECT u.[User], t.Application, t.Task, tl.Description
FROM SY.Tasks t
INNER JOIN SY.Users u --LEFT OUTER if it makes a difference
ON t.user = u.User --not sure if these fields are available maybe some type of userId?
LEFT OUTER JOIN SY.TaskLevels tl
ON t.Application = tl.Application
And t.Task = tl.Task AND t.DftAccessLevel = tl.AccessLevel
I think the problem is that in the second query, when you join the SY.Users table and the SY.TaskLevels table, you are referencing the SY.Tasks table - which is not part of the join.
Switch the Sy.Users table and the Sy.Tasks table around and your problem should be fixed.
It's because you're joining USERS to TaskLevels instead of Tasks to TaskLevels.
Try This:
SELECT u.[User], t.Application, t.Task, tl.Description
FROM SY.Users u, SY.Tasks t
LEFT OUTER JOIN SY.TaskLevels tl ON t.Application = tl.Application And t.Task = tl.Task AND t.DftAccessLevel = tl.AccessLevel
This will give you the cartesian product of users with (Tasks TaskLevels) though. So every user will have every task.