<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Forum PostgreSQL - select distinct ... where]]></title>
		<link>https://forum.postgresql.org.pl/viewtopic.php?id=1184</link>
		<description><![CDATA[Najświeższe odpowiedzi w select distinct ... where.]]></description>
		<lastBuildDate>Thu, 15 Dec 2011 16:05:11 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Odp: select distinct ... where]]></title>
			<link>https://forum.postgresql.org.pl/viewtopic.php?pid=3322#p3322</link>
			<description><![CDATA[Wystarczy:

[code]Select id_tab2 From tab1 Where suma1 + suma2 + suma3 > 0 And rok = 2012 And id_tab3 = 8 Group By id_tab2;[/code]

Ew. możesz użyć distincta (w wersji, której używam teraz tj. 8.1 robi dodatkowo iimplicit sortowanie), poniżej porównanie dla 100 000 losowych wierszy:

[code]Explain Analyze Select Distinct id_tab2 From tab1 Where suma1 + suma2 + suma3 > 0 And rok = 2012 And id_tab3 = 8;
                                                     QUERY PLAN                                                     
--------------------------------------------------------------------------------------------------------------------
 Unique  (cost=32898.61..32898.65 rows=1 width=4) (actual time=585.861..599.229 rows=21 loops=1)
   ->  Sort  (cost=32898.61..32898.63 rows=9 width=4) (actual time=585.858..594.046 rows=28907 loops=1)
         Sort Key: id_tab2
         ->  Seq Scan on tab1  (cost=0.00..32898.46 rows=9 width=4) (actual time=0.039..518.839 rows=28907 loops=1)
               Filter: ((((suma1 + suma2) + suma3) > 0) AND (rok = 2012) AND (id_tab3 = 8))
 Total runtime: 599.917 ms
(6 rows)

Explain Analyze Select id_tab2 From tab1 Where suma1 + suma2 + suma3 > 0 And rok = 2012 And id_tab3 = 8 Group By id_tab2 Order by id_tab2;
                                                     QUERY PLAN                                                     
--------------------------------------------------------------------------------------------------------------------
 Sort  (cost=32898.51..32898.51 rows=1 width=4) (actual time=527.562..527.564 rows=21 loops=1)
   Sort Key: id_tab2
   ->  HashAggregate  (cost=32898.49..32898.50 rows=1 width=4) (actual time=527.532..527.539 rows=21 loops=1)
         ->  Seq Scan on tab1  (cost=0.00..32898.46 rows=9 width=4) (actual time=0.037..517.137 rows=28907 loops=1)
               Filter: ((((suma1 + suma2) + suma3) > 0) AND (rok = 2012) AND (id_tab3 = 8))
 Total runtime: 527.613 ms
(6 rows)

Explain Analyze Select id_tab2 From tab1 Where suma1 + suma2 + suma3 > 0 And rok = 2012 And id_tab3 = 8 Group By id_tab2;
                                                  QUERY PLAN                                                  
--------------------------------------------------------------------------------------------------------------
 HashAggregate  (cost=32898.49..32898.50 rows=1 width=4) (actual time=526.200..526.207 rows=21 loops=1)
   ->  Seq Scan on tab1  (cost=0.00..32898.46 rows=9 width=4) (actual time=0.039..515.914 rows=28907 loops=1)
         Filter: ((((suma1 + suma2) + suma3) > 0) AND (rok = 2012) AND (id_tab3 = 8))
 Total runtime: 526.250 ms
(4 rows)[/code]]]></description>
			<author><![CDATA[dummy@example.com (gszpetkowski)]]></author>
			<pubDate>Thu, 15 Dec 2011 16:05:11 +0000</pubDate>
			<guid>https://forum.postgresql.org.pl/viewtopic.php?pid=3322#p3322</guid>
		</item>
		<item>
			<title><![CDATA[select distinct ... where]]></title>
			<link>https://forum.postgresql.org.pl/viewtopic.php?pid=3321#p3321</link>
			<description><![CDATA[Mam przykładową tabele:

tab1

id
id_tab2
id_tab3
rok
suma1
suma2
suma3

i potrzebuję wybrać unikalne id_tab2 spełniające warunek, że suma1+suma2+suma3>0 where (przykładowo)
rok = 2012 and id_tab3=8]]></description>
			<author><![CDATA[dummy@example.com (przesq)]]></author>
			<pubDate>Thu, 15 Dec 2011 11:46:36 +0000</pubDate>
			<guid>https://forum.postgresql.org.pl/viewtopic.php?pid=3321#p3321</guid>
		</item>
	</channel>
</rss>
