Corrupt double encoding with Portugese - utf-8

I have a database that has a lot of corrupt entries, such as:
IVAM BENEDITO GONÃALVES DE QUEIROZ
I have tried several encoding conversion to no avail. I've found other reports where by the characters à should be Ã. However in this case à should be Ç.
Does anyone have any idea how to successfully convert this? At current I am going through locating the problem characters and replacing the with REPLACE().

Not sure of the specific problem, but these replacements fixed the data:
UPDATE Brazil SET name = REPLACE( name, 'Ã','ç' ) WHERE name LIKE '%Ã%';
UPDATE Brazil SET name = REPLACE( name, 'À','A ' ) WHERE name LIKE '%À%';
UPDATE Brazil SET name = REPLACE( name, '©','ú' ) WHERE name LIKE '%©%';
UPDATE Brazil SET name = REPLACE( name, 'Á','A' ) WHERE name LIKE '%Á%';
UPDATE Brazil SET name = REPLACE( name, 'Â','A' ) WHERE name LIKE '%Â%';
UPDATE Brazil SET name = REPLACE( name, 'é','A' ) WHERE name LIKE '%é%';
UPDATE Brazil SET name = REPLACE( name, '±','é' ) WHERE name LIKE '%±%';
UPDATE Brazil SET name = REPLACE( name, 'ú','ú' ) WHERE name LIKE '%ú%';
UPDATE Brazil SET name = REPLACE( name, 'º','í' ) WHERE name LIKE '%º%';
UPDATE Brazil SET name = REPLACE( name, '³','ú' ) WHERE name LIKE '%³%';
UPDATE Brazil SET name = REPLACE( name, 'ô','é' ) WHERE name LIKE '%ô%';
UPDATE Brazil SET name = REPLACE( name, 'ö','ê' ) WHERE name LIKE '%ö%';
UPDATE Brazil SET name = REPLACE( name, 'Ç','ç' ) WHERE name LIKE '%Ç%';
UPDATE Brazil SET name = REPLACE( name, '¤','á' ) WHERE name LIKE '%¤%';
UPDATE Brazil SET name = REPLACE( name, 'Z¡','á' ) WHERE name LIKE '%Z¡%';
UPDATE Brazil SET name = REPLACE( name, '¡','ó' ) WHERE name LIKE '%¡%';
UPDATE Brazil SET name = REPLACE( name, 'ý','ç' ) WHERE name LIKE '%ý%';
UPDATE Brazil SET name = REPLACE( name, '┼','é' ) WHERE name LIKE '%┼%';
UPDATE Brazil SET name = REPLACE( name, '¾','á' ) WHERE name LIKE '%¾%';
UPDATE Brazil SET name = REPLACE( name, 'Ø','ã' ) WHERE name LIKE '%Ø%';
UPDATE Brazil SET name = REPLACE( name, 'Ø','ã' ) WHERE name LIKE '%Ø%';
UPDATE Brazil SET name = REPLACE( name, 'É','é' ) WHERE name LIKE '%É%';
UPDATE Brazil SET name = REPLACE( name, 'Ç','ç' ) WHERE name LIKE '%Ç%';
UPDATE Brazil SET name = REPLACE( name, 'â•”','é' ) WHERE name LIKE '%â•”%';
UPDATE Brazil SET name = REPLACE( name, 'Õ','ú' ) WHERE name LIKE '%Õ%';
UPDATE Brazil SET name = REPLACE( name, 'â•','í' ) WHERE name LIKE '%â•%';
UPDATE Brazil SET name = REPLACE( name, '¶','ã' ), name = REPLACE( name, 'µ','á' ), name = REPLACE( name, 'Â','' ) WHERE name LIKE '%ÂÂ%';
UPDATE Brazil SET name = REPLACE( name, 'µ','ñ' ) WHERE name LIKE '%µ%';
UPDATE Brazil SET name = REPLACE( name, 'Ãœ','ü' ) WHERE name LIKE '%Ãœ%';
UPDATE Brazil SET name = REPLACE( name, '├','ã' ) WHERE name LIKE '%├%';
UPDATE Brazil SET name = REPLACE( name, 'ë','ã' ) WHERE name LIKE '%ë%';
UPDATE Brazil SET name = REPLACE( name, 'Ô','õ' ) WHERE name LIKE '%Ô%';
UPDATE Brazil SET name = REPLACE( name, '¨','ã' ) WHERE name LIKE '%¨%';
UPDATE Brazil SET name = REPLACE( name, 'ÃŒ','â' ) WHERE name LIKE '%ÃŒ%';
UPDATE Brazil SET name = REPLACE( name, '°','é' ) WHERE name LIKE '%°%';
UPDATE Brazil SET name = REPLACE( name, 'æ','í' ) WHERE name LIKE '%æ%';
UPDATE Brazil SET name = REPLACE( name, 'Â¥','ú' ) WHERE name LIKE '%Â¥%';
UPDATE Brazil SET name = REPLACE( name, '¿','ó' ) WHERE name LIKE '%¿%';
UPDATE Brazil SET name = REPLACE( name, '×','ç' ) WHERE name LIKE '%×%';
UPDATE Brazil SET name = REPLACE( name, '┬Â','ã' ) WHERE name LIKE '%┬Â%';
UPDATE Brazil SET name = REPLACE( name, 'Â','â' ) WHERE name LIKE '%Â%';
UPDATE Brazil SET name = REPLACE( name, 'ãâ','ã' ) WHERE name LIKE '%ãâ%';
UPDATE Brazil SET name = REPLACE( name, 'Z£','ã' ) WHERE name LIKE '%Z£%';
UPDATE Brazil SET name = REPLACE( name, '£','ú' ) WHERE name LIKE '%£%';
UPDATE Brazil SET name = REPLACE( name, 'Z¢','ã' ) WHERE name LIKE '%Z¢%';
UPDATE Brazil SET name = REPLACE( name, 'ã│','ó' ) WHERE name LIKE '%ã│%';
UPDATE Brazil SET name = REPLACE( name, 'ã¢','ç' ) WHERE name LIKE '%ã¢%';
UPDATE Brazil SET name = REPLACE( name, 'á┢','ç' ) WHERE name LIKE '%á┢%';
UPDATE Brazil SET name = REPLACE( name, '┬│','a ' ) WHERE name LIKE '%┬│%';
UPDATE Brazil SET name = REPLACE( name, 'ç£','í' ) WHERE name LIKE '%ç£%';
UPDATE Brazil SET name = REPLACE( name, '¶','õ' ) WHERE name LIKE '%¶%';
UPDATE Brazil SET name = REPLACE( name, '§','ú' ) WHERE name LIKE '%§%';
UPDATE Brazil SET name = REPLACE( name, '¾','ó' ) WHERE name LIKE '%¾%';
UPDATE Brazil SET name = REPLACE( name, 'Ã','ç' ) WHERE name LIKE '%Ã%';

