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

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"?

Related

PostgreSQL how to use with as

Anybody know why this isn't working? I'm getting: ERROR: syntax error at or near "most_recent"
with most_recent as (SELECT MAX(public."Master_playlist".updated_at)
FROM public."Master_playlist")
SELECT * from public."Playlist"
JOIN public."Master_playlist_playlist" on public."Playlist".id = public."Master_playlist_playlist".playlist_id
JOIN public."Master_playlist" on public."Master_playlist_playlist".master_playlist_id = public."Master_playlist".id
WHERE public."Master_playlist".updated_at = most_recent;
Supposed to be getting the most recent date from Master_playlist and then using that to select a Master_playlist to join the inner query with
Thanks! HM
The with clause creates a derived table, which you need select from, using a join or a subquery. You also need to alias the column so you can refer to it afterwards, as in:
with most_recent as (
SELECT MAX(updated_at) max_updated_at
FROM public."Master_playlist"
)
SELECT *
from public."Playlist"
JOIN public."Master_playlist_playlist"
on public."Playlist".id = public."Master_playlist_playlist".playlist_id
JOIN public."Master_playlist"
on public."Master_playlist_playlist".master_playlist_id = public."Master_playlist".id
WHERE public."Master_playlist".updated_at = (SELECT max_updated_at FROM most_recent)
But here, it looks like it is simpler to use a row-limiting query:
select ...
from (
select *
from public."Master_playlist"
order by updated_at desc
limit 1
) mp
inner join public."Master_playlist_playlist" mpp
on mpp.master_playlist_id = mp.id
inner join public."Playlist" p
on p.id = mpp.playlist_id

SQL : Join variable table and view

I want to make an INNER JOIN between a table variable and a view
SELECT
T. *
FROM
#mytable AS T, Vw_GetPackageStat.DimensionValueName
From
dbo.Vw_GetPackageStat
INNER JOIN
T ON (dbo.Vw_GetPackageStat.AttributeValueID = T.AttributeValueID)
I have this error
Incorrect syntax near the keyword 'from'.
SELECT T.* ,
Vw_GetPackageStat.DimensionValueName
FROM #AllPackage AS T
INNER JOIN dbo.Vw_GetPackageStat ON ( dbo.Vw_GetPackageStat.AttributeValueID = T.AttributeValueID )
Use this
SELECT T.* ,
Vw_GetPackageStat.DimensionValueName
FROM #AllPackage AS T
INNER JOIN dbo.Vw_GetPackageStat ON
Vw_GetPackageStat.AttributeValueID = T.AttributeValueID
I think you were originally trying to do this
SELECT T.*
,Vw_GetPackageStat.DimensionValueName
FROM #mytable AS T
,Vw_GetPackageStat
WHERE dbo.Vw_GetPackageStat.AttributeValueID = T.AttributeValueID
this is a valid syntax but I would avoid it at all cost. Instead use traditional explicit INNER JOIN
SELECT T.*
,v.DimensionValueName
FROM #mytable AS T
INNER JOIN Vw_GetPackageStat AS v
ON T.AttributeValueID = V.AttributeValueID

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

Can Derby handle scalar subqueries in SELECT clause?

I'm having trouble getting my query working. Could someone cast an experienced eye on it please? The table structure is simple (2 one-to-many relationships). The query is trying to work out for each sign, how many contributions there are at each unique "PositionLocation".
Sign <- Signifier (f_key sign_oid) <- Contribution (f_key signifier_oid)
I'm getting the following error:
Error: An ON clause associated with a JOIN operator is not valid.
SQLState: 42972
ErrorCode: -1
My query is:
select s.NAME, c.POSITIONLOCATION, count(*) as num_per_locn,
(
select count(*) from APP.CONTRIBUTION c2
inner join APP.SIGNIFIER si2 on si2.OID = c2.SIGNIFIER_OID
inner join APP.SIGN s2 on s2.OID = si2.SIGN_OID
and s2.OID = s.OID
) as num_per_sign
from APP.CONTRIBUTION c
inner join APP.SIGNIFIER si on si.OID = c.SIGNIFIER_OID
inner join APP.SIGN s on s.OID = si.SIGN_OID
group by s.NAME, c.POSITIONLOCATION

What could create a syntax error if you take a SQL query and perform an UNION with itself?

I have this strange error in SQL Server 2005 where I take a working query, add the UNION keyword below it and then copy the query again. In my opinion, this should always be working, but it is not. I get the message 'Incorrect syntax near the keyword 'union'.
What could create this problem ?
To be more specific, here is the complete query :
select distinct deliveries.id, orders.id, 20 + sum(orders.mass1) as allowed_duration
from features_resources
inner join features on features.id = featureid
inner join orders on orders.id = features_resources.resourceid
inner join orderinformations on orders.id = orderinformations.orderid
inner join deliveries on orderinformations.deliveryid = deliveries.id
where features.name = 'O_FRAIS'
and (deliveries.ID IN
(SELECT ID
FROM dbo.DeliveriesInExportedSchedule))
group by deliveries.id, features.name ,orders.id order by deliveries.id
union
select distinct deliveries.id, orders.id, 20 + sum(orders.mass1) as allowed_duration
from features_resources
inner join features on features.id = featureid
inner join orders on orders.id = features_resources.resourceid
inner join orderinformations on orders.id = orderinformations.orderid
inner join deliveries on orderinformations.deliveryid = deliveries.id
where features.name = 'O_FRAIS'
and (deliveries.ID IN
(SELECT ID
FROM dbo.DeliveriesInExportedSchedule))
group by deliveries.id, features.name ,orders.id order by deliveries.id
I have tried to reproduce the error on a smaller query, by starting from a simple query and adding features one by one (inner join, nested queryes, group by, sum,....) but failed to reproduce the error again.
Any idea ?
It is actually the order by deliveries.id in the top half that causes the problem.
The order by needs to apply to the whole query.
Example Syntax
SELECT v1.number
FROM master.dbo.spt_values v1
WHERE v1.number > 2000
UNION
SELECT v2.number
FROM master.dbo.spt_values v2
WHERE v2.number < 10
ORDER BY v1.number
Try putting the individual SELECTs in parentheses:
(SELECT ... )
UNION
(SELECT ... )
The way you have it now, the second WHERE and GROUP BY clauses are ambiguous - should that apply to the SELECT, or to the UNION? I don't have any way to tell, and neither has your DB server.