Sum col2 of two tables based on duplicate matching col1 - hive

I have 2 tables both structured as (id, views)
Table 1:
id views
A 1
B 2
B 3
C 3
C 4
D 4
Table 2:
id views
C 1
D 3
D 4
E 5
E 7
F 8
I'm looking to sum views of ids that are both in table 1 and 2 (id C and D) in this case so the output would be:
Table 3:
id views
C 8
D 11

You could use the following query in your case :
select a.id,sum(a.views) from ( select * from table1 union table2 ) as a group by id;

select id,sum(views) from (select * from table1 union all select * from table2)a where a.id="C" or a.id="D" group by id;

Related

Recursive query to retrieve all child from given id

I am migrating from oracle to postgres and I don't know how to make a recursive query
Here is an example data set:
id
id_parent
name
1
0
aa
2
0
aa
3
1
aa
4
3
aa
5
3
aa
6
2
aa
7
6
aa
For id = 3, I want to get
id
3
4
5
for id =1
id
1
3
4
5
with this I have all but I don't know how to filter by id:
SELECT id FROM (
with recursive cat as (
select * from table
union all
select table.*
from table
join cat on cat.id_parent = table.id
)
select * from cat order by id
)
as listado where id != '0' group by id
There is no need of using sub-query. You just need to fix your recursive query only as -
WITH RECURSIVE cat (id, id_parent) as
(
SELECT id, id_parent
FROM table
WHERE id = 1
UNION ALL
SELECT d.id, d.id_parent
FROM table d
JOIN cat ON cat.id = d.id_parent
)
SELECT id
FROM cat
ORDER BY id;
Demo.

Count distinct by link on sql?

I'm trying to count distinct by the link between two columns.
Here is the example.
rownum
type
id
1
A
a
2
A
b
3
B
b
4
B
c
5
C
c
6
C
d
If I count distinct by type column, it returns 3. However, what I'd like to do is to consider rownum 2 and 3, 4 and 5 are not distinctive because they got the same value on id column.
To rephrase,
type
array of id
A
a, b
B
b, c
C
c, d
Since A and B got same b, and B and C got same c on their arrays, it would return 1 as a result.
I have no idea where to start. Would appreciate if I can get any hint or something.
Consider below:
you might use STRING_AGG
WITH TMP_TBL AS
(
SELECT 1 AS ROWNUM, 'A' AS TYPE, 'a' AS ID UNION ALL
SELECT 2,'A','b' UNION ALL
SELECT 3,'B','b' UNION ALL
SELECT 4,'B','b' UNION ALL
SELECT 5,'C','c' UNION ALL
SELECT 6,'C','d'
);
SELECT DISTINCT TYPE,N_ID
FROM
(
SELECT TYPE,STRING_AGG(ID)OVER(PARTITION BY TYPE) AS N_ID FROM TMP_TBL
)

fetch record as in order passed for IN condition in oracle

I want to fetch the records in order passed for IN condition.
select * from table where id in(6,3,7,1);
is returning the rows as
id name
1 abc
3 xy
6 ab
7 ac
but I want to display the records in same orders as ids passed in condition in Oracle
id name
6 ab
3 xy
7 ac
1 abc
Please help me in fetching the records in same order as in condition ids in oracle. The values in IN condition may change dynamically.
You can do this with a case statement in the order by clause or using a join.
select *
from table
where id in(6,3,7,1)
order by (case id when 6 then 1 when 3 then 2 when 7 then 3 when 1 then 4 end);
Or:
with ids as (
select 6 as id, 1 as ordering from dual union all
select 3 as id, 2 as ordering from dual union all
select 7 as id, 3 as ordering from dual union all
select 1 as id, 4 as ordering from dual
)
select *
from table t join
ids
on t.ids = ids.id
order by ids.ordering;
Note that you don't need the in in this case, because the join does the filtering.
you can use trick
select * from table where id in(6,3,7,1) order by case when id = 6 then 1
id = 3 then 2
id = 7 then 3
id = 1 then 4
end

sql oracle Union with null values

I have a query on 2 views which when it unions
View X
A B C
2 3
View Y
A B C
3 4
this is my query from the 2 views
select * from view X
UNION
select * from view Y;
the result I had:
A B C
2 3
3 4
the result I wanted (the 2 overrides the null value in view Y # attribute A):
A B C
2 3
2 3 4
how can i obtained that ?
Try this
select nvl(A, lag(A) over (order by rownum)), B, C from (
select A, B, C from X
union
select A, B, C from Y
)
sqlfiddle
here is one answer in oracle
select * from
(select x from tbl1
union
select x from tbl2) t1
,
(select y from tbl1
union
select y from tbl2) t2
,
(select z from tbl1
union
select z from tbl2) t3
where t1.x is not null
order by t1.x desc nulls first;

taking difference of tables

I have following tables;
A B A B
_____ _____
1 t 7 a
2 r 5 d
3 e 3 e
4 f
5 d
6 s
7 a
And, output should be ;
A B
_____
1 t
2 r
4 f
6 s
In other words I want difference of these two tables . I want region A in this figure.
How can I do that ?
Try this:
SELECT t1.*
FROM t1
LEFT JOIN t2 USING (A, B)
WHERE t2.A IS NULL
without using JOIN
SELECT A, B
FROM tableA
WHERE A NOT IN
(SELECT Distinct A FROM tableB)
This should do the trick:
SELECT
A,
B
FROM Table_1
WHERE NOT EXISTS
(
SELECT Table_2.A AS Test
FROM Table_2
INNER JOIN Table_1 AS T ON Table_2.A = Table_1.A
)
SELECT A, B
FROM Table1
EXCEPT
SELECT A, B
FROM Table2;