Selecting the right column - sql

I have a query.
with result as
(
select t1.name, t1.number, t2.number from table1 t1, table2 t2 where some conditions
union all
select t1.name, t1.number, t3.number from table1 t1, table3 t3 where some conditions
)select * from result
I need to insert in table5 t1.name and t2.number
table5 has the same columns as t1.
If I do something like
insert in table5(name, number)
select r.name, r.number from result r
what would be considered r.number? t1.number or t2.number? Because columns have the same name. Or is there a way to defferentiate? How can I make it so the query skips every row with t3.number? Can I even do it?
For example I have table1
A (+1)11111111
B (+1)22222222
C (+1)33333333
table2
(+2)44444444
(+2)55555555
first select will get me
A (+1)11111111 (+2)44444444
B (+1)22222222 (+2)55555555
table3
(+3)66666666
(+3)88888888
(+3)97898789
result of second select
B (+1)22222222 (+3)88888888
C (+1)33333333 (+3)97898789
this will be the result of union all
A (+1)11111111 (+2)44444444
B (+1)22222222 (+2)55555555
B (+1)22222222 (+3)88888888
C (+1)33333333 (+3)97898789
what I want in the end is
A (+2)44444444
B (+2)55555555
the end result should not have this rows
B (+1)22222222 (+3)88888888
C (+1)33333333 (+3)97898789

Both. In some rows, t2.number is number and in others t3.number is number.
The result of the union all in a single result set. The result set doesn't know the origin of the values in any particular column (although you could include another column with this information).

Related

Ignore a column while doing MINUS in SQL query

I have written the query:
Select distinct a,b from t1 minus Select distinct a,b from t2.
Here t1 and t2 are two tables. I want distinct values of a and b that occur in t1 but not in t2. So I'm using minus operator. I want values of both a and b but I know that in some cases the value of b in t1 and t2 maybe different. This would result in values of a and b that are present in both t1 and t2 as minus would not happen if values of b do not match in both the tables. How can I do this successfully?
How can I get values of a and b that are present in table t1 but not in table t2 even though in some cases values of b might not match in both the tables?
table1: table2:
column1 column2 column1 column2
1 a 1 c
2 b 3 d
In this case I would want values (2,b) only. I would not want (1,a) as 1 is also present in table2.
Start with not exists:
select distinct. . .
from t1
where not exists (select 1 from t2 where t2.a = t1.a and t2.b = t1.b);
From you describe, you might want the comparison only on a:
select distinct a, b
from t1
where not exists (select 1 from t2 where t2.a = t1.a);
Another option is to use sub query in the WHERE condition as below-
SELECT A.*
FROM table1 A
WHERE A.column1 NOT IN
(SELECT DISTINCT column1 FROM table2)
You can also use LEFT JOIN as below which will provide you the same output as below-
SELECT A.*
FROM table1 A
LEFT JOIN table2 B ON A.column1 = B.column1
WHERE B.column1 IS NULL
For the data not include in t2, you can either go for the NOT EXISTS or LEFT OUTER JOIN.
Here is the solution.
Using NOT EXISTS
SELECT DISTINCT A,B FROM T1 WHERE NOT EXISTS (SELECT 1 FROM T2 WHERE T2.A = T1.A AND T2.B = T1.B);
Using Left Join
SELECT DISTINCT a,b,c FROM T1 LEFT JOIN T2 ON T1.a = T2.a and T1.b = T2.b WHERE T2.a IS NULL AND T2.b IS NULL
Hope it helps.

not in operator in SQL

there is 2 tables
table1
ID
1
2
4
6
7
TABLE2
2
4
6
i want those number from table1 which is not in table2 how i do this ?
i try this
select id from table1 t1
inner join table2 t2 on t1.id=t2.id
where t1.id not in (select id from table2)
but this is not working
SELECT t1.id
FROM table1 t1
LEFT JOIN table2 t2 ON t2.id = t1.id
WHERE t2.id IS NULL
Conceptually, we select all rows from table1 and for each row we attempt to find a row in table2 with the same value for the id column. If there is no such row, we just leave the table2 portion of our result empty for that row. Then we constrain our selection by picking only those rows in the result where the matching row does not exist. Finally, We ignore all fields from our result except for the id column (the one we are sure that exists, from table1).
try this:
select id from table1 t1 where t1.id not in (select t2.id from table2 t2)
You don't need to join the two tables in this case. You could just do
select id from table1 A where A.id not in (select B.id from table2 B);
You could also just simply use the sql set difference EXCEPT operator to achieve this
(select id from table1) except (select id from table2);
Use NOT IN or NOT EXISTS
select id from table1 t1
where t1.id not in (select id from table2)
select id from table1 t1
where not exists (select id from table2 where id = t1.id)

Concatenate values

I want to concatenate all pair options in my column.
I have an table:
ID num
1 10
2 20
3 30
and I want to create a query that will give me the following result:
10,20
10,30
20,10
20,30
30,10
30,20
Also i want second query that will return without multiples(10,20=20,10)
10,20
10,30
20,30
How can I get the 2 above in 2 different queries?
Thanks
For the first one I would do a cartesian product filtering the equal ones.
SELECT a.num, b.num
FROM tablename a, tablename b
WHERE a.num != b.num // Unless you want to exclude by ID
For the second, I would force one side to be bigger than the other.
SELECT a.num, b.num
FROM tablename a, tablename b
WHERE a.num < b.num
Let's say your table name is "mytable".
1st Query:
Select
t1.num,
t2.num
from mytable t1 join mytable t2 on
t1.ID!=t2.ID
2nd Query:
Select
t1.num,
t2.num
from mytable t1 join mytable t2 on
t1.ID<t2.ID
You have to use self join to get desired results. Use || to concatenate the columns. If the table name is t
Query for the first will be:
select t1.num||','|| t2.num
from t t1 join t t2
on t1.id!=t2.id
The second result can be obtained using below query:
Select
t1.num||','|| t2.num
from t t1 join t t2 on
t1.id<t2.id

SQL Select where two values not in the same row of another table

I need a select that return the values from T2.C from rows which combination of T2.A and T2.B doesn't exist in T1.
Something like:
select C from T2 where A,B not in (select A,B from T1)
Results from example must be:
C
--
y
z
Do you mean like
SELECT T2.*
FROM T2
LEFT OUTER JOIN T1
ON T2.ColumnA = T1.ColumnA
AND T2.ColumnB = T1.ColumnB
WHERE T1.ColumnA IS NULL;
If you do an outer join, you can easily notice a lack of a match (via nulls).

query in sql server 2005

there are two tables ...know i need
1st condition:
all the records in table
2 nd condition:
In table2 i need only records which have data
...i want one query for the aove two conditions...
SELECT
*
FROM Table1 t1
INNER JOIN Table2 t2 on t1.PK = t2.FK
This will return all rows in table1 that have at least one corresponding row in table2
But if you want all rows from t1 no matter what then this may be what you want
SELECT
*
FROM Table1 t1
LEFT JOIN Table2 t2 on t1.PK = t2.FK
Finally, As I dont know the structure in place perhaps table1 and table2 have similar structures. If this is true perhaps you may want a union of the two
SELECT
*
FROM Table1 t1
UNION ALL
SELECT
*
FROM Table2 t2