Re: [GENERAL] DISTINCT vs GROUP BY - was Re: is (not) distinct from

2017-03-03 Thread George Neuner
Hi David, On Sat, 4 Mar 2017 02:32:48 +1300, David Rowley wrote: >On 3 March 2017 at 18:26, George Neuner wrote: >> I know most people here don't pay much - or any - attention to >> SQLServer, however there was an interesting article recently regarding >> significant performance differences be

Re: [GENERAL] DISTINCT vs GROUP BY - was Re: is (not) distinct from

2017-03-03 Thread David Rowley
On 3 March 2017 at 18:26, George Neuner wrote: > I know most people here don't pay much - or any - attention to > SQLServer, however there was an interesting article recently regarding > significant performance differences between DISTINCT and GROUP BY as > used to remove duplicates. > > https://s

Re: [GENERAL] DISTINCT vs GROUP BY - was Re: is (not) distinct from

2017-03-03 Thread Sven R. Kunze
On 03.03.2017 06:26, George Neuner wrote: I know most people here don't pay much - or any - attention to SQLServer, however there was an interesting article recently regarding significant performance differences between DISTINCT and GROUP BY as used to remove duplicates. https://sqlperformance.c

Re: [GENERAL] DISTINCT in STRING_AGG

2015-11-30 Thread Sterpu Victor
Thank you. I think there is no native function that will solve this problem, intarray extension ​can't order as in the example query. I could write a new SQL function but I solved the problem with subqueries and works fine. I need order because I use nested trees. To_left and to_right are the

Re: [GENERAL] DISTINCT in STRING_AGG

2015-11-29 Thread Geoff Winkless
On 29 November 2015 at 20:51, ​I wrote: > Well you could look at the intarray ​extension and a combination of > array_agg, uniq() and > *​​string_to_array*: ​Mind blip, apologies, obviously I meant array_to_string :)​ ​Geoff​

Re: [GENERAL] DISTINCT in STRING_AGG

2015-11-29 Thread Geoff Winkless
On 29 November 2015 at 18:59, Sterpu Victor wrote: > I can't skip the ordering. > I'm sure aqjs3 is the one that produces the duplication. > I guess subqueries are the only option, like this: ​Well you could look at the intarray ​extension and a combination of array_agg, uniq() and string_to_ar

Re: [GENERAL] DISTINCT in STRING_AGG

2015-11-29 Thread Sterpu Victor
1399031" 1399031;"1399032,1399033" Is there a better way? I usualy try to avoid subqueries. -- Original Message -- From: "Geoff Winkless" To: "Sterpu Victor" Cc: "PostgreSQL General" Sent: 11/29/2015 6:42:18 PM Subject: Re: [GENERAL] DISTINCT

Re: [GENERAL] DISTINCT in STRING_AGG

