MAX function not working on HIVE - hive

My SQL query:
select * from cds2 where tw=(select max(tw) from cds2) limit 10;
I am new to Hive and I wrote up this Hive query:
Select * from cds2 join (select max(tw) twmax from cds2) V on (1 = 1)
group by id,filename,tw;
The above query doesn't work.I would appreciate any help.Thanks

select * from (select max(f1) as maxf1 from t_ext_1_bkdoubledelete) t1 left outer join (select * from t_ext_1_bkdoubledelete) t2 on t1.maxf1=t2.f1;
What you do i

Related

How to convert Oracle SQL query into datalake(Impala)?

I have one Oracle SQL query in the below format how I can proceed to convert this into Impala:-
SELECT
Some_Columns
FROM
(
SELECT * FROM
(
SELECT
Some_more_columns
FROM
Main_table
LEFT JOIN t1 on codn_1 = cond_2
....
....
Some more left joins
.....
WHERE
some_filters
)
PIVOT(
count(*) for column_x in (some_column)
)
) T
ORDER BY 2,3

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';

How can i increase number of rows in sql table

This is my table structure
id name
1 a
2 b
3 c
4 d
how can i get number rows=16 if number of records=M then resultant should be M^2
i tried this query to get result
select * from t
union
select * from t
union
select * from t
union
select * from t
You can do it by cross Apply
Select A.* from Table1 a cross apply Table1
Sql Fiddle Demo
select * from table t1 cross join table t2
DEMO

Simple SQL Query problem: how to only "select" items from table_A which are also in table_B

This is the query I have been using in sqlite / python:
Select wbCode from CountriesList_A
but I would like to have some sort of IF statement to only return me the items which are ALSO in CountriesListB.
Suggestions? Thank you very much.
You can just join to the other table using an inner join, which only returns rows that are present in both tables.
SELECT a.wbCode
FROM CountriesList_A a
INNER JOIN CountriesList_B b ON a.wbCode = b.wbCode
You can do this:
select wbCode
from CountriesList_A
where wbCode in (select wbCode from CountriesListB)
TRY:
Select * From tableA A
Where Exists
(Select * From Table B
Where wbCode = A.wbCode )
Select a.wbCode from CountriesList_A a
WHERE EXISTS (
select b.wbCode
FROM CountriesListB b
WHERE a.wbCode = b.wbCode)
Or
Select a.wbCode from CountriesList_A a
WHERE a.wbCode IN (
select b.wbCode
FROM CountriesListB b)
Or
Select a.wbCode
from CountriesList_A a inner join CountriesListB b
on a.wbCode = b.wbCode

Self-join of a subquery

I was wondering, is it possible to join the result of a query with itself, using PostgreSQL?
You can do so with WITH:
WITH subquery AS(
SELECT * FROM TheTable
)
SELECT *
FROM subquery q1
JOIN subquery q2 on ...
Or by creating a VIEW that contains the query, and joining on that:
SELECT *
FROM TheView v1
JOIN TheView v2 on ...
Or the brute force approach: type the subquery twice:
SELECT *
FROM (
SELECT * FROM TheTable
) sub1
LEFT JOIN (
SELECT * FROM TheTable
) sub2 ON ...
Do you mean, the result of a query on a table, to that same table. If so, then Yes, it's possible... e.g.
--Bit of a contrived example but...
SELECT *
FROM Table
INNER JOIN
(
SELECT
UserID, Max(Login) as LastLogin
FROM
Table
WHERE
UserGroup = 'SomeGroup'
GROUP BY
UserID
) foo
ON Table.UserID = Foo.UserID AND Table.Login = Foo.LastLogin
Yes, just alias the queries:
SELECT *
FROM (
SELECT *
FROM table
) t1
JOIN (
SELECT *
FROM table
) t2
ON t1.column < t2.other_column