External data from file throws error while using where clause - sql

I am trying to read a text file with 2,000 values, but my where clause throws error.
SELECT mac.mac_id,mac.mac,mac.mac_type,record.soc_id
from mso_charter.mac
where record.soc_id in ('C:\Users\xyz\worldbox2_Prod_09-17-2017.txt')
join mso_charter.record on mac.record_id = record.header_id;
ERROR: syntax error at or near "join"
LINE 4: join mso_charter.record on mac.record_id = record.header_id;
^
********** Error **********
ERROR: syntax error at or near "join"
SQL state: 42601
Character: 155

This:
SELECT mac.mac_id,mac.mac,mac.mac_type,record.soc_id
from mso_charter.mac
join mso_charter.record on mac.record_id = record.header_id
where record.soc_id in ('C:\Users\xyz\worldbox2_Prod_09-17-2017.txt');
and unless you have multiple soc_Id's
where record.soc_id = ('C:\Users\xyz\worldbox2_Prod_09-17-2017.txt');
Not this:
SELECT mac.mac_id,mac.mac,mac.mac_type,record.soc_id
from mso_charter.mac
where record.soc_id in ('C:\Users\xyz\worldbox2_Prod_09-17-2017.txt')
join mso_charter.record on mac.record_id = record.header_id
Compilers are pretty picky about the order
SELECT
FROM
JOIN
WHERE
GROUP BY
HAVING
ORDER BY
Though order of execution is
FROM
JOIN
WHERE
GROUP BY
SELECT
HAVING
ORDER BY

Related

Inner-join errors on SQL stored procedure

I guess I'm just a bit weary, but the answer isn't jumping out for me right now. It throws an error
SQL Error [4145] [S0001]: An expression of non-boolean type specified in a context where a condition is expected, near 'WHERE'
Code:
ALTER PROCEDURE [dbo].[spDirectoryLookup]
(#LookupName nvarchar(100),
#ClientNumber bigint)
AS
BEGIN
SELECT DISTINCT
DL.subid,
C.ClientNumber,
C.ClientName,
listid,
dld.Description
FROM
INTELLIGENT_2414_1.DBO.dirListings DL
INNER JOIN
Intelligent_2414_1.DBO.cltClients C ON DL.subid = C.subid
INNER JOIN
INTELLIGENT_2414_1.DBO.dirListingDescriptions dld ON dld.listID
WHERE
Description LIKE '%'+#LookupName+'%'
AND DL.subid IN (SELECT subid
FROM Intelligent_2414_1.DBO.cltClients C
WHERE C.ClientNumber = #ClientNumber)
END
The second join's on clause is missing a condition. I'm guessing you meant to do this:
INNER JOIN INTELLIGENT_2414_1.DBO.dirListingDescriptions dld on dld.listID = DL.listid

syntax error at or near "JOIN" for postgresql

I am trying to join two tables, and for some reason I keeping getting:
ERROR: syntax error at or near "JOIN"
My code is:
SELECT
c.visit,
d.cake
FROM
customer c,
INNER JOIN desert d
ON c.visit = d.visit
LIMIT 10;
The comma is unnecessary:
SELECT
c.visit,
d.cake
FROM
customer c -- here
INNER JOIN desert d
ON c.visit = d.visit
LIMIT 10;

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

Join two select queries

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

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