How to use maximum select queries in hive - oracle

I have two tables emp and dept in oracle. Have imported it to hive. The same structure is in hive. I need a query where I can select max no of empno coloumn in hive. Can I use ORDER BY EMPNO instead of select max(empno)?
This is the query for Oracle Database that I am using.
select a.empno,
a.ename,
a.hiredate,
a.mgr,
a.job,
a.sal,
a.comm,
a.deptno,
b.deptno,
b.dname,
b.loc
from emp2 a,
dept1 b
where a.deptno=b.deptno
and a.empno=(select max(empno) from emp2);
How can I select max empno in hive?

This should work:
SELECT a.empno,a.ename,a.hiredate,a.mgr,
a.job,a.sal,a.comm,a.deptno,b.dname,b.loc
FROM emp2 a, JOIN dept1 b
ON (a.deptno=b.deptno )
WHERE a.empno = max(b.empno)
GROUP BY a.empno,a.ename,a.hiredate,a.mgr,
a.job,a.sal,a.comm,a.deptno,b.dname,b.loc
;

Related

How can I update in oracle apex interactive grid for given sql query only SAL_DATE column on emp table ( group by has applied on this sql query )

Interactive grid sql query :
select
deptno,
sum(SAL),
SAL_DATE
from
emp
group by
deptno,
SAL_DATE
I am also giving the emp table details

converting correlated oracle subquery to single query

Hi I have two tables EMP and Dept( data as normal emp and dept tables of oracle)
I want to find the number of employees earning more than the avg(sal) of their own dept without use of correlated subquery. I wrote the query as below
SELECT * from emp e JOIN
(SELECT avg(sal) avgsal,deptno
FROM emp group by deptno )avgsal_tab
on e.sal > avgsal_tab.avgsal where e.deptno =avgsal_tab.deptno
order by e.deptno
This gets me the output but how can I rewrite this with a single query without inline query as shown above.
You can use analytic window functions:
SELECT *
from (
SELECT
e.*
,avg(sal)over(partition by deptno) avgsal
from emp e
)
where avgsal>sal

SQL Delete using joins

I am trying to delete rows from both emp and dept tables using the below query.
But after the query executed, it is deleting rows from only dept table and not from emp.
delete (
select * from emp e,dept d
where e.empno=d.empno
and e.sal in ('200','1950','5000')
and d.objid in ('1001','1002','1003')
);
I am using oracle 8i.
Please help!!
Thanks in Advance

how to find the table row count in hive query using group by

Here my question: I have a table with some records like (name, date, type). Suppose I have three type a, b and c.Now I want to count percentage of each type mean COUNT(type)/COUNT(table row count)??
select type,COUNT(type) as counttype,counttype/(select COUNT(*) from xyz) from xyz group by xyz;
"(select COUNT(*) from xyz)" this giving me error.
How to find the table Row Count?
You can use below query :-
select A.type,A.type_cnt,(A.type_cnt/B.total_cnt) from
(Select type,count(type) as type_cnt from xyz group by type)A
JOIN
(select count(*)as total_cnt from xyz)B
ON 1=1;
Without JOIN it is much faster, using analytic function:
select type,
count(type) as type_cnt,
count(type)/count(*) over() as pct
from xyz
group by type;

Not sure how to write connect by clause

I have the following query to find employees who are not managers in the EMP table of oracle
select * from emp e1
where not exists (select null from emp e2
where e2.mgr=e1.empno)
I need output using start with connect by clause , thus avoiding self join
There is a function, CONNECT_BY_ISLEAF(), which indicates whether a given row in a hierarchical query is a leaf node. In the EMP table, employees who are not managers will be leaf nodes.
So, we can use this function in a nested hierarchical query to filter the non-managers:
select empno, mgr, ename
from (
select empno, mgr, ename, CONNECT_BY_ISLEAF cbi
from emp
start with mgr is null
connect by prior empno = mgr
) where cbi = 1
/
Oracle has several neat functions for interrogating hierarchies. Find out more.

Resources