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;
Related
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
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
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
i m using SQL 2008 R2
i got duplicate value from my join table:
SELECT *
FROM LETTRE_VOIT
LEFT JOIN FAWEB_CLIENT ON FAWEB_CLIENT.CODE_CLIENT = LETTRE_VOIT.CODE_CLIENT
LEFT JOIN ORDRE ON ORDRE.CODE_DEST = LETTRE_VOIT.CODE_DEST AND ORDRE.CODE_CLIENT = LETTRE_VOIT.CODE_CLIENT
AND ORDRE.DATE_CLOTUR = LETTRE_VOIT.DATE_CLOTURE
WHERE LETTRE_VOIT.NO_ORDRE IN ('5530','5533')
as you can see on image i got double value of 5530 and 5533.
my table FAWEB_CLIENT with ID Code_Client
table LETTRE_VOIT with ID NOID and NO_ORDRE
table ORDRE with ID NO_ORDRE and NO_CLIENT
i can not use DISTINCT:
error message: The text data type can not be selected as DISTINCT because it is not comparable
I suspect the problem is that your joins are not correct. My suspicion is that you are missing a join condition on Letter_Voit.No_Ordre:
SELECT *
FROM LETTRE_VOIT
LEFT JOIN FAWEB_CLIENT ON FAWEB_CLIENT.CODE_CLIENT = LETTRE_VOIT.CODE_CLIENT
LEFT JOIN ORDRE ON ORDRE.CODE_DEST = LETTRE_VOIT.CODE_DEST AND ORDRE.CODE_CLIENT = LETTRE_VOIT.CODE_CLIENT
AND ORDRE.DATE_CLOTUR = LETTRE_VOIT.DATE_CLOTURE and
LETTRE_VOIT.No_ORDRE = Order.No_ORDRE
WHERE LETTRE_VOIT.NO_ORDRE IN ('5530','5533')
You may be able to remove some of the other join conditions, which may become redundant.
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.