Hive Subquery in SELECT - sql

I have a query like
SELECT name, salary/ (SELECT max(money) from table_sal) FROM table_a;
I get an error saying
Unsupported SubQuery Expression Invalid subquery. Subquery in SELECT could only be top-level expression
Is there a way to resolve this?

Does this work with a CROSS JOIN?
SELECT name, salary / s.max_money
FROM table_a CROSS JOIN
(SELECT max(money) as max_money from table_sal) s

You can also do this as below, please let me know if it works for you.
Select t1.name
, t1.salary/T2.max_money
from
(SELECT name
, salary, 1 as dummy
from table_a ) t1
Join
(SELECT max(money) as max_money
, 1 as dummy
from table_sal) t2
on t1.dummy = t2.dummy ;

Related

How to create a select clause using a subquery

I have the following sql statement:
WITH
subquery AS (
select distinct id from a_table where some_field in (1,2,)
)
select id from another_table where id in subquery;
Edit
JOIN is not an option (this is just a reduced example of a bigger query)
But that obviously does not work. The id field exists in both tables (with a different name, but values are the same: numeric ids). Basically what I want to do is filter by the result of the subquery, like a kind of intersection.
Any idea how to write that query in a correct way?
You need a subquery for the second operand of IN that SELECTs from the CTE.
... IN (SELECT id FROM subquery) ...
But I would recommend to rewrite it as a JOIN.
Are you able to join on ID and then filter on the Where clause?
select a.id
from a.table
inner join b.table on a.id = b.id
where b.column in (1,2)
Since you only want the id from another_table you can use exists
with s as (
select id
from a_table
where some_field in (1,2)
)
select id
from another_table t
where exists ( select * from s where s.id=t.id )
But the CTE is really redundant since all you are doing is
select id
from another_table t
where exists (
select * from a_table a where a.id=t.id and a.some_field in (1,2)
)

Join table and pick rows where for given id exists only one value

I don't know, if I made good title, but please let me visualize this.
So I have two tables and for given case I need to select row where payment currency was ONLY in EUR.
Correct document Id's will be: 2, 3, 4, 5
These are overall bigger tables with 900k+ records.
Can you please suggest me how query should look?
use correlated subquery with not exists
select distinct a.document_id from tablename a inner join tablename b b on a.document_id=b.payment_docid
where not exists
(select 1 from tablename b1 where b1.payment_docid=b.payment_docid and currency<>'EUR')
Try this query:
select payment_docId from MyTable
group by payment_docId
having max(currency) = 'EUR'
and min(currency) = 'EUR'
or you could use having count(*) = 1 with min or max as well.
use corelated subquery
select t1.* from table2 as t1
where exists( select 1 from table2 t2 where t1.payment_docid=t2.payment_docid
having count(distinct currency)=1)
and currency='EUR'
It is possible to use INNER JOIN with the following conditions to get all rows:
SELECT
pd.payment_doc_id
, pd.currency
FROM DocTable dt
INNER JOIN PaymentDocs pd
ON dt.document_id = pd.payment_doc_id AND pd.currency IN ('EUR')
If you want distinct rows, then you can apply operator GROUP BY:
SELECT
pd.payment_doc_id
, pd.currency
FROM DocTable dt
INNER JOIN PaymentDocs pd
ON dt.document_id = pd.payment_doc_id AND pd.currency IN ('EUR')
GROUP BY pd.payment_doc_id
, pd.currency
Aggregation is the only efficient want :
select doc_id
from table t
group by doc_id
having min(currency) = max(currency) and min(currency) = 'EUR';

ParseException - mismatched input in subquery source - error in Hive

I am running the next query in Hive:
SELECT COUNT(*)
FROM
(
SELECT *
FROM
(SELECT id, COUNT(*) AS count_p_id FROM palladion GROUP BY id) a,
(SELECT cid, COUNT(*) AS count_q_cid FROM operations GROUP BY cid) b
WHERE a.id=b.cid
)
WHERE count_p_id < count_q_cid;
I keep getting the error like
ParseException line 1:103 mismatched input ',' expecting ) near 'a' in subquery source
What is the problem with the code? I can't see any.
Implicit join notation is supported starting with Hive 0.13.0. This allows the FROM clause to join a comma-separated list of tables, omitting the JOIN keyword. For example:
SELECT *
FROM table1 t1, table2 t2
WHERE t1.id = t2.id
I hope you are using < 0.13.0 version . If your hive version is < 0.13.0
Try this : you have to use JOIN - ON , not Comma - WHERE
SELECT COUNT(*)
FROM
(
SELECT *
FROM
(SELECT id, COUNT(*) AS count_p_id FROM palladion GROUP BY id) a JOIN
(SELECT cid, COUNT(*) AS count_q_cid FROM operations GROUP BY cid) b
ON a.id=b.cid
)
WHERE count_p_id < count_q_cid;

Join between two sql statements

can i join between two sql satatmnet
example
select ID, NAME from tab1;
12|blabla
and
Select DESC from tab2;
vvvvvv
I want to display to
12|blabla|vvvvvv
I was try the the UNION
select ID, NAME from tab1;
UNION
Select DESC from tab2;
but the result was
12|blabla
12|vvvvv
any help !!
You could do this:
select t1.*, t2.*
from (select ID, NAME from tab1) t1 cross join
(Select "DESC" from tab2) t2;
DESC is a SQL reserved word, so you need to escape it. Double quotes are one way of doing that.
Dont use UNION but instead use JOIN

SQL selecting minimum value in a sub query when exact value is unknown

I have an SQL query that is meant to select a list of things from different tables using a subquery. I am meant to find those things with the lowest value in a particular column.
This is the query that i currently have. I know the minimum rate is 350 but i cant use it in my query. Any effort to change it to MIN(rate) has been unsuccessful.
SELECT DISTINCT name
FROM table1 NATURAL JOIN table2
WHERE table2.code = (SELECT Code FROM rates WHERE rate = '100')
How do i change that subquery to find the minimum rate?
Most general way to do this would be
select distinct name
from table1 natural join table2
where
table2.code in
(
select t.Code
from rates as t
where t.rate in (select min(r.rate) from rates as r)
)
if you have windowed functions, you can use rank() function:
...
where
table2.code in
(
select t.Code
from (
select r.Code, rank() over(order by r.rate) as rn
from rates as r
) as t
where t.rn = 1
)
in SQL Server you can use top ... with ties syntax:
...
where
table2.code in
(
select top 1 with ties r.Code
from rates as r
order by r.rate
)
SELECT DISTINCT name
FROM table1 NATURAL JOIN table2
WHERE table2.code =
(SELECT CODE FROM RATE WHERE RATE=(SELECT MIN(RATE) FROM RATE))
Considering you are expecting only one record of minimum value.
try this,
WHERE table2.code = (SELECT Code FROM rates ORDER BY rate LIMIT 1)