Join two select queries - sql

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

Related

Group BY clause to temporary table

I am beginner in SQL trying to group by a result
SELECT
API.PROJECTNUMBER, Project.ProjectName, Project.PROJMGRID, Staff.Email
INTO
#TEMPUnmatchedProjectWithSMoD
FROM
{ProjectsIncidentsIntegration} API WITH(NOLOCK)
JOIN
{Project} project WITH(NOLOCK) ON Project.ProjectNumber = API.PROJECTNUMBER
JOIN
{COMMON_STAFF} Staff WITH(NOLOCK) ON Staff.EmplId = Project.PROJMGRID
WHERE
NOT EXISTS (SELECT 1
FROM #DBName.DBO.NCompasS_TBL_SMoDIncident SMoD WITH(NOLOCK)
WHERE SMoD.ProjectCode = API.PROJECTNUMBER)
SELECT *
FROM #TEMPUnmatchedProjectWithSMoD
GROUP BY Project.PROJMGRID
I get an error:
Database returned the following error:
Error in advanced query UnmatchedProjectWithSMoD2: The multi-part identifier "Project.PROJMGRID" could not be bound.
The problem appears to be in this query:
SELECT *
FROM #TEMPUnmatchedProjectWithSMoD
GROUP BY Project.PROJMGRID ;
There are two major issues:
SELECT * with GROUP BY is not correct. What should be done with the columns that are not aggregation keys? You might have some idea. SQL generates an error.
Project is not defined.
It is quite unclear what this code is supposed to be doing, so I cannot suggest anything useful. You can just select all rows from the temporary table using:
SELECT p.*
FROM #TEMPUnmatchedProjectWithSMoD p;

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

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

Postgresql Join on Select Statement

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.