how to figure out syntax error? - sql

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

Related

PostgreSQL multiple join issue

Iam trying to create query with multiple JOINS, but Im receiving error message regarding syntax on second JOIN.
ERROR: syntax error at or near "INNER"
LINE 19: INNER JOIN table2 AS jobinfo ON gaccess....
^
SQL state: 42601
Character: 418
Can you help me what am I doing wrong?
SELECT
access.id,
access.user_id,
access.device_id,
access.origin,
access.creation_date
FROM
table1 access
INNER JOIN (
SELECT
device_id,
MAX (creation_date) AS creation_date
FROM
table1
GROUP BY
device_id
) gaccess ON access.device_id = gaccess.device_id
AND access.creation_date = gaccess.creation_date;
INNER JOIN
table2 AS jobinfo
ON gaccess.device_id = jobinfo.id
You have a type - before the last INNER JOIN there is ; - you need to remove it.
SELECT access.id,
access.user_id,
access.device_id,
access.origin,
access.creation_date
FROM table1 access
INNER JOIN
(
SELECT device_id
,MAX(creation_date) AS creation_date
FROM table1
GROUP BY device_id
) gaccess
ON access.device_id = gaccess.device_id
AND access.creation_date = gaccess.creation_date
INNER JOIN table2 AS jobinfo
ON gaccess.device_id = jobinfo.id;

ERROR: 'Incorrect syntax near the keyword 'ORDER'.'

I'm getting an error from my SQL query:
SELECT TOP 20 * FROM
(
SELECT DISTINCT
p.ItemGroupName, p.Varenummer, s.EAN, s.inventoryQuantity
FROM
ShopInventory s, ProductData p
WHERE s.EAN = p.EAN
)
ORDER BY cast(inventoryQuantity AS int) DESC
ERROR: 'Incorrect syntax near the keyword 'ORDER'.'
Probably, you just need to give the subquery an alias:
SELECT TOP 20 * FROM
(
SELECT DISTINCT
p.ItemGroupName, p.Varenummer, s.EAN, s.inventoryQuantity
FROM
ShopInventory s, ProductData p
WHERE s.EAN = p.EAN
) mytable
ORDER BY cast(inventoryQuantity AS int) DESC
Some would say you are using the old join syntax instead of the recommended JOIN clause but for the purposes of solving your question I think thats a bit of a distraction. If you're interested in INNER JOIN , OUTER JOIN and all that you can read up here: What is the difference between "INNER JOIN" and "OUTER JOIN"?

Sql pgadmin confused

The following query gives me a syntax error:
Select
traseu_stud.An,
traseu_stud.CodSpec
from
traseu_stud
where
NumePren = "Popescu W.T. Vasile"
and AnUniv = "2012-2013"
inner join studenti on traseu_stud.matricol = studenti.matricol
inner join persoane on studenti.idPers = persoane.idPers
ERROR: syntax error at or near "inner"
LINE 3: ...Pren="Popescu W.T. Vasile" and AnUniv="2012-2013" inner join...
^
SQL state: 42601
Character: 122
You have to use subquery if you want use filter in this way:
select * from
(
Select traseu_stud.An,traseu_stud.CodSpec,matricol
from traseu_stud
where NumePren='Popescu W.T. Vasile' and AnUniv='2012-2013'
) a
inner join studenti on a.matricol=studenti.matricol
inner join persoane on studenti.idPers=persoane.idPers
otherwise you have to use filter below way
Select traseu_stud.An,traseu_stud.CodSpec from
traseu_stud inner join
studenti on traseu_stud.matricol=studenti.matricol inner join persoane on
studenti.idPers=persoane.idPers
where NumePren='Popescu W.T. Vasile' and AnUniv='2012-2013'
The JOIN go into the from clause.
Additionally: String constants need to be enclose in single quotes, double quotes are for identifiers:
Select
traseu_stud.An,
traseu_stud.CodSpec
from traseu_stud
inner join studenti on traseu_stud.matricol = studenti.matricol
inner join persoane on studenti.idPers = persoane.idPers
where NumePren = 'Popescu W.T. Vasile'
and AnUniv = '2012-2013'
thank you for your help, but I have same errors, I put here the image with tables and what I want to do: What Specialization(Specializare) and in what Study year(AnUniv) it's the Popescu W.T. Vasile in 2012-2013.
https://i.stack.imgur.com/e1iV1.jpg

Using a subquery in SELECT returns an error that I can't interpret

I have a table with order ids and I am trying to figure out the value/count
of those who came through facebook compared to those who didn't. As you see I put a subquery in the select which (on its own) shows the count of those who did not come through facebook. But as a subquery i get an error.
How do I fix this? Why is it not working?
SELECT
cs.country AS country,
COUNT(ltv.eco_id) AS count_ltv_fb,
ROUND(AVG(ltv.lifetime)/(365/12), 2) AS avg_life_months,
CONCAT('$ ', ROUND(AVG(fb.click_cost), 4)) AS click_cost,
CONCAT('$ ', ROUND(SUM(ltv.disc_ltv)/COUNT(distinct ltv.eco_id), 2)) AS disc_ltv,
COUNT(distinct ltv.eco_id) AS gns_count,
(
SELECT
COUNT(DISTINCT l.eco_id)
FROM published.company_status AS c
LEFT JOIN channel_analytics.facebook_ads_event AS f
ON f.order_id = sha2(CAST(c.company_id AS string),256)
LEFT JOIN ltv.ltv_forecast AS l
ON TRIM(SUBSTRING(l.eco_id,INSTR(l.eco_id,'|')+1)) = c.company_id
) AS count_ltv_not_fb
FROM published.company_status AS cs
LEFT JOIN channel_analytics.facebook_ads_event AS fb
ON fb.order_id = sha2(CAST(cs.company_id AS string),256)
LEFT JOIN ltv.ltv_forecast AS ltv
ON TRIM(SUBSTRING(ltv.eco_id,INSTR(ltv.eco_id,'|')+1)) = cs.company_id
WHERE fb.order_id IS NOT NULL
AND ltv.lifetime IS NOT NULL
AND cs.country in ('Australia', 'United States', 'Canada', 'United Kingdom')
GROUP BY cs.country
ORDER BY gns_count DESC
Error Msg
Error while compiling statement: FAILED: ParseException line 9:0 cannot recognize input near 'SELECT' 'COUNT' '(' in expression specification
You forgot to add the FROM before the subquery. It looks like Redshift (maybe using SQL Workbench?)
Edit:
Down voted? I didn't realize you were using a nested query until I wrote out the code. Have you tried putting the distinct before the count in the nested query? Also, you should add which type of SQL (PGSQL/TSQL) and which system you're using (Toad/MySQL/SQL Server/Workbench/Oracle/Toad/etc.).

SQL Server statement incorrect syntax near right?

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