This sql not compiling but not sure why - sql

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

Related

How to correctly word an inner join

I am trying to learn joins and have read extensively here (SOFlow) and several other places and of course copied code and tried it out.
So I made this code to suit my table:
SELECT
a.FIRSTNAME, a.LASTNAME,
b.[LINE1], b.[LINE2], b.[LINE3], b.[SUBURB],
b.[STATE], b.[POSTCODE],
p.[PRIVATE], p.[BUSINESS], p.[MOBILE]
FROM
(SELECT
a.UNIQID, a.FIRSTNAME, a.LASTNAME
FROM [PTPARTY]) a
INNER JOIN
(SELECT
[LINE1], [LINE2], [LINE3], [SUBURB], [STATE], [POSTCODE], PARTYID
FROM
[PTAddresses]
WHERE
ADDRESS_TYPE = 'Mailing') b ON a.Uniqid = b.Partyid
INNER JOIN
(SELECT
[Uniqid], [PRIVATE], [BUSINESS], [MOBILE]
FROM [PTPhone]) P ON a.Uniqid = P.Uniqid
WHERE
a.UNIQID = 4
But I get:
Msg 4104, Level 16, State 1, Line 3
The multi-part identifier "a.UNIQID" could not be bound.
Msg 4104, Level 16, State 1, Line 3
The multi-part identifier "a.FIRSTNAME" could not be bound.
Msg 4104, Level 16, State 1, Line 3
The multi-part identifier "a.LASTNAME" could not be bound.
Where as this code perform perfectly
SELECT
a.FIRSTNAME, a.LASTNAME,
b.[LINE1], b.[LINE2], b.[LINE3], b.[SUBURB], b.[STATE], b.[POSTCODE],
p.[PRIVATE], p.[BUSINESS], p.[MOBILE]
FROM
(SELECT
PTPARTY.UNIQID, PTPARTY.FIRSTNAME, PTPARTY.LASTNAME
FROM [PTPARTY]) a
INNER JOIN
(SELECT
[LINE1], [LINE2], [LINE3], [SUBURB], [STATE], [POSTCODE], PARTYID
FROM
[PTAddresses]
WHERE
ADDRESS_TYPE = 'Mailing') b ON a.Uniqid = b.Partyid
INNER JOIN
(SELECT
[Uniqid], [PRIVATE], [BUSINESS], [MOBILE]
FROM [PTPhone]) P ON a.Uniqid = P.Uniqid
WHERE
a.UNIQID = 4
I am 99% sure the not Working code was copied from here with changes to fit my tables of course.
Just wondering if I am doing something wrong.
Well I am sure I am doing something wrong, but would like to know what.
Kind regards to you all and keep up the good work
You are qualifying the columns in the subquery but there is no corresponding table alias:
Select p.FIRSTNAME, p.LASTNAME, a.[LINE1],
a.[LINE2], a.[LINE3], a.[SUBURB], a.[STATE], a.[POSTCODE],
ph.[PRIVATE], ph.[BUSINESS], ph.[MOBILE]
from PTPARTY p inner join
PTAddresses a
on p.Uniqid = a.Partyid inner join
PTPhone ph
on p.Uniqid = Ph.Uniqid
where a.ADDRESS_TYPE = 'Mailing' and p.UNIQID = 4;
This is really much simpler.
Notice that I replaced the table alias with meaning aliases. However, you don't need the subqueries:
Check this Code. This will work. The error is due to first subquery as in first subquery you haven't aliased the table but used alias.column name. So change it to the following:
Select a.FIRSTNAME,a.LASTNAME, b.[LINE1],b.[LINE2],b.[LINE3],b.[SUBURB],b.[STATE],b.[POSTCODE],p.[PRIVATE],p.[BUSINESS],p.[MOBILE]
from
(SELECT UNIQID,FIRSTNAME,LASTNAME FROM [PTPARTY]) a
inner join
(SELECT [LINE1],[LINE2],[LINE3],[SUBURB],[STATE],[POSTCODE],PARTYID FROM [PTAddresses]
where ADDRESS_TYPE = 'Mailing') b on a.Uniqid = b.Partyid
inner join
(SELECT [Uniqid],[PRIVATE],[BUSINESS],[MOBILE]FROM [PTPhone]) P on a.Uniqid = P.Uniqid
where a.UNIQID = 4
To help be very specific:
Consider this part of your query:
FROM
(SELECT
a.UNIQID, a.FIRSTNAME, a.LASTNAME
FROM [PTPARTY]) a
When you remove the parenthesis from the subquery you have this:
SELECT
a.UNIQID, a.FIRSTNAME, a.LASTNAME
FROM [PTPARTY]
The item a.UNIQUID is in the form table alias.column name. However, inside the parentheses, you do not specify a table alias for [PTPARTY]. Thus, a.UNIQUID is invalid.
Since there is only 1 table, the alias or table reference is unnecessary. For clarity, I will add the optional "AS" from here on. With only 1 table, you can do this with only the column names:
FROM
(SELECT
UNIQID, FIRSTNAME, LASTNAME
FROM [PTPARTY]) AS a
Of course, you would often have joined tables and want to use aliases, so you can do that inside the parentheses:
FROM
(SELECT
z.UNIQID, z.FIRSTNAME, z.LASTNAME
FROM [PTPARTY] AS z) AS a
In this scenario, the table alias "z" is not available/valid outside of the parentheses. For that reason, even though it is probably not best practice, you can even re-use the same alias. Again, though, this can become confusing.
FROM
(SELECT
a.UNIQID, a.FIRSTNAME, a.LASTNAME
FROM [PTPARTY] AS a) AS a

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.

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...
)
)

