Hi I have 2 tables below:
table1
GroupName PersonName email phone
A Tom tom# 123
A Jen jen# 223
B Kim kim# 232
table2
GroupName PersonName email phone
A Tom NULL 1
A Jen NULL 1
A Ken NULL 1
B Kim NULL 1
B Tai NULL 1
I need to insert all columns of the additional records from table2 into table1. for example, I need Ken and Tai's records from table2 added to table1. it's running on SQL Server 2000 so I cannot use EXCEPT or INTERSECT.
assuming email is unique:
insert into table1
select * from table2
where email not in (select email from table1)
alternative:
insert into table1
select a.*
from table2 a
LEFT JOIN table1 b
ON a.email = b.email
WHERE b.email IS NULL
this will do it..
Inser Table1
select GroupName, PersonName, email, phone
from Table2 where GroupName+'|'+PersonName+'|'+email+'|'+phone
not in (select GroupName+'|'+PersonName+'|'+email+'|'+phone from Table1)
Thanks!
#leo.
Related
Currently I have 3 tables like below
Master
ID_NUMBER
ZIPCODE
1
12341
2
12342
3
12343
4
12344
Table1
ID_NUMBER
CITYNAME
COUNTYNAME
1
NEW YORK
QUEENS
3
DETROIT
SUFFOLK
Table2
ID_NUMBER
CITYNAME
COUNTYNAME
2
ATLANTA
ROCKLAND
4
BOSTON
WINCHESTER
My desired output is like below. I want to filter based on the zipcode from master table
ID_NUMBER
ZIPCODE
CITYNAME
COUNTYNAME
2
12342
ATLANTA
ROCKLAND
How would i go about writing a query for this? Below is what i have tried but it's giving me null values if the ID_NUMBER is not found on that particular table.
SELECT mstr.id_number,
mstr.zipcode,
t1.cityname,
t1.countyname,
t2.cityname,
t2.countyname
FROM MASTER mstr
LEFT JOIN Table1 t1 ON mstr.id_number=t1.id_number
LEFT JOIN Table2 t2 ON mstr.id_number=t2.id_number
WHERE mstr.zipcode='12342'
Use COALESCE():
SELECT mstr.id_number,
mstr.zipcode,
COALESCE(t1.cityname, t2.cityname) as cityname
COALESCE(t1.countyname, t2.countyname) as countyname
FROM MASTER mstr LEFT JOIN
Table1 t1
ON mstr.id_number = t1.id_number LEFT JOIN
Table2 t2
ON mstr.id_number = t2.id_number
WHERE mstr.zipcode = '12342
'
Another approach you can try since your tables are identical is to join master with the union of the tables
with t as (
select Id_number, Cityname, Countyname
from t1
union all
select Id_number, Cityname, Countyname
from t2
)
select *
from Master m join t on m.Id_number=t.Id_Number
where m.zipcode='12342'
I have a oracle table called table1 that has a foreign key from table2.
table2 has 2 columns: id, name
table2.name has a lot of duplicates that need to be sorted out, so I grouped table2 by name and kept only 1 id for each name.
However, table1.table2_id uses a lot of the duplicated fields.
How can I change all the table1.table2_id fields so that there are no duplicate names?
Currently:
table1:
id
blabla
table2_id
1
row
1001
2
row
1002
3
row
1003
4
row
1004
5
row
1004
6
row
1005
table2:
id
name
1001
Bob
1002
Bob
1003
Bob
1004
Jack
1005
Jack
Desired Result:
table1:
id
blabla
table2_id
1
row
1001
2
row
1001
3
row
1001
4
row
1004
5
row
1004
6
row
1004
table2:
id
name
1001
Bob
1004
Jack
So imo, I would need to:
update all table1.table2_id to top 1 table2.id grouped by name
delete duplicate rows from table2
And as there are thousands of duplicate fields in table2, I cannot use case for this..
The solution I am trying returns 1 id for each name, but I am failing at the part where I can update the table.
update table1 d
set d.table2_id =
(select
b.id
from table2 b
where b.name in (select min(id) keep(dense_rank first order by id) id
, name
from table2
group by name)
order by b.id ASC
)
where d.table2_id in ( SELECT b.id FROM table2 b WHERE b.name in (select min(id) keep(dense_rank first order by id) id
, name
from table2
group by name));
Please help :)
Here are syntactically correct Oracle statements
update table1 t1
set t1.table2_id = (
select new_id
from (
select id, min(id) over (partition by name) new_id
from table2
) d
where d.id = t1.table2_id
);
delete from table2
where id not in (
select min(id) from table2 group by name
);
The analytic function min(id) over (partition by name) is used here so that you can have all original ids together with their new ids (the min ids from the set where the name is the same).
here is one way:
update table1 d
set d.table2_id = x.id_tokeep
from
(
select name , id , min(id) over (partition by name ) as id_tokeep
from table2
) x
where x.id = d.table2_id
delete from table2
where id not in ( select min(id) from table2 group by name)
I have two tables like so:
table1(user, id, sex) table2(user, name, sex)
jjj 123 m jjj John m
jjj 124 m bbb Bob m
jjj 125 m ppp Pete f
bbb 126 m
bbb 127 f
ppp 128 f
ppp 129 m
ppp 130 m
I want result table where it displays all the users with their names and sex from table 2 who changed their sex at some point along with a count of how many users each name has. So this would be the result table:
(user, name, sex, count)
bbb Bob m 2
ppp Pete f 3
Currently im using this query:
select table2.user, table2.name, table2.sex, count(*)
from table1
join table2 on table1.user = table2.user
where table1.sex <> table2.sex
group by table2.user, table2.name, table2.sex
order by user
However the count column just counts from the resulting join table and not from original table1. Any ideas? thanks.
If I follow this correctly, one option use a lateral join and filtering:
select t2.*, t1.cnt
from table2 t2
inner join lateral (
select count(*) as cnt, min(sex) as minsex, max(sex) as maxsex
from table1 t1
where t1.user = t2.user
) t1 on t1.minsex <> t1.maxsex or t1.minsex <> t2.sex
Basically this filters table1 on users that have different sex or whose sex is different than in table2.
I have two tables like below. I am using SQL DB.
Table1:
Id Name
1 leo
2 lin
3 kim
4 ken
5 jon
6 kelly
Table2:
Id Name Date
1 leo 2019-04-11
2 lin 2019-04-17
3 kim 2019-02-15
4 ken 2018-04-11
I am trying to compare this two table and find out which name in table1 does not have a Date.
I need this output.
Result:
Id Name
5 jon
6 kelly
Could anyone please help me with this query.
Use a LEFT JOIN:
SELECT * FROM Table1
LEFT JOIN Table2
ON Table1.Name = Table2.Name
WHERE Table2.Name is NULL
Or you can use WHERE NOT EXISTS:
SELECT * From Table1
WHERE NOT EXISTS (SELECT 1 FROM Table2 WHERE Table1.Name = Table2.Name)
Or you can use WHERE NOT IN:
SELECT * From Table1
WHERE Name NOT IN (SELECT Name FROM Table2)
Try this below:
SELECT Id,Name FROM Table1
EXCEPT
SELECT Id,Name FROM Table2
I want a query to select more than 4 AccountId repetitions from table2 having distinct name in table1
Here are the table structures:
table1 | Table2
ID AccountNumber Name | ID AccountNumber AccountID
1 12345 John Jeff | 1 12345 A467T
1 12345 Patrick Jones | 1 12345 A467T
ID and AccountNumber are the same on both tables
Table1 has Name
Table2 has AccountID
Here's a query to find AccountID's with more than 4 different names:
select t2.AccountID
from Table1 as t1
join Table2 as t2
on t1.AccountNumber = t2.AccountNumber
and t1.ID = t2.ID
group by
t2.AccountID
having count(distinct t1.Name) > 4
If that's not what you were looking for, please clarify the question! For examole, you could add desired output.
EDIT: In reply to your comment, you could query the ID and Numbers with a subquery:
select distinct
ID
, AccountNumber
, AccoutnID
from Table2
where AccountNumber in
(
... place query from above here ...
)