Sort and remove duplicates
Sort and remove duplicates
am 28.09.2007 15:10:54 von JimJx
Hi everyone,
Got what should be an easy problem.
I have a db (MySQL) that I need to sort on the first field and remove
dups from.
For example, let's say I have Red,1,2,3,4,5 and Blue,1,2,3,4,5 and
Green,1,2,3,4,5 and Green,2,2,2,2,2
How can I come out with Blue,1,2,3,4,5;Green,1,2,3,4,5;Red,1,2,3,4,5??
I know that there is a simple way to do this but my brain is not
functioning right now.
Thanks!
Jim
Re: Sort and remove duplicates
am 28.09.2007 16:33:55 von gbacon
In article <1190985054.159629.205280@22g2000hsm.googlegroups.com>,
JimJx wrote:
: I have a db (MySQL) that I need to sort on the first field and
: remove dups from.
:
: For example, let's say I have Red,1,2,3,4,5 and Blue,1,2,3,4,5
: and Green,1,2,3,4,5 and Green,2,2,2,2,2
:
: How can I come out with Blue,1,2,3,4,5;Green,1,2,3,4,5;
: Red,1,2,3,4,5??
Why does the Green row get 1,2,3,4,5 on the output?
Greg
--
The list of stable paper currencies built by central bankers is as
short as the list of stable democracies built by armed invaders.
-- Bill Bonner
Re: Sort and remove duplicates
am 28.09.2007 17:00:53 von JimJx
On Sep 28, 10:33 am, gba...@hiwaay.net (Greg Bacon) wrote:
> In article <1190985054.159629.205...@22g2000hsm.googlegroups.com>,
> JimJx wrote:
>
> : I have a db (MySQL) that I need to sort on the first field and
> : remove dups from.
> :
> : For example, let's say I have Red,1,2,3,4,5 and Blue,1,2,3,4,5
> : and Green,1,2,3,4,5 and Green,2,2,2,2,2
> :
> : How can I come out with Blue,1,2,3,4,5;Green,1,2,3,4,5;
> : Red,1,2,3,4,5??
>
> Why does the Green row get 1,2,3,4,5 on the output?
>
> Greg
> --
> The list of stable paper currencies built by central bankers is as
> short as the list of stable democracies built by armed invaders.
> -- Bill Bonner
Because Green would be a dup and 1,2,3,4,5 comes before 2,2,3,4,5
Re: Sort and remove duplicates
am 28.09.2007 17:21:21 von Glenn Jackman
At 2007-09-28 09:10AM, "JimJx" wrote:
> Hi everyone,
>
> Got what should be an easy problem.
>
> I have a db (MySQL) that I need to sort on the first field and remove
> dups from.
>
> For example, let's say I have Red,1,2,3,4,5 and Blue,1,2,3,4,5 and
> Green,1,2,3,4,5 and Green,2,2,2,2,2
>
> How can I come out with Blue,1,2,3,4,5;Green,1,2,3,4,5;Red,1,2,3,4,5??
my %data;
while () {
my $field1 = (split ',')[0];
$data{$field1} = $_ unless exists $data{$field1};
}
my @non_dups = values %data;
--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
Re: Sort and remove duplicates
am 28.09.2007 17:32:40 von Ben Morrow
Quoth JimJx :
> Hi everyone,
>
> Got what should be an easy problem.
>
> I have a db (MySQL) that I need to sort on the first field and remove
> dups from.
>
> For example, let's say I have Red,1,2,3,4,5 and Blue,1,2,3,4,5 and
> Green,1,2,3,4,5 and Green,2,2,2,2,2
>
> How can I come out with Blue,1,2,3,4,5;Green,1,2,3,4,5;Red,1,2,3,4,5??
>
> I know that there is a simple way to do this but my brain is not
> functioning right now.
perldoc DBI
perldoc -q duplicate
(possibly perldoc -f sort as well)
When you've had a go, post your code and we'll help you fix it.
Ben
Re: Sort and remove duplicates
am 28.09.2007 18:02:37 von gbacon
In article <1190991653.009964.230380@y42g2000hsy.googlegroups.com>,
JimJx wrote:
: On Sep 28, 10:33 am, gba...@hiwaay.net (Greg Bacon) wrote:
:
: > Why does the Green row get 1,2,3,4,5 on the output?
:
: Because Green would be a dup and 1,2,3,4,5 comes before 2,2,3,4,5
Ah. I thought you wanted to remove duplicates in the other columns
too. (The five 2s threw me off.)
Glenn Jackman's answer is what you want.
Greg
--
To admit that labor needs protection is to acknowledge its inferiority.
-- Henry George, Protection or Free Trade
Re: Sort and remove duplicates
am 28.09.2007 22:54:43 von Martijn Lievaart
On Fri, 28 Sep 2007 06:10:54 -0700, JimJx wrote:
> Hi everyone,
>
> Got what should be an easy problem.
>
> I have a db (MySQL) that I need to sort on the first field and remove
> dups from.
>
> For example, let's say I have Red,1,2,3,4,5 and Blue,1,2,3,4,5 and
> Green,1,2,3,4,5 and Green,2,2,2,2,2
>
> How can I come out with Blue,1,2,3,4,5;Green,1,2,3,4,5;Red,1,2,3,4,5??
>
> I know that there is a simple way to do this but my brain is not
> functioning right now.
select field1, min(field2), min(field3) ... from table group by field1;
HTH,
M4