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;
Related
How can I JOIN these 3 queries in to a single view
SELECT A,B,C FROM TABLE1
SELECT D,E,F FROM TABLE 2 WHERE G = 'TOM'
SELECT H,I,J FROM TABLE 2 WHERE G = 'HARRY'
OUTPUT TABLE/VIEW:
A,B,C,D,E,F,H,I,J
You can use cross join:
SELECT *
FROM (SELECT A,B,C FROM TABLE1) t1 CROSS JOIN
(SELECT D,E,F FROM TABLE 2 WHERE G = 'TOM') tom CROSS JOIN
(SELECT H,I,J FROM TABLE 2 WHERE G = 'HARRY') harry
This is what worked for me.
SELECT A,B,C from T1
LEFT JOIN
(SELECT D,E,F FROM T2 WHERE G ='TOM') ON T1.A = T2.D
LEFT JOIN
(SELECT H,I,J FROM T2 WHERE G = 'HARRY') as T3 ON T1.A = T3.H
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;
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;
lets say I have tableA which has a col1 and tableB which has col2 with the following content:
Table A
---------
A
B
C
D
Table B
---------
1
2
3
4
I want a select statement that returns to me:
A, 1
B, 2
C, 3
D, 4
I have tried making this call:
Select tableA.col1, tableB.col2 from tableA, tableB
but it returns the following:
A, 1
B, 1
C, 1
D, 1
A, 2
..
..
etc
how can i get it to just pull back this:
A, 1
B, 2
C, 3
D, 4
Try this:
select b.val1, b.val1, c.val2 from tableA a
inner join (select ROW_NUMBER() OVER () AS RowNumber, col1 as val1 from tableA) on a.col1 = b.val1
inner join (select ROW_NUMBER() OVER () AS RowNumber, col2 as val2 from tableB) c on c.RowNumber = b.RowNumber
I have following tables;
A B A B
_____ _____
1 t 7 a
2 r 5 d
3 e 3 e
4 f 9 a
5 d 10 c
6 s 11 a
7 a
And, output should be ;
A B
_____
1 t
2 r
4 f
6 s
9 a
10 c
11 a
In other words I want really different thing. I can only tell with this figure, take a look at. I want (A union B).
How can I do that ?
This query will do it. It loads up all the records from both tables, then displays all those that exist once
SELECT
A, B
FROM
(SELECT A, B FROM TABLE1
UNION ALL
SELECT A, B FROM TABLE2)
AS COMBINED
GROUP BY
A, B
HAVING
COUNT(*) = 1
ORDER BY A;
SELECT f.A, f.B
FROM firstTable f
LEFT JOIN secondTable s ON (f.A = s.A)
WHERE (s.A IS NULL)
UNION
SELECT s.A, s.B
FROM firstTable f
RIGHT JOIN secondTable s ON (f.A = s.A)
WHERE (f.A IS NULL)
SELECT iResult.*
FROM
(SELECT A, B
FROM tableA
WHERE A NOT IN
(SELECT Distinct A FROM tableB)
UNION
SELECT A, B
FROM tableB
WHERE A NOT IN
(SELECT Distinct A FROM tableB)) as iResult
SELECT A, B
FROM Table1
UNION
SELECT A, B
FROM Table2
EXCEPT
SELECT t1.A, t1.B
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.A = t2.A AND t1.B = t2.B
Edit Removed prior "solution" since I realised it's the same as the one proposed by Gary.
(select a, b from table_1 minus
select a, b from table_2) union
(select a, b from table_2 minus
select a, b from table_1);
This seemed to work with the followoing data on Oracle:
create table table_1 (
a number,
b varchar(2)
);
create table table_2 (
a number,
b varchar(2)
);
insert into table_1 values (1 ,'t');
insert into table_1 values (2 ,'r');
insert into table_1 values (3 ,'e');
insert into table_1 values (4 ,'f');
insert into table_1 values (5 ,'d');
insert into table_1 values (6 ,'s');
insert into table_1 values (7 ,'a');
insert into table_2 values (7 ,'a');
insert into table_2 values (5 ,'d');
insert into table_2 values (3 ,'e');
insert into table_2 values (9 ,'a');
insert into table_2 values (10 ,'c');
insert into table_2 values (11 ,'a');
Simply
SELECT a, b FROM table1 UNION SELECT a, b FROM table2;