Mutiple Where subqueries in Hive doesn't work - hadoop

I have a Query like below:
SELECT T.MTH_END_DT, T.SRC_SYS_CD, T.BTCH_ID
FROM PROD_RCRR.BAL_CNTRL_LOG T
WHERE T.SRC_SYS_CD='SL'
AND T.MTH_END_DT in (SELECT(MAX(MTH_END_DT)) FROM PROD_RCRR.BAL_CNTRL_LOG)
AND T.BTCH_ID in (SELECT(MAX(BTCH_ID )) FROM PROD_RCRR.BAL_CNTRL_LOG)
A error message shows Hive only can support one "in" clause. Anyone can give me a solution?

You can replace the whole thing with Join ON clause
SELECT
T.MTH_END_DT
, T.SRC_SYS_CD
, T.BTCH_ID
FROM PROD_RCRR.BAL_CNTRL_LOG T
JOIN ( SELECT
MAX(MTH_END_DT) ENDT
, MAX(BTCH_ID ) BTCH
FROM PROD_RCRR.BAL_CNTRL_LOG ) X
ON T.SRC_SYS_CD='SL'
AND T.MTH_END_DT = X.ENDT
AND T.BTCH_ID = X.BTCH

Related

Subqueries with select in hive

Team,
I have an issue here, have 2 temporary table a & b with value as 5 & 6 for the respective column like a.ref1 & b.ref2.
I am trying to get these values into another SQL like
"select c.col1, d.col1,d.col2 from c join d on a.id=d.id where d.col1=(schema_name).a.ref1 or
d.col2=(schema_name).b.ref2"
I get error like
"Invalid table alias or column reference "
. any thoughts, why it behaving like this. I tried with select query to pass the temp table values but this does not work in hive .Any further assistance would be appreciated
You you can do this by using something called Common Table Expression, This will make your query perform better.
It would look like this:
WITH
refined_d AS
(
SELECT
d.id,
d.col1,
d.col2
FROM
d
INNER JOIN
(schema_name).a
ON
( (schema_name).a.ref1 = d.col1)
INNER JOIN
(schema_name).b
ON
( ( schema_name).b.ref2 = d.col2)
)
SELECT
c.col1,
d.col1,
d.col2
FROM
c
JOIN
refined_d d
ON
c.id=d.id;

Use SUMMARIZECOLUMNS on table union in DAX query

I am trying to write a DAX query that runs the SUMMARIZECOLUMNS function on a table variable. The table variable is the union of two tables that have the same columns in the same order.
When I try to run the query, I get a Cannot find table error. Here is the query I am trying to run:
EVALUATE
VAR u = UNION(Table1, Table2)
RETURN SUMMARIZECOLUMNS(u[CreationYear], u)
How can I run this query on the union of the two tables?
It's not very elegant, but in response to your comment on Marco's solution, you can do a count like this:
EVALUATE
VAR u = UNION(Table1, Table1)
RETURN SUMMARIZE(u, [CreationYear],
"Count",
COUNTX(
FILTER(u,
[CreationYear] = EARLIER([CreationYear])
),
[Id]
)
)
Try using SUMMARIZE in stead of SUMMARIZECOLUMNS. Like this:
EVALUATE
VAR u = UNION ( Table1, Table2 ) RETURN SUMMARIZE ( u, [CreationYear] )

Error: ORA-00905: missing keyword when joining table to a select query

