I want to do data migration from one table to another table. Source table has country names in free floating text and destination table need to have country code.
For example source table has country names with incorrect spelling.
e.g. United Stts of Americ --> USA
Franc --> FRA
Can we have some key value pair in SQL where we can store these incorrect country names to 3 character country code?
PL/SQL tag reads as "Oracle". If that's so, see if something like this helps.
Sample tables:
SQL> select * from state;
ABB NAME
--- ------------------------
FRA France
USA United States of America
GER Germany
SQL> select * from garbage;
NAME
----------------------
United Stts of Americ
Unted states of Amerca
Frnce
France
Gremany
SQL>
Query:
SQL> select g.name, s.name, s.abbr
2 from state s join garbage g
3 on utl_match.jaro_winkler_similarity(g.name, s.name) > 90;
NAME NAME ABB
---------------------- ------------------------ ---
Frnce France FRA
France France FRA
United Stts of Americ United States of America USA
Unted states of Amerca United States of America USA
Gremany Germany GER
SQL>
Tweak similarity value, if necessary. I used 90, see how it behaves for your data.
Related
Apologies if something like this was answered, but I'm not seeing it. This is SQL Server. I'm looking at a list of trucks with unique identifiers (Unique ID). They go to various cities. They arrive (ArrivalDate) in the City (City) and then leave (SentDate). There is no destination column. The movement column is a sort of unique identifier for the movement; the first part is the UniqueID but the second is nothing.
UniqueID
City
ArrivalDate
SentDate
Movement
97841
Los Angeles North
25/01/2021
27/01/2021
97841 : 949814
93621
Baltimore
21/01/2021
22/01/2021
93621 : 646946
96872
Los Angeles South
19/01/2021
19/01/2021
96872 : 685469
97842
Boston
12/12/2020
20/12/2020
97841 : 646488
I'd like to write a query that shows a Unique ID that leaves from a particular city and goes to another. So, it would look something like this:
UniqueID
City 1
City 2
SentDate
ArrivalDate
97841
Los Angeles South
Boston
12/12/2020
15/12/2020
97841
Los Angeles North
Boston
01/01/2021
05/01/2021
I run the code below. I'm using Inner Join on the same table to filter on the UniqueID. I get the right UniqueID, right cities. But obviously the dates aren't linking right. I'm getting valid dates for the cities, but they are out of order/whack. As in sure, the truck arrived in Boston but it's a date for six months later, when it should've been the showing proper arrival date of 4 or 5 days later. Any ideas for the right solutions for this? Thanks!
SELECT DISTINCT
aa.UniqueID,
aa.City AS 'City 1',
bb.City AS 'City 2',
aa.SentDate,
bb.ArrivalDate
FROM Movedata aa
INNER JOIN Movedata bb on bb.UniqueID = aa.UniqueID
WHERE (aa.City LIKE '%Los Angeles South%' AND bb.City LIKE '%Los Angeles North%') AND
(aa.SentDate < bb.ArrivalDate) AND
aa.SentDate BETWEEN '2020-01-28 00:00:00.000' AND '2021-01-29 00:00:00.000'
ORDER by aa.SentDate DESC;
Scenarios:
Table 1
Name ID dept
John 112 Fin
Mary 113 Act
Table 2
Name email
John John#gmail.com
Mary Max#gmail.com
Table 3
Name Supervisor
John Kelly
Mary Adam
Table 4
Namevalue Salary
John 1000
Mary 1000
Expected Output:
I would like to know the occurrence of John and Mary in all these tables. There are more than 20 tables in difference database.
Firstly, specify the master table according to data quality, use this table as base and then use left joins to concat with other tables.
But, I think, there is a design problem in the database so maybe creating keys for each user can be useful.
For example, name + surname + User Something to make records unique then use joins otherwise all records will duplicate.
I am sorry for the supidity of this question, but i am new to SQL. I have the following table:
---------- Table countries -----------
REGION - Country - CITY
EMEA - Italy - Rome
EMEA - Italy - Florence
EMEA - SPAIN - Madrid
APJ - Japan - Tokio
APJ - Japan - Hiroshima
APJ - China - Bejin
I would have to unroll it using recursive queries and obtain what follows:
EMEA
Italy
Rome
Florence
SPAIN
Madrid
APJ
Japan
Tokio
Hiroshima
China
Bejin
I have no idea where to start.. any hints?
If I understand you correctly, you want to get something like this query:
with united AS (
SELECT DISTINCT
REGION col,
1 rnk, --Region goes before Countries and Cities
REGION,
null Country --we need only one appearance for each Region
FROM your_table
UNION ALL
SELECT DISTINCT
Country,
2,
REGION,
Country
FROM your_table
UNION ALL
SELECT
CITY,
3,
REGION,
Country
FROM your_table
)
select col
FROM united
ORDER BY REGION,Country,rnk
fiddle
But there is no recursion.
I have a table and I need to group all the country details in the table as count.
customer id city country
----------------------------------
1 Hyderabad India
2 Tamilnadu India
3 New York USA
4 Los Angeles USA
5 Sydney Australia
I need the output as below - I need to count all the fields and display output.
India 2
USA 2
Australia 1
India is twice in the table,USA is 2 times in the table and Australia is once in the table.
select country, count(*)
from your_table
group by country
Suppose I have country data in unmannered way (not in sequence) in table tbl. Now I want to show Country 'United States','Canada' & 'India' first & rest of the country shown in ascending order.
Country
India
China
Brazil
Azerbaijan
Bahamas
United State
Denmark
Canada
Now I want this data as :
Country
United State // US,Canada,India should be First 3.
Canada
India
Azerbaijan // After rest country comes in ascending Order.
Bahamas
Brazil
China
Denmark
Any suggestion really appreciate.
SELECT Country FROM tbl
ORDER BY CASE
WHEN Country='United State' THEN 0
WHEN Country='Canada' THEN 1
WHEN Country='India' THEN 2
ELSE 3
END, Country;
you could add column FixPosition and then just
select * from Country order by FixPosition asc, Name asc