I want to do a join on a Select Statement in Postgresql, but I am having issues
SELECT
s.sessionid, sp.lang
FROM
sessions s
INNER JOIN
pages sp
ON
sp.sessionid = s.sessionid
INNER JOIN
(
SELECT
max(sessionid)
FROM
sessions
AS
maxSession
)
ON
maxSession = s.sessionid
WHERE
--Where condition
I get the following error:
ERROR: subquery in FROM must have an alias
LINE 6: (
^
HINT: For example, FROM (SELECT ...) [AS] foo.
If I add the FROM
FROM
(
SELECT max(sessionid)
FROM sessions
)
AS maxSession
I get another error
ERROR: syntax error at or near "FROM"
LINE 7: FROM
Ideas?
You are close.
INNER JOIN
(
SELECT
max(sessionid) as 'maxSession'
FROM
sessions
) maxSession
ON
maxSession.maxSession = s.sessionid
Any time you refer to a query as a table, you need to give it an alias...alias goes right after the entire subquery, not in the subquery itself.
Related
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;
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"?
I am querying the aact database from clinicaltrials.gov. The database model is right here: https://aact.ctti-clinicaltrials.org/schema. I have two schemas that I am selecting from (ctgov, proj_cdek_standard_orgs). I am trying to join two select statements. edit: I have now tried aliasing my subqueries, but that still does nothing. I get the following error:
(SELECT ctgov.sponsors.name, ctgov.sponsors.nct_id, ctgov.sponsors.id, ctgov.studies.phase
FROM ctgov.sponsors, ctgov.studies
WHERE ctgov.sponsors.nct_id=ctgov.studies.nct_id) A
FULL [OUTER] JOIN
(SELECT proj_cdek_standard_orgs.cdek_synonyms.id, proj_cdek_standard_orgs.cdek_synonyms.name
FROM proj_cdek_standard_orgs.cdek_synonyms) B
ON
A.name = B.name;
I can do both select statements perfectly fine on their own, but I try the query and I get this error:
ERROR: syntax error at or near "t1" LINE 7: ) t1
What did I do wrong and how do I use joins without getting syntax errors?
Please use below query,
SELECT ctgov.sponsors.name, ctgov.sponsors.nct_id, ctgov.sponsors.id,
ctgov.studies.phase, proj_cdek_standard_orgs.cdek_synonyms.id,
proj_cdek_standard_orgs.cdek_synonyms.name
FROM ctgov.sponsors, ctgov.studies, proj_cdek_standard_orgs.cdek_synonyms
WHERE ctgov.sponsors.nct_id=ctgov.studies.nct_id
and proj_cdek_standard_orgs.cdek_synonyms.name = ctgov.sponsors.name;
But the right way is to use traditional joins,
SELECT ctgov.sponsors.name, ctgov.sponsors.nct_id, ctgov.sponsors.id,
ctgov.studies.phase, proj_cdek_standard_orgs.cdek_synonyms.id,
proj_cdek_standard_orgs.cdek_synonyms.name
FROM ctgov.sponsors
INNER JOIN ctgov.studies
ON (ctgov.sponsors.nct_id=ctgov.studies.nct_id)
INNER JOIN proj_cdek_standard_orgs.cdek_synonyms
ON (proj_cdek_standard_orgs.cdek_synonyms.name = ctgov.sponsors.name);
You can change it to LEFT or FULL OUTER JOIN according to your requirement.
You have to provide an alias to the sub-queries. Also you should not use implicit joins as you have used in first subquery, always try to use explicit joins.
SELECT
*
FROM
(
SELECT
ctgov.sponsors.name, ctgov.sponsors.nct_id, ctgov.sponsors.id, ctgov.studies.phase
FROM ctgov.sponsors
JOIN ctgov.studies
ON ctgov.sponsors.nct_id=ctgov.studies.nct_id
) t1
FULL JOIN
(
SELECT
proj_cdek_standard_orgs.cdek_synonyms.id, proj_cdek_standard_orgs.cdek_synonyms.name
FROM proj_cdek_standard_orgs.cdek_synonyms
) t2
ON
t1.name = t2.name;
I am trying to join two select queries. Here is my query.
(select 'stack' as id from RDB$DATABASE) t
inner join
(select 'stack' as id from RDB$DATABASE) q
on t.id=q.id
When I execute this query throws me error
Context: Statement::Prepare( (select 'stack' as id from RDB$DATABASE) t
inner join
(select 'stack' as id from RDB$DATABASE) q
on t.id=q.id )
Message: isc_dsql_prepare failed
SQL Message : -104
Invalid token
Engine Code : 335544569
Engine Message :
Dynamic SQL Error
SQL error code = -104
Token unknown - line 1, column 1
(
If I understand the error correctly, Firebird doesn't like '(' at the start of the query.
If so, how can I join two queries?
The error "Token unknown - line 1, column 1 (" is returned because the presence of ( in that location is unexpected. Your current query only has two sub-queries it is trying to join, but it is missing the select and from clauses. This makes it a syntactically invalid statement. See SELECT in the Firebird 2.5 Language Reference for the full syntax.
A valid query would be
select *
from (select 'stack' as id from RDB$DATABASE) t
inner join (select 'stack' as id from RDB$DATABASE) q
on t.id=q.id
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.