how to use two sub queries in hive - hive

When I run below query I am getting the error.
FAILED: SemanticException [Error 10249]: Line 13:15 Unsupported SubQuery Expression 'master_cd': Only 1 SubQuery expression is supported.
SELECT
cfs.roll_no,
max(cclas.crdm_cd) as crdm_cd,
max(cclas.kjtm_cd) as kjtm_cd
FROM cust_focus cfs
LEFT JOIN cust_class cclas
ON (cfs.CF_CLAS_NO = cclas.CLAS_NO
AND cfs.DFS_CD = cclas.DFS_CD
AND cclas.D_AREA = 'US'
AND cclas.active_flag = 'Y')
WHERE cfs.roll_no NOT IN (SELECT roll_no FROM class_hist)
AND UPPER(TRIM(cfs.D_AREA)) = 'US'
AND (cfs.master_cd IN (SELECT msk5.msk5_master_cd from msk5_mst_tbl as msk5 WHERE cfs.master_cd=msk5.msk5_master_cd and msk5_m_code=9)
OR cfs.master_cd IS NULL)
group by cfs.roll_no;
Please help me how to resolve this issue.
Thanks in advance.

You can replace your second sub-query (SELECT msk5.msk5_master_cd from msk5_mst_tbl as msk5 WHERE cfs.master_cd=msk5.msk5_master_cd and msk5_m_code=9) with a join
...
from
cust_focus cfs
...
left join msk5_mst_tbl msk5
on cfs.master_cd=msk5.msk5_master_cd
and msk5_m_code=9

Related

PostgreSQL multiple join issue

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;

Simple SQL query getting "ORA-00918: column ambiguously defined"?

I'm just trying to get the sum of money from a column.
SELECT SUM(amount_usd)
FROM WIRE_MSTR, TRANS_MSTR
INNER JOIN WIRE_MSTR ON WIRE_MSTR.trans_id = TRANS_MSTR.trans_id
WHERE WIRE_MSTR.dest_cntry = 'CANADA' AND TRANS_MSTR.trans_yyyymm = '201510';
But on line 4 I get an error "ORA-00918: column ambiguously defined."
I've referenced everything, what could be the problem?
You are using implicit and explicit join syntax. You should remove the implicit syntax:
SELECT SUM(amount_usd)
FROM TRANS_MSTR
INNER JOIN WIRE_MSTR ON WIRE_MSTR.trans_id = TRANS_MSTR.trans_id
WHERE WIRE_MSTR.dest_cntry = 'CANADA' AND TRANS_MSTR.trans_yyyymm = '201510';
The problem was, you had WIRE_MSTR twice in your FROM clause.
Try this. You got WIRE_MSTR twice.
SELECT SUM(amount_usd)
FROM TRANS_MSTR
INNER JOIN WIRE_MSTR ON WIRE_MSTR.trans_id = TRANS_MSTR.trans_id
WHERE WIRE_MSTR.dest_cntry = 'CANADA' AND TRANS_MSTR.trans_yyyymm = '201510';

SemanticException [Error 10128]: Not yet supported place for UDAF 'sum' in Hive/Hue

From this sentence:
SELECT a.comunity, sum(b.cont_woman),sum(b.cont_men)
FROM cont_per_comunity.states_per_comunities a
JOIN cont_per_comunity.cont_per_state b
ON a.state = b.state
WHERE sum(b.cont_woman) >= sum(b.cont_men)
GROUP BY a.comunity;
I get the following error:
Error occurred executing hive query: Error while compiling statement: FAILED: SemanticException [Error 10128]: Line 9:6 Not yet supported place for UDAF 'sum'
Is there another way to select the sum of the data?
You need to do that in a having clause, or in an outer query. You can't use aggregate functions in a where clause like you are trying to do.
Try this:
SELECT a.comunity, sum(b.cont_woman),sum(b.cont_men)
FROM cont_per_comunity.states_per_comunities a
JOIN cont_per_comunity.cont_per_state b
ON a.state = b.state
GROUP BY a.comunity
having sum(b.cont_woman) >= sum(b.cont_men)
Or
select * from (
SELECT a.comunity, sum(b.cont_woman) as cont_woman
,sum(b.cont_men) as cont_men
FROM cont_per_comunity.states_per_comunities a
JOIN cont_per_comunity.cont_per_state b
ON a.state = b.state
GROUP BY a.comunity ) t
where cont_woman >= cont_men

How to use aggregate function in comparison

Im trying to run this query in DB2:
SELECT A.EMAIL_ADDR_ID, A.SENT_TS, B.SENT_TS FROM ecom.EMAIL_HIST A
INNER JOIN ecom.EMAIL_RTM_HIST B
ON A.EMAIL_ADDR_ID = B.EMAIL_ADDR_ID
WHERE max(b.SENT_TS) > max(a.SENT_TS)
GROUP BY A.EMAIL_ADDR_ID;
On trying to run the above query, I get this below error message.
SQL Error [42903]: Invalid use of an aggregate function or OLAP function..SQLCODE=-120, SQLSTATE=42903, DRIVER=3.64.114
Any pointers on what mistake I have done here?
Thank you!
solution 1
SELECT A.EMAIL_ADDR_ID, max(A.SENT_TS) maxsendTS_A, max(B.SENT_TS) maxsendTS_B
FROM ecom.EMAIL_HIST A INNER JOIN ecom.EMAIL_RTM_HIST B
ON A.EMAIL_ADDR_ID = B.EMAIL_ADDR_ID
GROUP BY A.EMAIL_ADDR_ID
having max(b.SENT_TS) > max(a.SENT_TS)
solution 2
modify your query like this
select * from (
SELECT A.EMAIL_ADDR_ID, max(A.SENT_TS) maxsendTS_A, max(B.SENT_TS) maxsendTS_B
FROM ecom.EMAIL_HIST A INNER JOIN ecom.EMAIL_RTM_HIST B
ON A.EMAIL_ADDR_ID = B.EMAIL_ADDR_ID
GROUP BY A.EMAIL_ADDR_ID
) tmp where maxsendTS_B>maxsendTS_A
You need a having clause:
SELECT A.EMAIL_ADDR_ID, MAX(A.SENT_TS), MAX(B.SENT_TS)
FROM ecom.EMAIL_HIST A INNER JOIN
ecom.EMAIL_RTM_HIST B
ON A.EMAIL_ADDR_ID = B.EMAIL_ADDR_ID
GROUP BY A.EMAIL_ADDR_ID
HAVING max(b.SENT_TS) > max(a.SENT_TS);

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