select columns matching - sql

Newbie question. I want to do something like:
SELECT c1,c2,c3 FROM TABLE t1
UNION
SELECT c1,c2,c3 FROM TABLE t2 WHERE t1.c1 IS NOT NULL AND t1.c2 IS NULL;
so if I have t1:
c1|c2|c3
1 | a|v1
2 | b|v2
and t2:
c1|c2|c3
1 | a|v3
2 | b|v4
2 | c|v5
3 | d|v6
I would get:
c1|c2|c3
1 | a|v1
2 | b|v2
2 | c|v5
Anybody knows how to do this?

According to SQL Docs, JOIN is faster that SubQueries. try this:
SELECT c1, c2, c3
FROM t1
UNION
SELECT b.c1, b.c2, b.c3
FROM t1 a inner join t2 b ON
(a.c1 = b.c1) and (a.c1 = b.c1)
WHERE (b.c1,b.c2) NOT IN (SELECT c1,c2 FROM t1)
to prove, see here: http://sqlfiddle.com/#!2/50a4e/23

The query should look like something like below.However,I am not sure how efficient it is.
SELECT
T1.c1,
T1.c2,
T1.c3
FROM T1
UNION
SELECT
T2.c1,
T2.c2,
T2.c3
FROM T2
WHERE ((T2.c1,T2.c2) NOT IN (SELECT t1.c1,t1.c2 FROM t1)) AND
(T2.c1 IN (SELECT t1.c1 FROM t1))

select t2.c1,t2.c2,t2.c3
from t1,t2
where t1.c1=t2.c2;

Related

How to stop the multiplying sum of same value reptedly without entering any values in that column in SQL using full join and group by

I have two tables T1 & T2 and two same columns in both tables C1 & C2 I am doing the subtraction of sum of two columns using full join and group by but when I adding the value in T1.C2 or T2.C2 the other columns value is multiplying automatically. I want to stop this multiplication please help me
My query is
Select T1.C1,(Sum(T1.C2)-Sum(T2.C2))
from T1
Full join T2 on T1.C1=T2.C1
group by T1.C1;
When I entering the value 1000 in T1.C2 & 100 in T2.C2 the subtraction is happening right my Output is
___________
|C1 C2. |
| |
|A 900 |
| |
But When I entering 1000 again in C1.C2 the the output came wrong
____________
|C1 C2. |
| |
|A 1800 |
| |
Expected output is 1900
This will do:
select T1.C1,
sum(T1.C2) - (Select isnull(sum(T2.C2), 0) From T2 where T1.C1 = T2.C1)
From T1
group by T1.C1
Output:
A 1900
I think this will do what you're looking for
select
(select sum(t1.C1) from (values (1000), (1000)) T1(C1))-
(select sum(t2.C1) from (values (100)) T2(C1));
Results
1900
If you want to allow unmatched rows in both tables, then a full join is the way to go. I would recommend pre-aggregating in subqueries first to avoid multiplying the rows:
select
coalesce(t1.c1, t2.c1) c1,
coalesce(t1.c2, 0) -
coalesce(t2.c1, 0) c2
from (select c1, sum(c2) sum_c2 from t1) t1
full join (select c1, sum(c2) sum_c2 from t2) on t1.c1 = t2.c1
group by coalesce(t1.c1, t2.c1)
If you want rows from both tables, then use an inner join, or a left join if you want all rows from one table, and all matched rows from the other one:
select
t1.c1,
t1.c2 - coalesce(t2.c1, 0) c2
from (select c1, sum(c2) sum_c2 from t1) t1
left join (select c1, sum(c2) sum_c2 from t2) on t1.c1 = t2.c1
group by t1.c1

How to find the same elements in tow arrays of two different tables in HIVE?

