<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Forum PostgreSQL - Pomoc w napisaniu zaytania]]></title>
	<link rel="self" href="http://forum.postgresql.org.pl/extern.php?action=feed&amp;tid=1464&amp;type=atom"/>
	<updated>2013-02-26T08:42:00Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.postgresql.org.pl/viewtopic.php?id=1464</id>
		<entry>
			<title type="html"><![CDATA[Odp: Pomoc w napisaniu zaytania]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=4072#p4072"/>
			<content type="html"><![CDATA[Działa :-)]]></content>
			<author>
				<name><![CDATA[tomek]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=1459</uri>
			</author>
			<updated>2013-02-26T08:42:00Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=4072#p4072</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Odp: Pomoc w napisaniu zaytania]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=4071#p4071"/>
			<content type="html"><![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]]></content>
			<author>
				<name><![CDATA[c_michal]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=627</uri>
			</author>
			<updated>2013-02-25T15:53:43Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=4071#p4071</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Odp: Pomoc w napisaniu zaytania]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=4069#p4069"/>
			<content type="html"><![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]]]></content>
			<author>
				<name><![CDATA[tomek]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=1459</uri>
			</author>
			<updated>2013-02-25T14:52:32Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=4069#p4069</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Pomoc w napisaniu zaytania]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=4068#p4068"/>
			<content type="html"><![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 :-( ?]]></content>
			<author>
				<name><![CDATA[tomek]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=1459</uri>
			</author>
			<updated>2013-02-25T12:30:50Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=4068#p4068</id>
		</entry>
</feed>