2015-11-29 Thread Geoff Winkless
On 28 November 2015 at 18:35, Sterpu Victor wrote: > Can I make a distinct STRING_AGG? > This is my query : > SELECT atjs.id, STRING_AGG(CAST(aqjs1.id AS VARCHAR), ',' ORDER BY > aqjs1.to_left) AS children > FROM administration.ad_query_join_select atjs > JOIN administration.ad_query aq ON (aq.id

Re: [GENERAL] DISTINCT in STRING_AGG

2015-11-28 Thread John J. Turner
On Nov 28, 2015, at 1:35 PM, Sterpu Victor wrote: > Hello > > Can I make a distinct STRING_AGG? > This is my query : > SELECT atjs.id, STRING_AGG(CAST(aqjs1.id AS VARCHAR), ',' ORDER BY > aqjs1.to_left) AS children > FROM administration.ad_query_join_select atjs > JOIN administration.ad_query

Re: [GENERAL] DISTINCT ON changes sort order on its own it seems

2013-04-27 Thread Tom Lane
Alexander Reichstadt writes: > following a query: > SELECT DISTINCT ON (msgid) msgid FROM (SELECT refid_messages as msgid FROM > messagehistorywithcontent WHERE 1=1 AND > (lower(ARRAY_TO_STRING(ARRAY[login,firstname,lastname,content,msgtitle], ' > ')) LIKE '%gg%') ORDER BY messagekind DESC) as

Re: [GENERAL] DISTINCT ON changes sort order

2013-04-24 Thread Tom Lane
Alexander Reichstadt writes: > I think I solved it: > SELECT * FROM (SELECT DISTINCT ON(refid_messages) refid_messages as msgid, * > FROM messagehistorywithcontent WHERE > (lower(ARRAY_TO_STRING(ARRAY[login,firstname,lastname,content,msgtitle], ' > ')) LIKE '%gg%') ORDER BY refid_messages DES

Re: [GENERAL] DISTINCT ON changes sort order

2013-04-24 Thread Alexander Reichstadt
I think I solved it: SELECT * FROM (SELECT DISTINCT ON(refid_messages) refid_messages as msgid, * FROM messagehistorywithcontent WHERE (lower(ARRAY_TO_STRING(ARRAY[login,firstname,lastname,content,msgtitle], ' ')) LIKE '%gg%') ORDER BY refid_messages DESC) as foo ORDER BY messagekind ASC Tha

Re: [GENERAL] DISTINCT ON changes sort order

2013-04-24 Thread Kevin Grittner
Alexander Reichstadt wrote: > SELECT >   DISTINCT ON (msgid) > msgid >   FROM ( >  SELECT refid_messages as msgid >    FROM messagehistorywithcontent >    WHERE 1=1 >  AND >(lower(ARRAY_TO_STRING(ARRAY[login,firstname,lastname,content,msgtitle], ' ')) >LI

Re: [GENERAL] DISTINCT ON changes sort order

2013-04-24 Thread Shaun Thomas
On 04/24/2013 09:03 AM, Alexander Reichstadt wrote: The order is correct. Now from the outer SELECT I would expect then to get: 53 29 46 Please re-read the manual on DISTINCT ON. "SELECT DISTINCT ON ( expression [, ...] ) keeps only the first row of each set of rows where the given expressio

Re: [GENERAL] distinct values and count over window function

2012-11-27 Thread Rodrigo Rosenfeld Rosas
Em 27-11-2012 19:50, David Johnston escreveu: From: pgsql-general-ow...@postgresql.org [mailto:pgsql-general-ow...@postgresql.org] On Behalf Of Rodrigo Rosenfeld Rosas Sent: Tuesday, November 27, 2012 4:23 PM To: pgsql-general@postgresql.org Subject: [GENERAL] distinct values and count over windo

Re: [GENERAL] distinct values and count over window function

2012-11-27 Thread David Johnston
From: pgsql-general-ow...@postgresql.org [mailto:pgsql-general-ow...@postgresql.org] On Behalf Of Rodrigo Rosenfeld Rosas Sent: Tuesday, November 27, 2012 4:23 PM To: pgsql-general@postgresql.org Subject: [GENERAL] distinct values and count over window function Hello, this is my first post to thi

Re: [GENERAL] Distinct on a non-sort column

2011-11-06 Thread Cstdenis
On 11/5/2011 4:11 PM, Tom Lane wrote: Cstdenis writes: If I understand that you are proposing as select * from (select distinct on (user_id) * from stories as s order by user_id) as foo order by date_submitted desc limit 10; No, you always need to sort by *more

Re: [GENERAL] Distinct on a non-sort column

2011-11-05 Thread Tom Lane
Cstdenis writes: > If I understand that you are proposing as > select * from > (select distinct on (user_id) * from stories as s order > by user_id) as foo > order by date_submitted desc limit 10; No, you always need to sort by *more* columns than are listed in DISTI

Re: [GENERAL] Distinct on a non-sort column

2011-11-05 Thread Cstdenis
On 11/5/2011 12:49 PM, Tom Lane wrote: Cstdenis writes: I am trying to write a query that selects recent submissions (sorted by submission_date) but only selects the most recent one for each user_id. example query: /select distinct on (user_id) * from stories order by date_submitted desc limit

Re: [GENERAL] Distinct on a non-sort column

2011-11-05 Thread John R Pierce
On 11/05/11 11:39 AM, Cstdenis wrote: example query: /select distinct on (user_id) * from stories order by date_submitted desc limit 10;/ select user_id,max(date_submitted) from stories group by date_submitted; ? -- john r pierceN 37, W 122 santa cruz ca

Re: [GENERAL] Distinct on a non-sort column

2011-11-05 Thread Tom Lane
Cstdenis writes: > I am trying to write a query that selects recent submissions (sorted by > submission_date) but only selects the most recent one for each user_id. > example query: /select distinct on (user_id) * from stories order by > date_submitted desc limit 10;/ > However postgres will n

Re: [GENERAL] Distinct on a non-sort column

2011-11-05 Thread Tair Sabirgaliev
On Sun, Nov 6, 2011 at 12:39 AM, Cstdenis wrote: > I am trying to write a query that selects recent submissions (sorted by > submission_date) but only selects the most recent one for each user_id. > > example query: select distinct on (user_id) * from stories order by > date_submitted desc limit 1

Re: [GENERAL] DISTINCT ON without ORDER BY

2009-04-21 Thread Martijn van Oosterhout
On Tue, Apr 21, 2009 at 12:11:26PM +, Jasen Betts wrote: > > Is there a way to acheive the above result without a sort and without a > > self-join? > > anyway you could possibly write an agregate function that returns a > copy of the row with the highest id? Put that way it sounds like someth

Re: [GENERAL] DISTINCT ON without ORDER BY

2009-04-21 Thread Jasen Betts
On 2009-04-19, Martijn van Oosterhout wrote: > Hi, > > I was going through the queries of an SQL application and came across > queries like: > > SELECT * FROM foo > WHERE id in (SELECT max(id) FROM foo GROUP BY bar); > > I thought, here's a case where this could be better written using > DISTINCT

Re: [GENERAL] DISTINCT ON without ORDER BY

2009-04-20 Thread Gregory Stark
Martijn van Oosterhout writes: > SELECT * FROM foo > WHERE id in (SELECT max(id) FROM foo GROUP BY bar); > > Is there a way to acheive the above result without a sort and without a > self-join? Something like SELECT bar, (magic_agg_func(foo)).* FROM foo GROUP BY bar where you define an aggrega

Re: [GENERAL] DISTINCT is not quite distinct

2006-11-06 Thread Florian Weimer
* Richard Huxton: > I take it SELECT DISTINCT bar... shows the same problem? SELECT bar FROM baz does *not* show the duplicate row. > If so, can you do: > SELECT OID,xmin,cmin,xmax,cmax,bar FROM baz > WHERE bar = Even if I force a complete index scan, I get xmin = 1007617 for both rows, the

Re: [GENERAL] DISTINCT is not quite distinct

2006-11-06 Thread Florian Weimer
* Tom Lane: >> According to EXPLAIN, an index scan on the bar column is used (using >> the underlying B-tree index). > > Do you mean an indexscan followed immediately by a Unique node? If > so, yeah, that would depend entirely on correct ordering of the > indexscan output to produce distinct resu

Re: [GENERAL] DISTINCT is not quite distinct

2006-11-06 Thread Tom Lane
Florian Weimer <[EMAIL PROTECTED]> writes: > I run this innocent query > CREATE TABLE foo AS SELECT DISTINCT bar FROM baz ORDER BY bar; > and the resulting table contains duplicate rows. 8-( > According to EXPLAIN, an index scan on the bar column is used (using > the underlying B-tree index). Do

Re: [GENERAL] DISTINCT is not quite distinct

2006-11-06 Thread Richard Huxton
Florian Weimer wrote: I run this innocent query CREATE TABLE foo AS SELECT DISTINCT bar FROM baz ORDER BY bar; and the resulting table contains duplicate rows. 8-( According to EXPLAIN, an index scan on the bar column is used (using the underlying B-tree index). This is with PostgreSQL 8.1.4

Re: [GENERAL] DISTINCT to get distinct *substrings*?

2006-08-08 Thread Tom Lane
Christoph Pingel <[EMAIL PROTECTED]> writes: > Am 08.08.2006 um 19:49 schrieb Nikolay Samokhvalov: >> don't use "DISTINCT ON" at all, it's evil :-) (why? >> http://chernowiki.ru/index.php?node=38#A13 > Thanks for the good advice! From reading this, it seems to be a > *really* bad thing. And I di

Re: [GENERAL] DISTINCT to get distinct *substrings*?

2006-08-08 Thread Christoph Pingel
Thanks for the input, I think I get this now. In my case, the querySELECT DISTINCT ON (substring(attribute from '^http://[^/]*/')) attribute from pg_atp where attribute like 'http://%' doesn't get me just the root of the URL, but the whole URL - but only for the first row for each individual root.

Re: [GENERAL] DISTINCT to get distinct *substrings*?

2006-08-08 Thread Christoph Pingel
Am 08.08.2006 um 19:49 schrieb Nikolay Samokhvalov: SELECT DISTINCT substring(attribute from '^http://[^/]*/') from pg_atp where attribute like 'http://%'; w/o DISTINCT there should be duplicates (if any) don't use "DISTINCT ON" at all, it's evil :-) (why? http://chernowiki.ru/index.php?node=

Re: [GENERAL] DISTINCT to get distinct *substrings*?

2006-08-08 Thread Ben
DISTINCT ON is extremely useful when you know what you're doing. It's postgres' version of oracle's first_value analytical function, and when you need it, nothing else really suffices. On Tue, 8 Aug 2006, Nikolay Samokhvalov wrote: SELECT DISTINCT substring(attribute from '^http://[^/]*/') fr

Re: [GENERAL] DISTINCT to get distinct *substrings*?

2006-08-08 Thread Nikolay Samokhvalov
SELECT DISTINCT substring(attribute from '^http://[^/]*/') from pg_atp where attribute like 'http://%'; w/o DISTINCT there should be duplicates (if any) don't use "DISTINCT ON" at all, it's evil :-) (why? http://chernowiki.ru/index.php?node=38#A13) On 8/8/06, Christoph Pingel <[EMAIL PROTECTED]

Re: [GENERAL] distinct not working in a multiple join

2006-02-10 Thread David Rio Deiros
On Thu, Feb 09, 2006 at 09:51:13PM -0500, Stephen Frost wrote: > * David Rio Deiros ([EMAIL PROTECTED]) wrote: > > Now I have to redefine my query because I want to get the second > > output but keeping the group_id. Ideas and suggestions are welcome. > > You might want to look at 'distinct on'.

Re: [GENERAL] distinct not working in a multiple join

2006-02-09 Thread Stephen Frost
* David Rio Deiros ([EMAIL PROTECTED]) wrote: > Now I have to redefine my query because I want to get the second > output but keeping the group_id. Ideas and suggestions are welcome. You might want to look at 'distinct on'. Stephen signature.asc Description: Digital signature

Re: [GENERAL] distinct not working in a multiple join

2006-02-09 Thread David Rio Deiros
On Thu, Feb 09, 2006 at 07:20:19PM -0500, Tom Lane wrote: > David Rio Deiros <[EMAIL PROTECTED]> writes: > > I have some issues with the query attached at the end of this email. > > If I run that query I got this output ( I have removed some of the > > fields) despite the distinct clause: > > > Q

Re: [GENERAL] distinct not working in a multiple join

2006-02-09 Thread Tom Lane
David Rio Deiros <[EMAIL PROTECTED]> writes: > I have some issues with the query attached at the end of this email. > If I run that query I got this output ( I have removed some of the > fields) despite the distinct clause: > QC Q&A | www.xxx.com | 44281 > QC Q&A | www.xxx.com | 44281 > WhyMAX?

Re: [GENERAL] DISTINCT ON

2005-11-18 Thread Jeremy Palmer
Sorry I posted this to the wrong list. I have now reposted this is pgsql-sql. -Original Message- From: Jeremy Palmer [mailto:[EMAIL PROTECTED] Sent: Saturday, 19 November 2005 11:05 a.m. To: 'pgsql-general@postgresql.org' Subject: DISTINCT ON Hi, I have a table: observation ( id in

Re: [GENERAL] 'distinct on' and 'order by' conflicts of interest

2004-12-31 Thread Bruno Wolff III
On Fri, Dec 31, 2004 at 15:02:56 -0600, [EMAIL PROTECTED] wrote: > > I've put an '*' next to the rows I want. So my dilemma is two part. > First, I want to sort by the ordinal information only when the arc is > pointing from the source object (id 638) to the other objects. Well, it's > pretty

Re: [GENERAL] 'distinct on' and 'order by' conflicts of interest

2004-12-31 Thread stephen
> Define the problem, not how you think it should be solved. What > are you trying to do? If you can't get the query to work, then > please post SQL statements to create and populate a table and > describe the query results you'd like to see. the situation is i have a set of records in a table (

Re: [GENERAL] 'distinct on' and 'order by' conflicts of interest

2004-12-31 Thread Stephan Szabo
On Fri, 31 Dec 2004 [EMAIL PROTECTED] wrote: > It has come up several times on the various postgresql lists that in order > to get around the requirement of DISTINCT ON parameters matching the first > ORDER BY parameters, wrap the distinct query in a new 'order by' query: > > select * from (selec

Re: [GENERAL] 'distinct on' and 'order by' conflicts of interest

2004-12-31 Thread Michael Fuhr
On Fri, Dec 31, 2004 at 10:48:21AM -0600, [EMAIL PROTECTED] wrote: > It has come up several times on the various postgresql lists that in order > to get around the requirement of DISTINCT ON parameters matching the first > ORDER BY parameters, wrap the distinct query in a new 'order by' query: >

Re: [GENERAL] distinct

2000-10-09 Thread Tom Lane
Stephan Szabo <[EMAIL PROTECTED]> writes: > There really isn't a spec way to do it, however, > select distinct on (col1) col1, col2, col3, col4 from tablename; > would get you one row for each distinct col1, but it's undefined what > row that gets you. You can get a well-defined result with DIS