I have two tables like the follows:
table1:
id sid
1 | ['101', '102', '103']
2 | ['102', '101', '103']
3 | ['103', '101', '102']
table2:
id | sid
1 | ['101', '102', '103']
3 | ['102', '103']
and I wish to get the following table:
id sid
1 | ['101', '102', '103']
2 | ['102', '101', '103']
3 | ['103', '102']
Explanation: I wish to select the same elements in table1.sid and table2.sid with the same order in table1. Besides, if the id in table1 doesn't exist in table2, then keep the sid as it is in table1. What should I do?
You can use posexplode() to basically do what you want. Of course, all this array stuff is more complex in Hive than in other databases, particularly getting the results in the order you want:
select t1.id, collect_list(sid2)
from (select t1.id, t2.sid2, t1.pos1
from (table1 t1 lateral view
posexplode(t1.sid) as pos1, sid1
) left join
(table2 t2 lateral view
posexplode(t2.sid) as pos2, sid2
)
on t2.id = t1.id and t2.sid2 = t1.sid1
distribute by t1.id
order by t1.id, t1.pos1
) t
Something like this:
with t as (
select t1.id, collect_list(sid2) as sid
from (select t1.id, t2.sid2, t1.pos1
from (table1 t1 lateral view
posexplode(t1.sid) as pos1, sid1
) left join
(table2 t2 lateral view
posexplode(t2.sid) as pos2, sid2
)
on t2.id = t1.id and t2.sid2 = t1.sid1
distribute by t1.id
order by t1.id, t1.pos1
) t
select t1.id,
(case when size(t.sid) = 0 then t1.sid else t.sid end)
from t1 left join
t
on t1.id = t.id

How to make this Sql Consult

I have two tables:
T1
C1 C2 (Columns)
A 1
B 2
T2
C1 C2 (Columns)
A Null
C Null
what I expect as result is:
C1 C2 (Columns)
A 1
B 2
C NULL
please help, I try to find the most clean solution, thanks.
I do this:
Select case when T1.C1 is Null then T2.C1 else T1.C1 end As C1,
T1.C2
from T1 FULL OUTER JOIN T2 on T1.C1 = T2.C1

MSSQL join with itself

I have two tables:
id
1
2
3
4
t1 AND t2
id | related_id
1 | 2
1 | 3
Where t2 is relationship table between t1 records. What is the best way to get desired output?
t1.id | t1_copy.id
1 | NULL -- want to get this NULL row
1 | 2
1 | 3
Simple JOIN would almost work, however it doesn't give me the first NULL row.
SELECT t1.id, t1_copy.id FROM t1
LEFT JOIN t2 ON t1.id = t2.id
LEFT JOIN t1 t1_copy ON t1_copy.id = t2.related_id
WHERE t1.id = 1
P.S: Yes, I do realize that desired output is wacky.
Seems like a simple UNION should do the trick
SELECT
id,
null as copy_id
FROM
t1
WHERE
exists (select * from t2 where t1.id = t2.id)
UNION ALL
SELECT
t1.id,
t2.related_id
FROM
t1
INNER JOIN t2
ON t1.id = t2.id
SQL Fiddle
SELECT DISTINCT t1.id, t1_copy.id FROM t1
LEFT OUTER JOIN t2 ON t1.id = t2.id
WHERE t1.id = 1

MySQL: Union of a Left Join with a Right Join

Say we have the following tables t1 and t2:
t1:
id | column_1
-------------
1 | 1
2 | 2
t2:
id | column_2
-------------
2 | 2
3 | 3
and we want to find the following result:
id | column_1 | column_2
------------------------
1 | 1 |
2 | 2 | 2
3 | | 3
This basically is the union of a right join with a left join. The following code works but feels clumsy:
(
SELECT t1.id, t1.column_1, t2.column_2
FROM t1
LEFT JOIN t2 ON t1.id = t2.id
)
UNION
(
SELECT t2.id, t1.column_1, t2.column_2
FROM t1
RIGHT JOIN t2 ON t1.id = t2.id
)
Is there a better way to achieve this?
select a.id, t1.column_1, t2.column_2
from (
select id from t1
union
select id from t2
) a
left outer join t1 on a.id = t1.id
left outer join t2 on a.id = t2.id
Try this one:
SELECT t1.id, t1.column_1, t2.column_2
FROM t1
FULL OUTER JOIN t2 ON (t1.id = t2.id)
Edit: Doesn't work, MySQL does not know FULL OUTER JOIN.
Have a look here:
http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/
Haven't tried this myself, but this might work:
SELECT t1.id, t1.column_1, t2.column_2, t2a.column_2
FROM t1
LEFT JOIN t2 ON t1.id = t2.id
RIGHT JOIN t2 AS t2a ON t1.id = t2a.id