Spark sql subquery - apache-spark-sql

I can't find issue with below query. It keeps complaining about
cannot recognize input near 'SELECT' 'wrk_prd_dt' '.' in expression specification (state=42000,code=40000)
select tb1.name from dept tb1 LEFT JOIN emp lexp ON (lexp.id = tb1.id)
where tb1.prd_dt = (SELECT wrk_prd_dt.prd_dt FROM class wrk_prd_dt order by wrk_prd_dt.prd_dt asc limit 1);
tried Second approach but same error.
SELECT tb1.name
FROM dept tb1
WHERE tb1.prd_dt >= (select min(wrk.prd_dt) from class wrk)
limit 1

Related

ParseException line cannot recognize input near '(' 'select' 'from' in joinSource

I am trying to execute a query in hive and getting the error. I check over and over again but I cannot see any problem.
select
a.phone_no,
a.app_name
from
(select * from (select app_name,phone_no from lc_app_flag) ) a
inner join
(select * from (select phone_no,city_id_day,city_id_night,lat_day,lng_day,lat_night,lat_night from TW_FEATS_FIN_LCEXT01 where month_id='202205' ) b where city_id_day='440100' or city_id_night='440100') c
on a.phone_no=c.phone_no
You missed aliasing first innermost subquery. Correct SQL below -
select
a.phone_no,
a.app_name
from
(select * from (select app_name,phone_no from lc_app_flag) subq) a --inner subquery as subq
inner join
(select * from (select phone_no,city_id_day,city_id_night,lat_day,lng_day,lat_night,lat_night from TW_FEATS_FIN_LCEXT01 where month_id='202205' ) b where city_id_day='440100' or city_id_night='440100') c
on a.phone_no=c.phone_no

Oracle SQL COUNT in an EXISTS SELECT

I want to create a query where I get all posts from my table INV where the INNUM exists more than 2 times in table INVS.
This is my query right now, but it fails with the typical "missing right parenthesis" error.
But when I run the EXISTS Query isolated, it works....
SELECT WO.WONUM, WO.DESCRIPTION, INV.INNUM, INV.STATUSDATE
FROM INV LEFT OUTER JOIN WO ON INV.WOID = WO.WOID
WHERE EXISTS (
SELECT COUNT(*) FROM INVS WHERE INVS.INNUM = INV.INNUM and INVS.SITEID='ARZ' GROUP BY INVS.INNUM
HAVING COUNT(*) > 2 ORDER BY INVS.INNUM
);
I dont really know why!?
Hmmm . . . use a scalar subquery to calculate the count and compare to "2" in the outer query:
SELECT WO.WONUM, WO.DESCRIPTION, INV.INNUM, INV.STATUSDATE
FROM INV LEFT OUTER JOIN
WO
ON INV.WOID = WO.WOID
WHERE (SELECT COUNT(*)
FROM INVS
WHERE INVS.INNUM = INV.INNUM AND
INVS.SITEID = 'ARZ'
) > 2;
Your query is relying on a doubly nested correlation clause which Oracle does not support.
You could also move the subquery to the FROM clause, but this version is more in the spirit of how you have written the query.
You get ORA-00907: missing right parenthesis while using the ORDER BYclause in the subquery.
Remove it and you get a valid syntax
Example
with tab as (select rownum id from dual connect by level <= 5
union all
select 3 from dual union all
select 5 from dual)
select * from tab t
where exists
(select count(*) from tab
where id = t.id
group by id
having count(*) > 1)
;
ID
----------
3
5
3
5
This is not a valid syntax --> ORA-00907: missing right parenthesis
select * from tab t
where exists
(select count(*) from tab
where id = t.id
group by id
having count(*) > 1 order by id)

LEFt JOIN LATERAL showing error with SELECT

