Forums

Unfortunately no one can be told what FluxBB is - you have to see it for yourself.

You are not logged in.

#1 2009-01-21 02:03:47

MattF
Member
From: South Yorkshire, England
Registered: 2008-05-06
Posts: 1,230
Website

SQL DISTINCT question

Would the following:

'SELECT DISTINCT client_ip, client_key, port FROM '

apply to all of the columns listed, or just the first column after DISTINCT?


Cheer. smile


Screw the chavs and God save the Queen!

Offline

#2 2009-01-21 02:11:05

elbekko
Former Developer
From: Leuven, Belgium
Registered: 2008-04-30
Posts: 1,131
Website

Re: SQL DISTINCT question

The first one (unless I'm somehow horribly mistaken tongue).


Ben
SVN repository for my extensions - The thread
Quickmarks 0.5
“Question: How does a large software project get to be one year late? Answer: One day at a time!” - Fred Brooks

Offline

#3 2009-01-21 03:01:56

MattF
Member
From: South Yorkshire, England
Registered: 2008-05-06
Posts: 1,230
Website

Re: SQL DISTINCT question

I was hoping that wouldn't be the answer. big_smile Just trying to find a way to achieve the PgSQL method but without the MySQL incompatible syntax:

'SELECT DISTINCT ON (client_ip, client_key, port) client_ip FROM '

Back to the drawing board then. big_smile


Screw the chavs and God save the Queen!

Offline

#4 2009-01-21 03:08:27

Smartys
Former Developer
Registered: 2008-04-27
Posts: 3,117
Website

Re: SQL DISTINCT question

I'm pretty sure it applies to all columns: it looks for identical rows according to the documentation

Offline

#5 2009-01-21 05:38:02

MattF
Member
From: South Yorkshire, England
Registered: 2008-05-06
Posts: 1,230
Website

Re: SQL DISTINCT question

Sweet. That'll make things a lot easier then. I must admit, I'd interpreted it the same way as Elbekko, that only the first column applied.


Screw the chavs and God save the Queen!

Offline

#6 2009-01-21 07:46:54

horus
Member
From: Italy
Registered: 2008-05-13
Posts: 59

Re: SQL DISTINCT question

I don't know PgSQL but in other db (I know quite well oracle and mysql) distinct is applied to all column (as sayd by Smartys) and ensure that there is no duplicate rows. I think that all db engine do the same.

Offline

#7 2009-01-21 12:12:38

elbekko
Former Developer
From: Leuven, Belgium
Registered: 2008-04-30
Posts: 1,131
Website

Re: SQL DISTINCT question

Hrmm, I must've remembered wrong then, my bad tongue


Ben
SVN repository for my extensions - The thread
Quickmarks 0.5
“Question: How does a large software project get to be one year late? Answer: One day at a time!” - Fred Brooks

Offline

#8 2009-02-04 13:55:58

php.web
Member
Registered: 2009-01-27
Posts: 4
Website

Re: SQL DISTINCT question

select distinct T.FLD1,T.FLD2, T.FLD3 workds fine for me.

Offline

#9 2009-02-04 15:05:01

MattF
Member
From: South Yorkshire, England
Registered: 2008-05-06
Posts: 1,230
Website

Re: SQL DISTINCT question

Cheers for the advice on this one gents. smile I had completely misinterpreted the documentation on it. I can alter that query now without wondering whether it's going to be less selective or not.

Thanks again. smile


Screw the chavs and God save the Queen!

Offline

#10 2009-03-23 16:39:09

MattF
Member
From: South Yorkshire, England
Registered: 2008-05-06
Posts: 1,230
Website

Re: SQL DISTINCT question

It would appear that elbekko and I were correct. Just been working on something totally unrelated, but if I put a COUNT(DISTINCT([first column])) on a similar query, the COUNT can be drastically different to the overall number of rows returned. It would appear, (unless I've missed some point completely), to work on the first column only in MySQL. Any additional columns make it return all results matching all criteria.

The intention is to only return results where the id, (first column), is unique. Adding extra columns just returns all matches which incorporate the extra columns whether the id is distinct or not.

Last edited by MattF (2009-03-23 16:42:10)


Screw the chavs and God save the Queen!

Offline

#11 2009-03-23 17:20:28

Smartys
Former Developer
Registered: 2008-04-27
Posts: 3,117
Website

Re: SQL DISTINCT question

SELECT DISTINCT is not the same as SELECT COUNT(DISTINCT col_name)).

http://dev.mysql.com/doc/refman/5.0/en/ … ation.html
Check out the equivalent group by statement they mention.

http://dev.mysql.com/doc/refman/5.0/en/ … t-distinct
As you can see, very different

Offline

#12 2009-03-23 17:40:21

MattF
Member
From: South Yorkshire, England
Registered: 2008-05-06
Posts: 1,230
Website

Re: SQL DISTINCT question

It demonstrates the results difference nicely though. smile

Doing the query both ways returns a different result.

For example, the particular thing I'm working on at the moment:


SELECT DISTINCT id1 FROM [tablename] WHERE id1=1;

returns 890 rows.


SELECT DISTINCT id1, id2, id3 FROM [tablename] WHERE id1=1;

returns 899 rows. There are nine rows where id1 isn't distinct.


Screw the chavs and God save the Queen!

Offline

#13 2009-03-23 17:48:10

MattF
Member
From: South Yorkshire, England
Registered: 2008-05-06
Posts: 1,230
Website

Re: SQL DISTINCT question

Damn. It needs the GROUP BY clause as well. It was in one of those links you posted Smartys. Cheers. smile

This query does return the expected result:

SELECT DISTINCT id1, id2, id3 FROM [tablename] WHERE id1=1 GROUP BY id1;

Edit: Just for future reference.

In MySQL, it appears to return the expected result with just the GROUP BY. The use of DISTINCT appears moot.

In PgSQL, it appears to need both GROUP BY and DISTINCT, literally as above. Something along the lines of:

SELECT DISTINCT id1 FROM [tablename] WHERE id1=1 GROUP BY id1, id2, id3;

Last edited by MattF (2009-03-23 18:23:30)


Screw the chavs and God save the Queen!

Offline

#14 2009-03-23 18:34:13

elbekko
Former Developer
From: Leuven, Belgium
Registered: 2008-04-30
Posts: 1,131
Website

Re: SQL DISTINCT question

pgSQL is annoying when it comes to group by (and having, and ...).


Ben
SVN repository for my extensions - The thread
Quickmarks 0.5
“Question: How does a large software project get to be one year late? Answer: One day at a time!” - Fred Brooks

Offline

#15 2009-03-23 18:43:48

MattF
Member
From: South Yorkshire, England
Registered: 2008-05-06
Posts: 1,230
Website

Re: SQL DISTINCT question

elbekko wrote:

pgSQL is annoying when it comes to group by (and having, and ...).

It's quirky, I'll definitely give it that. big_smile On that note, I've just been playing around a bit more, and to return multiple columns whilst keeping the id1 distinct, the query needs to be similar to:

SELECT DISTINCT DISTINCT ON (id1) id1, id2, id3 FROM [tablename] WHERE id1=1 GROUP BY id1, id2, id3;

Every variation one tries returns completely different results. big_smile big_smile

This one only appears to work correctly if you're returning a single column:

SELECT DISTINCT id1 FROM [tablename] WHERE id1=1 GROUP BY id1, id2, id3;

If you try it as:

SELECT DISTINCT id1, id2, id3 FROM [tablename] WHERE id1=1 GROUP BY id1, id2, id3;

you lose the exclusivity, hence the ON statement when returning more than one column. Well, at least if anyone comes across this thread in the future, they should have the answer to work with. big_smile big_smile


Screw the chavs and God save the Queen!

Offline

Board footer

Powered by FluxBB 1.4.8