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
Related
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
UPDATE
t1
SET
t1.c3 = t2.c3,
t1.c4 = t1.c4
FROM
t1
LEFT JOIN t2 ON t1.c1 = t2.c1 AND t1.c2 = t2.c2
WHERE
t1.c5 = 'In Progrss'
I want to update value from top row of table t2.
For example in t2 table having 3 rows with criteria match above only top row value update in t1 table(ROW ID 3 VALUES TO UPDATE IN t1 table).
t2 table:
id c1 c2 c3 c4
-----------------------------
1 ABC XYZ 280 300
2 ABC XYZ 290 400
3 ABC XYZ 310 500
4 PQR STR 210 400
t1 table:
id c1 c2 c3 c4 c5
----------------------------------
1 ABC XYZ In Progrss
5 ABC XYZ In Progrss
8 ABC XYZ In Progrss
15 PQR STR IN Progress
Postgres’ update/join syntax goes like:
update t1
set c3 = t2.c3, c4 = t1.c4
from t2
where
t1.c1 = t2.c1
and t1.c2 = t2.c2
and t1.c5 = 'In Progress'
If there are several matching rows in t2 and you want the one with the smallest id, then you can use row_number() in a subquery:
update t1
set c3 = t2.c3, c4 = t1.c4
from (select t2.*, row_number() over(partition by c1, c2 order by id) rn from t2) t2
where
t1.c1 = t2.c1
and t1.c2 = t2.c2
and t1.c5 = 'In Progress'
and t2.rn = 1
We have encountered a weird problem, which Hive returned incorrect result while use same alias name in subquery.
Following 3 SQL will return "A, C":
SELECT * FROM
(
SELECT T1.C1, T2.C1 C2
FROM (SELECT 'A' C1) T1
LEFT JOIN (SELECT 'C' C1) T2 ON 1 = 1
WHERE T1.C1 = 'C') T1
SELECT * FROM (SELECT T1.C1, T2.C1 C2 FROM (SELECT 'A' C1) T1 LEFT JOIN (SELECT 'C' C1) T2 ON 1 = 1 WHERE T1.C1 = 'C') T2
SELECT * FROM (SELECT T1.C1, T2.C1 C2 FROM (SELECT 'A' C1) T1 LEFT JOIN (SELECT 'C' C1) T2 ON 1 = 1 WHERE T2.C1 = 'C') T1
Following 1 SQL will return "C, C":
SELECT * FROM (SELECT T1.C1, T2.C1 C2 FROM (SELECT 'A' C1) T1 LEFT JOIN (SELECT 'C' C1) T2 ON 1 = 1 WHERE T2.C1 = 'C') T2
I have two tables. Table T1 with the following columns and rows:
#A B C D
-----------------
P1 01 C1 1
P1 02 C2 2
P2 01 C3 1
P2 02 C4 3
Table T2 with the same columns as T1 but with some differences in the data
#A B C D
---------------
P1 01 C1 1
P1 02 C9 8
P1 03 C5 1
P2 01 C6 2
P2 05 C8 4
Columns A & B together form the primary key.
I want to compare the two tables by keeping the column A as the reference column between the two tables. In my output I want to see the difference between the two tables.
#A B C D B C D T1-vs-T2
---------------------------------------------
P1 01 C1 1 01 C1 1 Match
P1 02 C2 2 02 C9 8 No Match
P1 -- -- - 03 C5 1 Not in T1
P2 01 C3 3 01 C6 2 No Match
P2 02 C4 3 -- -- - Not in T2
P2 -- -- - 05 C8 4 Not in T1
You are looking for a full outer join. Access does not directly support a full outer join operator, but we can simulate it using a union query.
SELECT
t1.A AS A,
t1.B AS B,
t1.C AS t1_C,
t1.D AS t1_D,
t2.C AS t2_C,
t2.D AS t2_D,
IIF(t1.C = t2.C AND t1.D = t2.D, 'Match',
IIF(t1.A IS NOT NULL AND t2.A IS NOT NULL, 'No Match',
'Not in T2')) AS T1_vs_T2
FROM Table1 t1
LEFT JOIN Table2 t2
ON t1.A = t2.A AND t1.B = t2.B
UNION ALL
SELECT
t2.A,
t2.B,
t1.C,
t1.D,
t2.C,
t2.D,
'Not in T1'
FROM Table1 t1
RIGHT JOIN Table2 t2
ON t1.A = t2.A AND t1.B = t2.B
WHERE
t1.A IS NULL;
I think a better way to implement the logic is to start with all ids and just use left join:
select ab.a, ab.b, t1.c, t1.d, t2.c, t2.d,
switch(t1.a is null, 'Not in t1',
t1.b is null, 'Not in t2',
t1.c = t2.c and t1.d = t2.d, 'Match',
1=1, 'No Match'
) as t1_vs_t2
from ((select a, b from t1
union -- on purpose to remove dups
select a, b from t2
) ab left join
t1
on t1.a = ab.a and t1.b = ab.b
) left join
t2
on t2.a = ab.a and t2.b = ab.b;
I prefer this because the logic for the comparison is all in one place.
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;