Related

How to display all data with nulls using Pivot [duplicate]

please help me solve this problem:
You are given a table, containing two columns:
column is one of the followings:
Doctor
Professor
Singer
Actor
Write a query to output the names underneath the corresponding occ. in the following format:
+--------+-----------+--------+------+
| Doctor | Professor | Singer | Actor|
+--------+-----------+--------+------+
Names must be listed in alphabetically sorted order.
Sample Input
Name Occupation
Meera Singer
Ashely Professor
Ketty Professor
Christeen Professor
Jane Actor
Jenny Doctor
Priya Singer
Sample Output
Jenny Ashley Meera Jane
Samantha Christeen Priya Julia
NULL Ketty NULL Maria
Note
Print "NULL" when there are no more names corresponding to an occupation.
I tried using :
SELECT *
FROM
(
SELECT [Name], [Occupation]
FROM occupations
) AS source
PIVOT
(
max([Name])
FOR [occupation] IN ([Doctor], [Professor], [Singer], [Actor])
) as pvt;
which gives the following output:
Priya Priyanka Kristeen Samantha
How to fix it ?
You just need to give each name a row number based on their occupation and order alphabetically.. then include that row number in your pivot query.
CREATE TABLE Occupations (
NAME VARCHAR(MAX),
Occupation VARCHAR(MAX)
)
INSERT INTO Occupations
VALUES
('Samantha','Doctor'),
('Julia','Actor'),
('Maria','Actor'),
('Meera','Singer'),
('Ashley','Professor'),
('Ketty','Professor'),
('Christeen','Professor'),
('Jane','Actor'),
('Jenny','Doctor'),
('Priya','Singer');
SELECT
[Doctor],
[Professor],
[Singer],
[Actor]
FROM
(SELECT
ROW_NUMBER() OVER (PARTITION BY Occupation ORDER BY Name) rn,
[Name],
[Occupation]
FROM
Occupations
) AS source
PIVOT
(MAX(Name) FOR [occupation] IN ([Doctor],[Professor],[Singer],[Actor])) as pvt
ORDER BY rn
DROP TABLE Occupations
-- Edit: We need to enclose the subquery after PIVOT within parenthesis "()" to make it work on SQL Server.
I tried this in Oracle, seemed easier to comprehend:
SELECT min(Doctor), min(Professor), min(Singer), min(Actor)
FROM
( Select
ROW_NUMBER() OVER (PARTITION BY Occupation order by Name) rn,
CASE
WHEN Occupation = 'Doctor' then Name
end as Doctor,
CASE
WHEN Occupation = 'Professor' then Name
end as Professor,
CASE
WHEN Occupation = 'Singer' then Name
end as Singer,
CASE
WHEN Occupation = 'Actor' then Name
end as Actor
from OCCUPATIONS
order by Name) a
group by rn
order by rn;
SELECT [Doctor], [Professor], [Singer], [Actor] FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY Occupation ORDER BY Name) ROW_NO,
ISNULL(NULL,Name) as Name, Occupation
FROM Occupations
) AS t
PIVOT(
MAX(Name)
FOR Occupation IN (
[Doctor],
[Professor],
[Singer],
[Actor]
)
) AS pivot_table
ORDER BY ROW_NO;
Here is the MYSQL version of the answer. Little difficult because of no presence of FULL OUTER JOIN in HACKERRANK.com
SET #r1=0, #r2 = 0, #r3 = 0, #r4 = 0;
SELECT t1.name, t2.name, t3.name, t4.name
FROM
(SELECT
(#r1:=#r1 + 1) AS num1,
Name
FROM
Occupations
WHERE Occupation = 'Doctor'
ORDER BY Name) as t1
RIGHT JOIN
(SELECT
(#r2:=#r2 + 1) AS num2,
Name
FROM
Occupations
WHERE Occupation = 'Professor'
ORDER BY Name) as t2
ON t1.num1 = t2.num2
LEFT JOIN
(SELECT
(#r3:=#r3 + 1) AS num3,
Name
FROM
Occupations
WHERE Occupation = 'Singer'
ORDER BY Name) as t3
ON t2.num2 = t3.num3
LEFT JOIN
(SELECT
(#r4:=#r4 + 1) AS num4,
Name
FROM
Occupations
WHERE Occupation = 'Actor'
ORDER BY Name) as t4
ON t2.num2 = t4.num4
SET #r1=0, #r2=0, #r3 =0, #r4=0;
SELECT MIN(Doctor), MIN(Professor), MIN(Singer), MIN(Actor) FROM
(SELECT CASE Occupation WHEN 'Doctor' THEN #r1:=#r1+1
WHEN 'Professor' THEN #r2:=#r2+1
WHEN 'Singer' THEN #r3:=#r3+1
WHEN 'Actor' THEN #r4:=#r4+1 END
AS RowLine,
CASE WHEN Occupation = 'Doctor' THEN Name END AS Doctor,
CASE WHEN Occupation = 'Professor' THEN Name END AS Professor,
CASE WHEN Occupation = 'Singer' THEN Name END AS Singer,
CASE WHEN Occupation = 'Actor' THEN Name END AS Actor
FROM OCCUPATIONS ORDER BY Name) AS t
GROUP BY RowLine;

Oracle sql select data from table where attribute is nested table

I have a objects:
create type t_history_rec is object
(
date_from date,
current float
);
create type t_history is table of t_history_rec;
and table defined:
create table person
(
id integer primary key,
name varchar2(30),
history t_history
);
and I want to get select name, history.date_from, history.current like this:
name1 date1 current1
name1 date2 current2
name2 date3 current3
...
How to do this?
Cannot verify this, but you could try something like this:
select p.name, pp.date_from, pp.current
from person p, table(p.history) pp;
You have some errors. current is reserved
create or replace type t_history_rec is object
(
date_from date,
curr float
);
/
create type t_history is table of t_history_rec;
/
Table definition needs store as
create table person
(
id integer primary key,
name varchar2(30),
history t_history
) NESTED TABLE history STORE AS col1_tab;
insert into person (id, name, history) values (1, 'aa', t_history(t_history_rec(sysdate, 1)));
insert into person (id, name, history) values (2, 'aa', t_history(t_history_rec(sysdate, 1), t_history_rec(sysdate, 1)));
Then select is:
SELECT t1.name, t2.date_from, t2.curr FROM person t1, TABLE(t1.history) t2;

codeigniter active record Join multiple select statements

I cannot find any refrence on joining multiple selects like the query below in active record, anyone know how such is done?
select a1.act, a1.date, a2.clos
from
(
SELECT count( AccountActiveDate ) act, DATE_FORMAT( AccountActiveDate, '%Y-%m' ) date
FROM customert cust
GROUP BY YEAR( AccountActiveDate ) , MONTH( AccountActiveDate )
) a1
join
(
SELECT count( AccountClosedDate ) clos, DATE_FORMAT( AccountClosedDate, '%Y-%m' ) date
FROM customert cust2
GROUP BY YEAR( AccountClosedDate) , MONTH( AccountClosedDate)
) a2
ON a1.date = a2.date
You can put you sql string like this:
$this->db->query('YOUR QUERY HERE')
Example:
$this->db->query('SELECT * FROM tbl_users WHERE age > 18');

LINQ Left Join on Max Date

Ok so here are my tables in the database:
CREATE DATABASE Temp
GO --------------------------
USE Temp
GO --------------------------
CREATE TABLE Table1
(
Table1Id INT IDENTITY(1, 1) ,
Name VARCHAR(20) ,
CONSTRAINT pk_Table1 PRIMARY KEY ( Table1Id )
)
GO --------------------------
CREATE TABLE Table2
(
Table2Id INT IDENTITY(1, 1) ,
Table1Id INT ,
NAME VARCHAR(20) ,
TheDate SMALLDATETIME ,
CONSTRAINT pk_Table2 PRIMARY KEY ( Table2Id ) ,
CONSTRAINT fk_Table2_Table1 FOREIGN KEY ( Table1Id ) REFERENCES Table1 ( Table1Id )
)
GO --------------------------
INSERT INTO Table1
( Name )
VALUES ( 'Stack Overflow' )
GO --------------------------
INSERT INTO Table1
( Name )
VALUES ( 'Expert Sex Change' )
GO --------------------------
INSERT INTO Table1
( Name )
VALUES ( 'Code Project' )
GO --------------------------
INSERT INTO dbo.Table2
( Table1Id ,
NAME ,
TheDate
)
VALUES ( 1 ,
'S1' ,
'11-01-2012'
)
GO --------------------------
INSERT INTO dbo.Table2
( Table1Id ,
NAME ,
TheDate
)
VALUES ( 1 ,
'S2' ,
'11-01-2013'
)
GO --------------------------
INSERT INTO dbo.Table2
( Table1Id ,
NAME ,
TheDate
)
VALUES ( 2 ,
'E1' ,
'10-01-2013'
)
And here's my LINQ:
from t1 in Table1s
join t2 in Table2s.OrderByDescending(x => x.TheDate)
on t1.Table1Id equals t2.Table1Id into tt
from t2 in tt.DefaultIfEmpty()
select new
{
t1.Table1Id,
t1.Name,
t2.NAME,
t2.TheDate
}
This one returns:
Table1Id - Name - NAME - TheDate
1 - Stack Overflow - S2 - 11/1/2013
2 - Expert Sex Change - E1 - 10/1/2013
1 - Stack Overflow - S1 - 11/1/2012
3 - Code Project - null - null
I want the LINQ query not to return the third line, as is from an older Date Value.
I think I got it, the answer is:
from t1 in Table1s
join t2 in Table2s
on t1.Table1Id equals t2.Table1Id
into tt
from x in tt.DefaultIfEmpty()
// where ... t1 && x ..
orderby t1.Table1Id
group x by new {t1.Table1Id,t1.Name} into g
select new {
Table1Id = g.Key.Table1Id,
Name = g.Key.Name,
TheDate = g.Max(c => c.TheDate)
}

identify duplicates as well as the matched unique record in oracle

hi i am running the follwoing query to identify the duplicate records.
SELECT *
FROM unique2 P WHERE EXISTS(SELECT 1 FROM unique2 C
WHERE ( (C.surname) = (P.surname))
AND ( (C.postcode) = (P.postcode))
AND ((( (C.forename) IS NULL OR (P.forename) IS NULL)
AND (C.initials) = (P.initials))
OR (C.forename) = (P.forename))
AND ( (C.sex) = (P.sex)
OR (C.title) = (P.title))
AND (( (C.address1))=( (P.address1))
OR ( (C.address1))=( (P.address2))
OR ( (C.address2))=( (P.address1))
OR instr(C.address1_notrim, P.address1_notrim) > 0
OR instr(P.address1_notrim, C.address1_notrim) > 0)
AND C.rowid < P.rowid);
But with this query i can't identify the unique record id which is matched to the duplicate records. Is there a way to identify the
duplicates as well as the unique record id(my table has unique key) to which those duplicates are matched?
select id
from promolog
where surname, postcode, dob in (
select surname, postcode,dob
from (
select surname, postcode, dob, count(1)
from promolog
group by surname,postcode,dob
having count(1) > 1
)
)
You can also do this with analytic functions:
select id, num_of_ids, first_id, surname, postcode, dob
from (
select id,
count(*) over (partition by surname, postcode, dob) as num_of_ids,
first_value(id)
over (partition by surname, postcode, dob order by id) as first_id,
surname,
postcode,
dob
from promolog
)
where num_of_ids > 1;
Based on your update, I think you can just do a self-join, which you can make as complicated as you like:
select dup.*, master.id as duplicate_of
from promolog dup
join promolog master
on master.surname = dup.surname
and master.postcode = dup.postcode
and master.dob = dup.dob
... and <address checks etc. > ...
and master.rowid < dup.rowid;
But maybe I'm still missing something. As the name suggests, exists is for testing the existence of a matching record; if you want to retrieve any of the data from the matched record then you'll need to join to it at some point.

Resources