Error in Hive : For Exists/Not Exists operator SubQuery must be Correlated - hadoop

select * from students1;
students1.name students1.age students1.gpa
fred 35 1.28
barney 32 2.32
shyam 32 2.32
select * from students2;
students1.name students1.age
fred 35
barney 32
When I am running this query
select
name,age from students1
where not exists
(select name,age from students2);
I am getting this bellow error
Error while compiling statement: FAILED: SemanticException line 39:22
Invalid SubQuery expression 'age' in definition of SubQuery sq_1 [
exists (select name,age from students2) ] used as sq_1 at Line 3:10:
For Exists/Not Exists operator SubQuery must be Correlated.

The error message is clear. The subquery should be correlated when using exists/not exists.
select name,age
from students1 s1
where not exists (select 1
from students2 s2
where s1.name=s2.name and s1.age=s2.age
)

You are trying to achieve a MINUS output of a query. It is unfortunately not available in Hive.
You can read through the limitations of HQL and SQL here. HQL vs SQL
For usage of not exists, the manual has good example.
subqueries in hive

MINUS is now available in Hive. You can achieve this as the following:
select name,age from students1
MINUS
select name,age from students2;

Related

How do I use FETCH FIRST 1 ROWS ONLY in combination with UNION ALL in DB2?

See title.
This is what I'm trying:
select a.work_order_no
from (
select work_order_no as work_order_no
from work_order_line
where insert_timestamp is not null
FETCH FIRST 1 ROWS ONLY
union all
select work_order_no as work_order_no
from work_order_line
where insert_timestamp is null
FETCH FIRST 1 ROWS ONLY
) as a
FETCH FIRST 1 ROWS ONLY
But it give the following error:
SQL State: 42601 Vendor Code: -199 Message: [SQL0199] Keyword UNION not expected. Valid tokens: ). Cause . . . . . :   The keyword UNION was not expected here.  A syntax error was detected at keyword UNION.  The partial list of valid tokens is ). This list assumes that the statement is correct up to the unexpected keyword.  The error may be earlier in the statement but the syntax of the statement seems to be valid up to this point. Recovery  . . . :   Examine the SQL statement in the area of the specified keyword.  A colon or SQL delimiter may be missing. SQL requires reserved words to be delimited when they are used as a name. Correct the SQL statement and try the request again.  Processing ended because the highlighted statement did not complete successfully  Failed statements: 1
In SQL this concept would work with the 'top 1' syntax. I'm assuming this can also work in DB2 but I'm just doing something wrong with the syntax order?
I have asked a colleague and luckily he responded rather quickly:
I missed some ()
select a.work_order_no
from (
(select work_order_no as work_order_no
from work_order_line
where insert_timestamp is not null
FETCH FIRST 1 ROWS ONLY)
union all
(select work_order_no as work_order_no
from work_order_line
where insert_timestamp is null
FETCH FIRST 1 ROWS ONLY )
) as a
FETCH FIRST 1 ROWS ONLY

Is there any replacement for ROWNUM in Oracle?

I have JPA Native queries to an Oracle database. The only way I know to limit results is using 'rownum' in Oracle, but for some reason, query parser of a jar driver I have to use does not recognize it.
Caused by: java.sql.SQLException: An exception occurred when executing the following query: "/* dynamic native SQL query */ SELECT * from SFDC_ACCOUNT A where SBSC_TYP = ? and rownum <= ?". Cause: Invalid column name 'rownum'. On line 1, column 90. [parser-2900650]
com.compositesw.cdms.services.parser.ParserException: Invalid column name 'rownum'. On line 1, column 90. [parser-2900650]
How can I get rid of that?
ANSI Standard would be something like the following
SELECT *
FROM (
SELECT
T.*,
ROW_NUMBER() OVER (PARTITION BY T.COLUMN ORDER BY T.COLUMN) ROWNUM_REPLACE
FROM TABLE T
)
WHERE
1=1
AND ROWNUM_REPLACE < 100
or you could also use the following:
SELECT * FROM TABLE T
ORDER BY T.COLUMN
OFFSET 0 ROWS
FETCH NEXT 100 ROWS ONLY;

