SQL Server statement incorrect syntax near right? - sql

I'm trying to write a statement like this:
SELECT
TBL_CS_LINKS.LINK_ID,
TBL_CS_LINKS.LINK_NAME,
TBL_CS_LINKS.LINK_URL,
( SELECT COUNT(*)
FROM TBL_CS_TEMP_CLICK
WHERE TBL_CS_TEMP_CLICK.LINK_ID = 1)
FROM TBL_CS_LINKS
join right TBL_CS_TEMP_CLICK
on TBL_CS_LINKS.LINK_ID = TBL_CS_TEMP_CLICK.LINK_ID
WHERE
(CHARINDEX('s', TBL_CS_LINKS.LINK_URL) > 0) OR
(CHARINDEX('s', TBL_CS_LINKS.LINK_NAME) > 0)
order by TBL_CS_LINKS.LINK_NAME
and it gives me an error :
Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'right'.
What could be the problem ?
Thanks!

you got the keywords in the wrong order.
It's
right join
left join
or
right outer join
left outer join

You have used the right keyword incorrectly. Please check below for the proper use of it
SELECT TBL_CS_LINKS.LINK_ID,
TBL_CS_LINKS.LINK_NAME,
TBL_CS_LINKS.LINK_URL,
(SELECT COUNT(*) FROM TBL_CS_TEMP_CLICK WHERE TBL_CS_TEMP_CLICK.LINK_ID = 1)
FROM TBL_CS_LINKS
right join TBL_CS_TEMP_CLICK
on TBL_CS_LINKS.LINK_ID = TBL_CS_TEMP_CLICK.LINK_ID
WHERE (CHARINDEX('s', TBL_CS_LINKS.LINK_URL) > 0) OR
(CHARINDEX('s', TBL_CS_LINKS.LINK_NAME) > 0)
order by TBL_CS_LINKS.LINK_NAME

Related

Unknown SQL Syntax Error near Keyword 'left'

I am getting a syntax error "incorrect syntax near the keyword 'left'," but I don't know what I am doing wrong. I am trying to run an update query to set French addresses to 5. What am I missing?
UPDATE
Persons p
left join States s on p.StateID = p.pkState
SET
p.International = 5
WHERE
s.CountryRegionCodeID = 'FR';
The correct syntax in SQL Server uses a FROM clause:
UPDATE p
SET p.International = 5
FROM Persons p JOIN
States s
ON p.StateID = p.pkState
WHERE s.CountryRegionCodeID = 'FR';
Note: I changed the LEFT JOIN to a JOIN. The WHERE clause is turning the outer join into an inner join anyway.

PostgreSQL RIGHT JOIN ERROR: syntax error at or near "RIGHT"

Please tell me what is wrong with that sql ?
SELECT users_map.user_id
FROM users_map
WHERE users_map.service_id = 1
AND users_map.service_user_id = 0
RIGHT OUTER JOIN user_data
ON users_map.user_id = user_data.user_id
WHERE clause should be placed at the end of your query:
SELECT users_map.user_id
FROM users_map
RIGHT OUTER JOIN user_data
ON users_map.user_id = user_data.user_id
WHERE users_map.service_id = 1 AND
users_map.service_user_id = 0

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

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