WITH Clause syntax error getting in Teradata - sql

I am attempting to join multiple tables/views inside with clause but it failing. Please help me.
with a as (
select
*
from
db.a
),
b as (
select
*
from
db.b
),
c as (
select
*
from
b
where
col = 'Hi'
),
d as (
SELECT
*
FROM
c P
WHERE
col = 'Y'
),
e as (
SELECT
*
FROM
b P
LEFT JOIN (
SELECT
*
FROM
a
WHERE
col= 'Y'
) M ON p.col1= M.col1
WHERE
P.Date > current_date - 5
)
select
*
from
e
Getting Error any thing wrong in it:
Teradata with clause help
Teradata with clause help
Teradata with clause help
Teradata with clause help
Teradata with clause help

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

Cross Apply Equivalent in Hive?

I want to create the effect of a cross apply in AWS EMR Hive. I've got a little sample code here that runs in SQL Server 2017.
with r as (
select 1 as d
union all
select 2 as d
)
select * from r
cross apply (select 'f' as u) e;
How can I run the equivalent of this in EMR Hive?
I've checked out the Lateral View documentation, but it all references explode, and I don't have an array.
Instead of do CROSS APPLY you may do CROSS JOIN in your case. F.e.:
SET hive.strict.checks.cartesian.product = false;
WITH r AS (
SELECT 1 AS d
UNION ALL
SELECT 2 AS d
)
SELECT *
FROM r
CROSS JOIN (SELECT 'f' AS u) e;
I ended up working around with by just adding an extra field with a single value and joining the two tables together on that to produce the same effect.
It ended up looking something like:
with d as (
select column, 'AreYouKiddingMe' as k from table
), e as (
select column2, 'AreYouKiddingMe' as k from table2
)
select * from d inner join e on d.k = e.k

selecting incremental data from multiple tables in Hive

I have five tables(A,B,C,D,E) in Hive database and I have to union the data from these tables based on logic over column "id".
The condition is :
Select * from A
UNION
select * from B (except ids not in A)
UNION
select * from C (except ids not in A and B)
UNION
select * from D(except ids not in A,B and C)
UNION
select * from E(except ids not in A,B,C and D)
Have to insert this data into final table.
One way is to create a the target table (target)and append it with data for each UNION stage and then using this table for joining with the other UNION stage.
This would be the part of my .hql file :
insert into target
(select * from A
UNION
select B.* from
A
RIGHT OUTER JOIN B
on A.id=B.id
where ISNULL(A.id));
INSERT INTO target
select C.* from
target
RIGHT outer JOIN C
ON target.id=C.id
where ISNULL(target.id);
INSERT INTO target
select D.* from
target
RIGHT OUTER JOIN D
ON target.id=D.id
where ISNULL(target.id);
INSERT INTO target
select E.* from
target
RIGHT OUTER JOIN E
ON target.id=E.id
where ISNULL(target.id);
Is there a better to make this happen ? I assume we anyway have to do the
multiple joins/lookups .I am looking forward for best approach to achieve this
in
1) Hive with Tez
2) Spark-sql
Many Thanks in advance
If id is unique within each table, then row_number can be used instead of rank.
select *
from (select *
,rank () over
(
partition by id
order by src
) as rnk
from (
select 1 as src,* from a
union all select 2 as src,* from b
union all select 3 as src,* from c
union all select 4 as src,* from d
union all select 5 as src,* from e
) t
) t
where rnk = 1
;
I think I would try to do this as:
with ids as (
select id, min(which) as which
from (select id, 1 as which from a union all
select id, 2 as which from b union all
select id, 3 as which from c union all
select id, 4 as which from d union all
select id, 5 as which from e
) x
)
select a.*
from a join ids on a.id = ids.id and ids.which = 1
union all
select b.*
from b join ids on b.id = ids.id and ids.which = 2
union all
select c.*
from c join ids on c.id = ids.id and ids.which = 3
union all
select d.*
from d join ids on d.id = ids.id and ids.which = 4
union all
select e.*
from e join ids on e.id = ids.id and ids.which = 5;

MAX function not working on 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

query optimization with condition

I want to optimze the follwing query, to not use subquery to get max value:
select c.ida2a2 from table1 m, table2 c
where c.ida3a5 = m.ida2a2
and (c.createstampa2 < (select max(cc.createstampa2)
from table2 cc where cc.ida3a5 = c.ida3a5));
Any idea? Please let me know if you want to get more info.
This may be a more efficient way to write the query:
select c.ida2a2
from table1 m join
(select c.*, MAX(createstampa2) over (partition by ida3a5) as maxcs
from table2 c
) c
on c.ida3a5 = m.ida2a2
where c.createstampa2 < maxcs
I'm pretty sure Oracle optimizes this correctly (filtering the rows before the join). If you wanted to be clearer:
select c.ida2a2
from table1 m join
(select c.*
from (select c.*, MAX(createstampa2) over (partition by ida3a5) as maxcs
from table2 c
) c
where c.createstamp2 < c.maxcs
) c
on c.ida3a5 = m.ida2a2