merging two tables but removing duplicates in in a certain column - sql

so I am trying to merge two tables for example
table 1:
name
col1
col2
David
W
C
BOB
v
w
table 2:
name
col1
col2
David
o
n
Kevin
l
x
Im trying to merge them such as if I have a duplicate name in table 1 and table 2, ill keep only the one in table 1
so table 3 would be like this
table 3:
left
center
right
David
W
C
Bob
v
w
Kevin
l
x
if I use union or union all, it creates 2 rows of David but I need only one of them

I think you want a full join:
select coalesce(t1.name, t2.name), t1.center, t2.right
from table_1 t1 full join
table_2 t2
on t1.name = t2.name;

I'm not clear what your goal is, if a match is found in col1/col2 in both tables. but try this:
SELECT
table1.name
,table1.col1
,table1.col2
,table2.col1
,table2.col2
FROM table_1 AS table1
FULL JOIN table_2 AS table2
ON table1.name = table2.name
This will join table_1 and table_2 on the name column
It will return null values in table1.col1/col2 and table2.col1/col2 if a match is not found

Related

Delete a record from a table referring to 2 or more columns from another table

Consider having 2 tables as follows.
Table 1:
Unit
SKU number
Active
A
1
Y
B
2
Y
c
3
Y
Table 2:
Unit
SKU number
description
X
4
Apple
B
2
Mango
Y
5
Grapes
z
6
Banana
I wanted to delete record B,2,Y from table 1 by referring to table 2 where values in columns Unit and SKU number match.
I tried using the following query but it didn't seem to work
DELETE FROM table1
WHERE (Unit, SKU_Number) IN (SELECT Unit, SKU_Number FROM table2);
The error was
An expression of non-boolean type specified in a context where a condition is expected, near ','
Can someone please help me understand what I am doing wrong here or help me rewrite the SQL query to achieve the required objective?
You could use similar logic with exists:
DELETE
FROM table1 t1
WHERE EXISTS (SELECT 1 FROM table2 t2
WHERE t2.Unit = t1.Unit AND t2.SKU_Number = t1.SKU_Number);
You can try using this query, assuming Unit of Table 1 is unique:
DELETE FROM table1
WHERE table1.Unit IN (
SELECT table1.Unit
FROM table1
LEFT JOIN table2 ON table1.Unit = table2.Unit
AND table1.SKU_Number = table2.SKU_Number
)
If unit is not an unique field, simply replace it with whichever field is unique, or with primary key of Table 1.
You can use inner join for delete:
DELETE t1
FROM table1 t1
INNER JOIN table2 t2
ON t1.unit=t2.unit and t1.SKU_Number = t2.SKU.Number

Select data from multiple table

I have the following:
Table1 IdTable1, IdTable2, IdTable3
Table2 IdTable2, Title
Table3 IdTable3, FName, LName
I Need the content of Table1 with the Title, FName, LName
I have tried
SELECT T2.Title, T3.FName, T3.LName
FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.IdTable2 = T2.IdTable2
LEFT JOIN Table3 T3 ON T1.IdTable3 = T3.IdTable3
All what I get is the Table2 with the other columns NULL or the 2 columns of Table3 with the first column NULL.
Table 1 Contains
1 2 1
2 2 1
3 2 5
Table 2 Contains
1 Mr
2 Madame
Table3 Contains
1 A B
2 C D
3 E F
4 G H
5 I J
The Results of all my Queries are
Madame Null Null
Madame Null Null
Madame Null Null
or
NULL A B
NULL A B
NULL I J
Your SQL is fine as long as you want to return partial results.
Have you verified the actual data is correct? You are using a LEFT JOIN. This means rows in T1 will be returned even if there is no matching data in T2 or T3. If there are no matching rows in one of those tables, the columns in that table will be NULL.
If you include all of the columns in T1 in your result set, you will see that there is indeed data in T1, but there is no matching data in T2 or T3. In cases where T3 columns are NULL, that means there is data in T1 and T2 but not T3.
It looks to me like you've got a referential integrity problem. You may need to add foreign keys with drop constraints or fix some application logic problems when creating or deleting records.
I guess your Table1 is a pivot table for Table2 and Table3. I will do something like below.
SELECT T2.Title, T3.FName, T3.LName
FROM Table1 T1, Table2 T2, Table3 T3 WHERE T2.IdTable2 = T1.IdTable2 and T3.IdTable3 = T1.IdTable3

