Find id having duplicate address - sql

I have a table
Id
cno
Firstname
Lastname
address
1.
111.
A.
B.
Xyz street
1.
222.
C.
D.
Xyz street
2
333.
E.
F.
Vwx
I want records where id and address is same but other details are different. I tried using join but not getting desired result

Related

Oracle: Move column to new table, eliminate non-unique rows remaining in original table while maintaining foreign relationship between two tables

I have a table like the following:
Table 1: Person_Favorite_Food
id name address favorite_food
------------------------------------------
1 Dave 123 Cherry Ln Pizza
2 Dave 123 Cherry Ln Cheeseburger
3 Dave 456 Peachtree St Ice cream
4 Cindy 789 Grove Rd Pizza
id - primary key
unique key constraint on the following columns: name, address and food
Since each person can have more than one favorite food item I'd like to split Table 1 into two tables like the following:
Table 2: Person
id name address
--------------------------
1 Dave 123 Cherry Ln
3 Dave 456 Peachtree St
4 Cindy 789 Grove Rd
Table 3: Person_Favorite_Food
person_id favorite_food
-----------------
1 Pizza
1 Cheeseburger
3 Ice cream
4 Pizza
How would I go about doing this in Oracle?
Note: In the original table Rows 1 and 2 represent favorite food for the same person so the favorite food entries in the Person_Favorite_Food table will need to have the same identifier for both of those entries although the identifiers are different in the initial table.
You can use:
create table new1 as
select distinct id, name, address
from t;
create table new2 as
select id, favorite_food
from t;
I would recommend create two new tables and not trying to morph the existing table into one of the new ones.
Use simple aggregation to create table person:
create table person as
select min(id) id, name, address
from person_favorite_food
group by name, address;
Use the same query in merge to change id for some rows:
merge into person_favorite_food a
using (select min(id) id, name, address from person_favorite_food group by name, address) b
on (a.name = b.name and a.address = b.address)
when matched then update set a.id = b.id;
Drop unwanted columns:
alter table person_favorite_food drop (name, address);
Done. Demo in dbfiddle.

Find rows for an id where value changes in any of the coloumn in sql

I have a table with say 10 columns where the unique identifier is id column.
I need print all the rows where value for any given id changes for any of the 10 coloums.
Eg:
Id name city state address mail phn number store no business name date
1 adam california ca acbb street xyz#gmail.com 12345 456 abc pvt 01-01-2017
1 adam newyork Ny avc xyz#gmail.com 12345 456 abc pvt 11-03-2018
1 adam newyork Ny avc xyz#gmail.com 12345 456 abcd pvt 24-03-2018
2 brian dallas Tx sasa sasa#gmail.com 21212 dsdssd ltd 01-01-2017
2 brian dallas Tx sasa sasa#gmail.com 232323 21212 dsdssd ltd 01-01-2017
3 donald dallas Tx qwer qwwqw#gmail.com 2121212 435345 sffsss ltd 23-01-2017
As shown above for id 1 there is a change from the first row to 2nd row on city state address so that should come .also for id 1 the 3rd row changes only business name so that should also be printed.
So basically for an id the status for that id before that date should be checked and if there is any change in any column it should be printed.
Same goes for id 2.For id 3 there is only one entry so that should not be printed.
The check needs to be done based on date for an id.
The data set that I have is in millions so would need a fine tuned query.
If you have a column for row number( for example ROWID), you can simply select all rows that has a pair (based on ID) in previous records and check if any of the last row columns differs from the first one:
select a.* from [yourTable] a
inner join [yourTable] b
on a.Id = b.Id where a.RowId > b.RowId
and (a.name <> b.name OR a.city <> b.city OR a.state <> b.state OR a.address <> b.address ... )

Simple nested query on the same table

So this is my table set up of 100k rows. I have around 30k rows which have the wrong dealer associated to it even though it refers to the same person but just a different bank designation. This happened due to a failure to port information accurately from a previous version of the database .
Table CustomerName
Name Bank Dealer SSN
John 1 ABC unique1
Mike 1 DEF unique2
Mike 2 wrong unique2
Mark 1 XYZ unique3
Mark 2 wrong unique3
Desired Table set up
Table CustomerName
Name Bank Dealer SSN
John 1 ABC unique1
Mike 1 DEF unique2
Mike 2 DEF unique2
Mark 1 XYZ unique3
Mark 2 XYZ unique3
I want to write a query which will target the rows (Bank 2 rows essentially) and change it to Bank 1 Dealer values. Is there a way to do it ? I'm using T-SQL ( SSMS 2016 )
EDIT :
SSN are like primary keys for the customer. every customer will have one ssn . the bank 2 is basically a delinquent account bank. A customer may or may not have a bank 2 account , but they'll have a bank 1 account. But my problem is that somehow the dealer didn't come through right for bank 2 and I need to update to the correct value
The question is which Dealer to use. Let me guess that it is the first one. You can use CTE and update to accomplish this:
with topudate as (
select cn.*,
max(case when bank = 1 then dealer end) over (partition by ssn) as dealer1
from customername cn
)
update toupdate
set dealer = dealer1
where dealer <> dealer1 or dealer is null;
If you have some other logic for getting the right name, then that would go in the case expression instead.
update bank2
set bank2.dealer = bank1.dealer
from CustomerName bank1
join CustomerName bank2
on bank2.SSN = bank1.SSN
and bank2.Bank = 2
and bank1.Bank = 1

How to split String and insert it as new line in the same table?

i have a table Address.
create table address(
id number,
city text,
street text,
house_number text);
some times i get city = Berlin, street = xyz and house_number
instead 3 ,for example, 3-5 or 3-5-7.
i want in this case to split this numbers and crate new line/s so i get :
id city street house_number
1. Berlin xyz 3
2. Berlin xyz 5
2. Berlin xyz 7
With regard
Andrey
Use select unnest(string_to_array(YOUR STRING, '-'));
it will convert '3-5-7'
to:
3
5
7
Then you can simply run a query that will insert the rows to your table.
See SQLFiddle.

How to insert a record in a table so that the string field with id?

I have a table to be imported from excel.It has an column called the sector table name "ExcelTable"
Name Title Sector
John manager Sofware
Sam Lawyer Jus
"ExcelTable" has 3284 rows.I create table called "SECTORS"."SECTORS" table's cloumn like this
SectorId SectorName
1 Sofware
2 Jus
It has 61 rows.
I inserted "EXCELTABLE" to "GLOBALCONTACTS".They has same rows number 3284 I want to insert "GLOBAL_CONTACTS" table sector by sectorid .It is now
ContactId Name Title Sector
1 John manager null
2 Sam Lawyer null
I want it to be like this
ContactId Name Title Sector
1 John manager 1
2 Sam Lawyer 2
I think you just want to join ExcelTable to Sectors for an INSERT:
INSERT INTO GLOBAL_CONTACTS (Name,Title,Sector)
SELECT e.Name,e.Title,s.SectorID
FROM ExcelTable e
INNER JOIN Sectors s
ON e.Sector = s.SectorName