I am trying to run below query :
SELECT
tc.ID_NUMBER AS AFC_RPP_Number,
hc.BUSINESS AS Business,
hc.DIRECTOR AS Director,
tc.REASON_FOR_REVISION AS Description_of_Change
FROM alo_gg.AWS_PIM tc
left join lateral(
select BUSINESS,DIRECTOR
FROM alo_ggg.tracker
WHERE START_DATE <= tc.DATE AND SO = tc.SO
ORDER BY START_DATE DESC
LIMIT 1
) hc;
Above query is showing error:
ERROR: syntax error at or near "SELECT"
left join lateral (SELECT BUSINESS,DIRECTOR...
If I run the subquery separately it is giving me a result, but with lateral it is giving me an error.
You need to add ON TRUE and remove comma:
SELECT
tc.ID_NUMBER AS AFC_RPP_Number,
hc.BUSINESS AS Business,
hc.DIRECTOR AS Director,
tc.REASON_FOR_REVISION AS Description_of_Change
FROM alo_gg.AWS_PIM tc -- removing comma
left join lateral(
select BUSINESS,DIRECTOR
FROM alo_ggg.tracker
WHERE START_DATE <= tc.DATE AND SO = tc.SO
ORDER BY START_DATE DESC
LIMIT 1
) hc ON TRUE; -- adding `ON` clause

SQL Correlation error using OVER & PARTITION in SELECT statement

I am getting the following error when I am trying to execute my SQL SELECT statement
Could not execute statement.
Correllation name 'contact' not found
SQLCODE=-142, ODBC 3 State"42S02"
Line 1, Column 1
My code is as follows
Select forename, surname, email, quotedate
From ( SELECT *, ROW_NUMBER() OVER (PARTITION BY tblQuote.contno ORDER BY quoteno DESC) AS rn
FROM dba.quotehdr as tblQuote left join dba.contact as tblContact on tblQuote.contno = tblContact.contno)q
where rn = 1 and quotedate <=today()-720 and emailbounced = 0 and email is not null and dba.contact.statusflag = 'A'
order by quotedate desc
This error only happended when I added in
dba.contact.statusflag = 'A'
I have tried this as
tblContact.statusflag = 'A'
and I get the same error!
Any suggestions?
(What about q.statusflag = 'A' , as it seems you are using q as an Alias.) This original answer is not correct, amended to:
#Shannon Severance is correct in his comment. You are trying to use the Where clause on the outer query - which does not contain any fields from the contact table. Let me tidy your query to help you see your subquery (q) - as:
Select
forename
,surname
,email
, quotedate
From
(
SELECT
*
, ROW_NUMBER() OVER (PARTITION BY tblQuote.contno ORDER BY quoteno DESC) AS rn
FROM dba.quotehdr as tblQuote
left join dba.contact as tblContact on tblQuote.contno = tblContact.contno
) q
left join dba.contact as tblContact on q.contno = tblContact.contno
where rn = 1
and quotedate <=today()-720
and emailbounced = 0
and email is not null
and tblContact.statusflag = 'A' -- Now sourced from last left join
order by quotedate desc
You will need another LEFT JOIN on the dba.contact table to be able to access this field (ADDED NOW as an example).
Also, depending on your database engine - if your field is duplicated in both tables, the SELECT * in a subquery may eject those fields, or rename them, or throw an error. Run your inner subquery by itself and see what it produces, or use explicit field name instead of *
(I still really think your * in the subquery is causing the error and also the confusion. Remove it and replace with table.field names - this will help you understand what is going wrong ...Otherwise your query logic is pretty fine, and adding the extra left join that I suggest is overkill)

MySQL subquery returns more than one row

I am executing this query:
SELECT
voterfile_county.Name,
voterfile_precienct.PREC_ID,
voterfile_precienct.Name,
COUNT((SELECT voterfile_voter.ID
FROM voterfile_voter
JOIN voterfile_household
WHERE voterfile_voter.House_ID = voterfile_household.ID
AND voterfile_household.Precnum = voterfile_precienct.PREC_ID)) AS Voters
FROM voterfile_precienct JOIN voterfile_county
WHERE voterfile_precienct.County_ID = voterfile_County.ID;
I am trying to make it return something like this:
County_Name Prec_ID Prec_Name Voters(Count of # of voters in that precienct)
However, I am getting the error:
#1242 - Subquery returns more than 1 row.
I have tried placing the COUNT statement in the subquery but I get an invalid syntax error.
If you get error:error no 1242 Subquery returns more than one row, try to put ANY before your subquery. Eg:
This query return error:
SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);
This is good query:
SELECT * FROM t1 WHERE column1 = ANY (SELECT column1 FROM t2);
You can try it without the subquery, with a simple group by:
SELECT voterfile_county.Name,
voterfile_precienct.PREC_ID,
voterfile_precienct.Name,
count(voterfile_voter.ID)
FROM voterfile_county
JOIN voterfile_precienct
ON voterfile_precienct.County_ID = voterfile_County.ID
JOIN voterfile_household
ON voterfile_household.Precnum = voterfile_precienct.PREC_ID
JOIN voterfile_voter
ON voterfile_voter.House_ID = voterfile_household.ID
GROUP BY voterfile_county.Name,
voterfile_precienct.PREC_ID,
voterfile_precienct.Name
When you use GROUP BY, any column that you are not grouping on must have an aggregate clause (f.e. SUM or COUNT.) So in this case you have to group on county name, precienct.id and precient.name.
Try this
SELECT
voterfile_county.Name, voterfile_precienct.PREC_ID,
voterfile_precienct.Name,
(SELECT COUNT(voterfile_voter.ID)
FROM voterfile_voter JOIN voterfile_household
WHERE voterfile_voter.House_ID = voterfile_household.ID
AND voterfile_household.Precnum = voterfile_precienct.PREC_ID) as Voters
FROM voterfile_precienct JOIN voterfile_county
ON voterfile_precienct.County_ID = voterfile_County.ID
See the below example and modify your query accordingly.
select COUNT(ResultTPLAlias.id) from
(select id from Table_name where .... ) ResultTPLAlias;