SQL Server 2000 Group By asking for aggregate function when it is supplied

I must be missing something obvious but why does the following
SELECT c.ContractID, max( cs.ContractStatusCreated)
FROM dbo.NMPT_Contract AS c INNER JOIN
dbo.NMPT_ContractStatus AS cs ON c.ContractID = cs.ContractID INNER JOIN
dbo.CMSS_Status AS s ON cs.StatusID = s.StatusID
group by c.ContractID
having cs.ContractStatusCreated = MAX(cs.ContractStatusCreated)
return the following from SQL Server 2000?
Msg 8121, Level 16, State 1, Line 1 Column 'cs.ContractStatusCreated'
is invalid in the HAVING clause because it is not contained in either
an aggregate function or the GROUP BY clause.
Isn't MAX an aggregate function?
Because cs.ContractStatusCreated is not in the group-by clause, therefore it does not know where to get the data from.
And better yet, if you were to group by it you would end up with a HAVING 1=1 condition and your max(cs.ContractStatusCreated) wouldn't do what you want. I think you need a self-join or subquery to find your max value to compare to.
The error:
Msg 8121, Level 16, State 1, Line 1 Column 'cs.ContractStatusCreated' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause.
MAX is aggregate function
But ContractStatusCreated is NOT in the Group By
Haven't checked this but you could write your 'max' into a subquery something like this:
select ContractID, ContractStatusCreated
FROM dbo.NMPT_Contract AS x INNER JOIN
dbo.NMPT_ContractStatus AS y ON x.ContractID = y.ContractID
where (x.ContractID, y.ContractStatusCreated) = (
SELECT c.ContractID, max( cs.ContractStatusCreated)
FROM dbo.NMPT_Contract AS c INNER JOIN
dbo.NMPT_ContractStatus AS cs ON c.ContractID = cs.ContractID INNER JOIN
dbo.CMSS_Status AS s ON cs.StatusID = s.StatusID
group by c.ContractID
)

SQL Query is invalid in the select list

I dont know why this is coming up as invalid and I can not figure it out. I was given a legacy database as my supervisor left and I am in charge until someone comes to replace him. I am trying to run this query...
SELECT tblM.guidRId, SUM(dbo.tblCH.curTotalCost) AS curValue
FROM tblCH INNER JOIN
tblM ON tblCH.guidMId = tblM.guidMId INNER JOIN
ViewLM ON tblM.strNumber = ViewLM.strNumber
WHERE (tblM.guidRId = '4d832bc8-1827-4054-9896-6111844b0f26')
The error I keep getting is...Msg 8120, Level 16, State 1, Line 1
Column 'tblM.guidRId' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Why is this error occuring?
You are forgetting to group guidRId. (you are aggregating the data)
SELECT
tblM.guidRId,
SUM(dbo.tblCH.curTotalCost) AS curValue
FROM
tblCH
INNER JOIN tblM ON tblCH.guidMId = tblM.guidMId
INNER JOIN ViewLM ON tblM.strNumber = ViewLM.strNumber
WHERE
tblM.guidRId = '4d832bc8-1827-4054-9896-6111844b0f26'
GROUP BY tblM.guidRId
Because you need a GROUP BY if you are going to use an aggregate functon like SUM() [or COUNT, AVG(), etc...] with another non-aggregate column:
SELECT tblM.guidRId, SUM(dbo.tblCH.curTotalCost) AS curValue
FROM tblCH
INNER JOIN tblM
ON tblCH.guidMId = tblM.guidMId
INNER JOIN ViewLM
ON tblM.strNumber = ViewLM.strNumber
WHERE tblM.guidRId = '4d832bc8-1827-4054-9896-6111844b0f26'
GROUP BY tblM.guidRId;
Try:
SELECT
tblM.guidRId, SUM(dbo.tblCH.curTotalCost) AS curValue
FROM
tblCH
INNER JOIN tblM
ON tblCH.guidMId = tblM.guidMId
INNER JOIN ViewLM
ON tblM.strNumber = ViewLM.strNumber
WHERE (tblM.guidRId = '4d832bc8-1827-4054-9896-6111844b0f26')
GROUP BY tblM.guidRId