select data betwenn two date

I'm working in Hive on data set below
+++++++++++++++++++++++++++++++++++++++++++
code dateJ capa
+++++++++++++++++++++++++++++++++++++++++++
1988 2015-08-22 23
0470 2015-07-26 455
... ..... ...
5884 2015-08-01 54
4587 2015-06-05 100
I would like to pick up "code" from the table between two dates. query below works :
SELECT code FROM tabl WHERE dateJ BETWEEN '2015-06-05' AND '2015-08-22'
But when I use nested/sub-queries I doesn't work :
SELECT code FROM tabl WHERE dateJ BETWEEN (SELECT MIN(dateJ) FROM tabl) and (SELECT MAX(dateJ) FROM tabl)
Does any body could help on how I can fix the problem (with the second query). hive don't support subqueries.
Thx
I found a solution. Here it is :
select code from tabl,
(select min(dateJ) mindate, max(dateJ) maxdate from tabl) tmp
where dateJ between tmp.mindate and tmp.maxdate
This functionality is not supported in Hive, unfortunately. You can only use a select statement in the where clause in conjunction with IN, NOT IN, EXISTS, and NOT EXISTS. If you can find a way to construct the functionality you need using those and joins, then that's the way to go.

Oracle - Using variables in SELECT statement

My background in SQL is SQL Server, so forgive me for using Oracle in a similar way to it. I need to use a variable so I can use the value stored in it to perform a separate SELECT query. My aim is to find a percentage value and the variable will hold the total value of a table.
DECLARE
v_Count INT;
BEGIN
--get total rows
SELECT COUNT(OrderID)
INTO v_Count
FROM OrderedEquipment;
--find percentage of equipment ordered
SELECT a.Equip_Name, COUNT(b.EquipmentID), ((COUNT(b.EquipmentID)*1.0)/(v_Count*1.0)*100)
FROM Equipment a
LEFT OUTER JOIN OrderedEquipment b ON a.EquipmentID = b.EquipmentID
GROUP BY a.Equip_Name;
END;
SQL Developer will then throw this error:
Error report -
ORA-06550: line 10, column 1:
PLS-00428: an INTO clause is expected in this SELECT statement
I tried looking for solutions to this, and stumbled on to this post:
How do I use variables in Oracle SQL Developer?
But none of the answers really solved my problem. I tried using the bind variables example but that didn't work.
The simplest way is not using variable at all
SELECT a.Equip_Name, COUNT(b.EquipmentID),
(COUNT(b.EquipmentID)*1.0)/((SELECT COUNT(OrderID) cnt FROM OrderedEquipment)*1.0)*100
FROM Equipment a
LEFT OUTER JOIN OrderedEquipment b ON a.EquipmentID = b.EquipmentID
GROUP BY a.Equip_Name;
You can also in your block select data into 3 variables
... a.Equip_Name into v1, COUNT(b.EquipmentID) into v2,
((COUNT(b.EquipmentID)*1.0)/(v_Count*1.0)*100) into v3 ...
and list them with
dbms_output.put_line(v1||' '||v2||' '||v3);
Edit - this query should be faster:
with counter as (select count(OrderID) cnt from OrderedEquipment)
select a.Equip_Name, count(b.EquipmentID),
(count(b.EquipmentID)*1.0)/(max(counter.cnt)*1.0)*100
from Equipment a
left join OrderedEquipment b ON a.EquipmentID = b.EquipmentID
cross join counter
group by a.Equip_Name;

Display Records through SQL in Oracle

I had run following query in Oracle Database and produces following output:
Query: select id,name from member where name like 'A%';
ID Name
261 A....
706 Aaa.......
327 Ab.....
and more...
This Query returns 50 records and
I want to display 10 records at a time to user.
Since, ID does not contain data in autoincrement fashion, i cannot use between operator.
and rownum operator also doesn't help much.
Kindly Help.
Regards,
Ankit Agarwal
SELECT ID, Name
from (
select id,name, ROW_NUMBER() over( order by name) r
from member
where name like 'A%'
)
WHERE R between FromRowNum AND ToRowNum;
See http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:76812348057

Resources