How to replace fetched values with another column while querying with SQL Server 2008 R2

Alright let me explain my question with example
We have 1 table
This table contains
Id
Name
Number
Now example
1 House 4
2 Hospital 3
3 Airport 1
4 Station 2
Now when fetching as select * from table
I want to replace third column number values with that number representing Name
So example of fetching
1 House Station
2 Hospital Airport
3 Airport House
4 Station Hospital
How can i do this ? thank you
select t1.id,
t1.name,
t2.name as name2
from your_table t1
left join your_table t2 on t1.number = t2.id
You can join the same table twice to replace the number with the name. The on contidion in the join matches the table again and then you can select the name from that table (t2)
SQLFiddle Example
You can do this with an explicit join:
select t.id, t.name, t2.name as otherName
from t left outer join
t t2
on t2.number = t.id

sql simple 3 table join using same table?

I have two tables:
Table 1
id, name1
Table 2
id, name2a, name2b
Table 2's column names name2a, and name2b are references to table 1's id. I need to create a query that pulls both the names out of table 1 based on the id's used in Table 2.
Therefore, if Table one contained:
1 Peter
2 Paul
And Table 2 contained:
1 1 2
2 2 2
Then a select statement should give me:
Peter Paul
Paul Paul
I've gone around the bend trying to build this SQL and the best I came up with was:
SELECT table1.name AS 'name', table1.name AS 'Other name'
FROM table1, table2
WHERE table1.id = table2.name2a
Which only gives me the name2a column correctly.
Any help appreciated! I guess I need to do a join, but I'm really struggling...
Start with your 2nd table and join TWICE to table 1 (different aliases respectively), then get the name field from each aliased Table1 entry.
select
T2.ID,
TJ1.Name1 as FirstName,
TJ2.Name1 as SecondName
from
Table2 t2
join Table1 TJ1
on t2.Name2a = TJ1.ID
join Table1 TJ2
on t2.Name2b = TJ2.ID
select foo.*, t1.x, t2.y
join t1 on t1.id = foo.a
join t1 as t2 on t2.id = foo.b
If there's a chance that col a or col b is null, use a left join.
Have you tried using an INNER JOIN?
SELECT table1.name AS 'name', table1.name AS 'Other name'
FROM table1 INNER JOIN table2 ON table1.id = table2.name2a;
Sorry if I'm no help, not that great at SQL myself hehe.
Your problem is that you need to reference table1 twice: once for the plain table1.name and again to look up what table2 is pointing at. You can join one table in multiple times if you give them aliases:
SELECT t1.name1, o.name1
FROM table1 t1
JOIN table2 t2 ON t1.id = t2.name2a
JOIN table1 o ON t2.name2b = o.id -- And JOIN back to table1 to get the name1

How to write a query to get only first matching row while joining two tables?

Consider I have following tables:
Table 1:
AId
AMoniker
Table2:
BId
BMoniker
Table1->Table2 is one to many relationship
I want a temp table out of these two tables where if a particular Amoniker has multiple BMonikers then only first one should go in the table.
For example, if tables have following data:
Table1:
1 ABCD
2 DEFG
3 QWER
Table 2:
1 QZ
1 XC
1 CV
2 DE
2 OP
3 QW
the query should return the following:
ABCD QZ
DEFG DE
QWER QW
My query to get all the rows is:
select b.BMoniker, a.AMoniker
into #moniker_map
from Table1 a inner join Table2 b
on a.Aid=b.Bid
How can i modify this to get only 1st row from Table2 for each Id.
I tried following query:
select b.BMoniker, a.AMoniker
from Table1 a inner join Table2 b
on a.Aid=b.Bid
and b.BMoniker in
(
select top 1 BMoniker
from Table2
where Bid=cb.Bid
ORDER BY BMoniker
)
But i get following error:
Incorrect syntax near keyword 'top'
Sybase error code = 156, SQLState="ZZZZZ"
Selects row with minimum value from table2 (PostgreSQL syntax):
SELECT a.AMoniker, MIN(b.BMoniker) FROM Table1 a, Table2 b
WHERE a.Aid = b.Bid GROUP BY 1;
Maybe it is a typo but it looks like your subquery is referencing an table alias cb which is not defined. Can you try this:
select b.BMoniker, a.AMoniker
from Table1 a inner join Table2 b
on a.Aid=b.Bid
and b.BMoniker in
(
select top 1 BMoniker
from Table2 c
where c.Bid=a.Bid
ORDER BY BMoniker
)