I am trying to link a table to a select query and I get the Error: ORA-00905: missing keyword. This is the oracle sql I have written.
When I run things separately data pulls. Its when I try to join them I get the error. I tried adding in the Group BY and Order by per some help information I found on the Internet but still get the same error.
SELECT
AS_MASTER_NF.CONTRACT_NO
, AS_HISTORY_NF.AH_CONTRACT_NBR
, AS_MASTER_NF.ID
, AS_MASTER_NF.A_INVENT_DATE
, AS_MASTER_NF.A_DISP_DATE
FROM INFL_IDS.AS_MASTER_NF
LEFT JOIN (SELECT
NVL(SUBSTR(ALTERNATE_ID, 0, INSTR(ALTERNATE_ID, '*')-1), ALTERNATE_ID) AS ASSET
, AS_HISTORY_NF.AH_CONTRACT_NBR
FROM INFL_IDS.AS_HISTORY_NF
WHERE LENGTH (AH_CONTRACT_NBR)> 3) AS ASHISTORY
ON INFL_IDS.AS_MASTER_NF.ID = INFL_IDS.ASHISTORY.ASSET
WHERE AS_MASTER_NF.A_INVENT_DATE IS NOT NULL
GROUP BY
AS_MASTER_NF.CONTRACT_NO
, AS_HISTORY_NF.AH_CONTRACT_NBR
, AS_MASTER_NF.ID
, AS_MASTER_NF.A_INVENT_DATE
, AS_MASTER_NF.A_DISP_DATE
ORDER BY
AS_MASTER_NF.CONTRACT_NO
, AS_HISTORY_NF.AH_CONTRACT_NBR
, AS_MASTER_NF.ID
, AS_MASTER_NF.A_INVENT_DATE
, AS_MASTER_NF.A_DISP_DATE
FETCH FIRST 20 ROWS ONLY
Change:
ON INFL_IDS.AS_MASTER_NF.ID = INFL_IDS.ASHISTORY.ASSET
to:
ON INFL_IDS.AS_MASTER_NF.ID = ASHISTORY.ASSET
Because, ASHISTORY is not a table or view under INFL_IDS schema but just a sub-query which's defined in this sql.

If statement in Oracle

I need to create a query, that if a certain field is blank or null. I need to do a select statement to another table and retrieve the blank field . Could you please advise on a way to accomplish this. Below is the query. The field in question is BEAT.
SELECT COALESCE(ADDRESSES.BEAT,Incident_addresses.beat)
, COALESCE (ADDRESSES.SUB_BEAT,Incident_addresses.sub_beat)
, ADDRESSES.STREET_NAME
, ADDRESSES.STREET_NUMBER
, ADDRESSES.SUB_NUMBER
, WARRANT_PEOPLE_VW.LNAME
, WARRANT_PEOPLE_VW.FNAME
, WARRANT_PEOPLE_VW.DOB
, WARRANT_PEOPLE_VW.RACE_RACE_CODE
, WARRANT_PEOPLE_VW.SEX_SEX_CODE
, WARRANT_PEOPLE_VW.CASE_NUMBER
, E_WARRANTS.DATE_ISSUED
, E_WARRANTS.TELETYPE_NUMBER
, E_WARRANTS.ORDINANCE_VIOLATION
FROM EJSDBA.ADDRESSES
, POL_LEEAL.E_WARRANTS
, POL_LEEAL.WARRANT_PEOPLE_VW,incident_people,Incident_addresses
WHERE ADDRESSES.ADDRESS_ID =E_WARRANTS.ADDR_ADDRESS_ID
AND E_WARRANTS.WARRANT_ID = WARRANT_PEOPLE_VW.WARRANT_ID
AND WARRANT_PEOPLE_VW.NME_TYP_NAME_TYPE_CODE = 'P'
AND WARRANT_PEOPLE_VW.AGNCY_CD_AGENCY_CODE = 'MCPD'
AND WARRANT_PEOPLE_VW.WSC_CODE='A'
AND EJSDBA.ADDRESSES.ADDRESS_ID= Incident_addresses.ADDRESS_ID
and incident_people.inc_incident_id=Incident_addresses.incident_id
ORDER BY ADDRESSES.BEAT
, ADDRESSES.SUB_BEAT
, ADDRESSES.STREET_NAME
, ADDRESSES.STREET_NUMBER
;
You can embed a correlated subquery into a case expression, but you MUST reference the table inside the subquery to some values of the outer query so that the correct value can be located.
SELECT
COALESCE(ADDRESSES.BEAT, Incident_addresses.beat)
, CASE
WHEN Addresses.BEAT IS NULL THEN (
SELECT
Beat
FROM incidents inner_ref
WHERE ???outer??.incident_id = inner_ref.id
AND rownum = 1)
END AS x
, COALESCE(ADDRESSES.SUB_BEAT, Incident_addresses.sub_beat)
...
and that subquery MUST only return a single value (hence I have used "and rownum = 1".
(In Oracle 12 you could use FETCH FIRST 1 ROW ONLY)

Convert oracle query to HQL by using subquery

I'm really confusing about sub Query of hibernate.
I've standard oracle query but unable to convert it into HQL.
select distinct b.nameId
from
(
select nameId from seg_user where id=1
)a, seg_user b
where b.id=a.nameId
can somebody convert it to HQL by using SubQuery or Crieteria
select distinct b.nameId
from seg_user b
where b.id = some (
select a.nameId from seg_user a where a.id=1
)
You can see how to use subqueries here: http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-subqueries

Resources