<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Forum PostgreSQL - Pomoc w napisaniu zaytania]]></title>
		<link>https://forum.postgresql.org.pl/viewtopic.php?id=1464</link>
		<description><![CDATA[Najświeższe odpowiedzi w Pomoc w napisaniu zaytania.]]></description>
		<lastBuildDate>Tue, 26 Feb 2013 08:42:00 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Odp: Pomoc w napisaniu zaytania]]></title>
			<link>https://forum.postgresql.org.pl/viewtopic.php?pid=4072#p4072</link>
			<description><![CDATA[Działa :-)]]></description>
			<author><![CDATA[dummy@example.com (tomek)]]></author>
			<pubDate>Tue, 26 Feb 2013 08:42:00 +0000</pubDate>
			<guid>https://forum.postgresql.org.pl/viewtopic.php?pid=4072#p4072</guid>
		</item>
		<item>
			<title><![CDATA[Odp: Pomoc w napisaniu zaytania]]></title>
			<link>https://forum.postgresql.org.pl/viewtopic.php?pid=4071#p4071</link>
			<description><![CDATA[proszę

with tabela as (
select '2012-03-31' as termin,0.00 as kwota,1 as klient union all
select '2012-06-31',0.00,1 union all
select '2012-09-31',0.00,1 union all
select '2012-12-31',0.00,1 union all
select '2012-03-31',0.00,2 union all
select '2012-06-31',0.00,2 union all
select '2012-09-31',1.00,2 union all
select '2012-12-31',3.00,2 ) 
select 
min(case when kwota=0.0 then null else termin end) as termin
,klient
,sum(kwota) as kwota
from tabela
group by klient]]></description>
			<author><![CDATA[dummy@example.com (c_michal)]]></author>
			<pubDate>Mon, 25 Feb 2013 15:53:43 +0000</pubDate>
			<guid>https://forum.postgresql.org.pl/viewtopic.php?pid=4071#p4071</guid>
		</item>
		<item>
			<title><![CDATA[Odp: Pomoc w napisaniu zaytania]]></title>
			<link>https://forum.postgresql.org.pl/viewtopic.php?pid=4069#p4069</link>
			<description><![CDATA[Tzn. może jest jakieś inne sprytniejsze rozwiązanie niż takie:
[code]with h1 as ( select sum(kwota) as kwota, date(null) as termin, klient
             from tabelka
             group by klient
             having sum(kwota_raty)=0),
     h2 as (select sum(kwota) as kwota, min(termin) as termin, klient
            from tabelka
            where kwota != 0
            group by klient
            having sum(kwota)!=0),
     tab as (select * from h1
             union
             select * from h2)
select * from tab;[/code]]]></description>
			<author><![CDATA[dummy@example.com (tomek)]]></author>
			<pubDate>Mon, 25 Feb 2013 14:52:32 +0000</pubDate>
			<guid>https://forum.postgresql.org.pl/viewtopic.php?pid=4069#p4069</guid>
		</item>
		<item>
			<title><![CDATA[Pomoc w napisaniu zaytania]]></title>
			<link>https://forum.postgresql.org.pl/viewtopic.php?pid=4068#p4068</link>
			<description><![CDATA[Mam tabele:

[code]termin     | kwota | klient
-----------------------------
2012-03-31 | 0.00    |  1
2012-06-31 | 0.00    |  1
2012-09-31 | 0.00    |  1
2012-12-31 | 0.00    |  1

2012-03-31 | 0.00    |  2
2012-06-31 | 0.00    |  2
2012-09-31 | 1.00    |  2
2012-12-31 | 3.00    |  2[/code]

Chciałbym otrzymać wynik:

[code]termin     | kwota | klient
-----------------------------
null       | 0.00    |  1

2012-09-31 | 4.00    |  2[/code]

Czyli dla tych klientów którzy nie mają kwoty po prostu wpisać im sumę= 0 i datę = null,
a dla tych którzy mają jakąś kwotę wpisać sumę = suma pojedynczych kwot, a za datę wpisać datę pierwszej kwoty.

Na razie mam taki pomysł:

[code]
select termin sum(kwota) as kwota, klient
from tabela
group by klient[/code]
tylko że wtedy będę miał: [code]termin     | kwota | klient
-----------------------------
2012-01-31 | 0.00    |  1

2012-01-31 | 4.00    |  2[/code]

a jak dam:
[code]
select termin sum(kwota) as kwota, klient
from tabela
where kwota != 0 
group by klient[/code]
to będę miał: [code]termin     | kwota | klient
-----------------------------
2012-09-31 | 4.00    |  2[/code]

Czy jest jakiś sprytny pomysł aby to zrobić, czy trzeba zrobić dwie tabele: - jedną gdzie suma = 0, drugą gdzie suma != 0 i je później "union" ?


P.S.
Co to za spam się tu dostał na forum :-( ?]]></description>
			<author><![CDATA[dummy@example.com (tomek)]]></author>
			<pubDate>Mon, 25 Feb 2013 12:30:50 +0000</pubDate>
			<guid>https://forum.postgresql.org.pl/viewtopic.php?pid=4068#p4068</guid>
		</item>
	</channel>
</rss>
