2017-01-22 4 views
0

表のスキーマとデータをグローバルフィールドにする必要があります。psqlのクエリーカウントは条件

CREATE TABLE aaa (
    nall integer NOT NULL, 
    ladata date NOT NULL, 
    txt text, 
    CONSTRAINT aaa_pkey PRIMARY KEY (nall, ladata) 
) 

データ:

2 | '2016-01-01' | 'a' 
2 | '2016-02-02' | 'a' 
5 | '2016-03-03' | 'a' 
6 | '2016-03-03' | 'b' 

問合せ:

select txt, 
     count(txt), 
     min(ladata), 
     (select count(txt) from aaa where txt !='') 
from aaa 
where ladata > (select MIN(ladata) from aaa) and nall != 2 
group by txt 

http://sqlfiddle.com/#!15/db06b1/1

返すために:

txt count min  count 
b  1 March, 03 2016 00:00:00  1 
a  3 March, 03 2016 00:00:00  3 
は私が TXT同じ を持つ行からの部分的と2

からnallの異なると行から日付を必要とするが、テーブル全体からのカウントを返す保ちます:

txt count min  count 
b  1 March, 03 2016 00:00:00  4 
a  1 March, 03 2016 00:00:00  4 

私はこれが解決策であるPostgreSQLの9.6

+0

あなたの質問では、クエリやデータを含めます。 –

+0

'where ladata>(aaaからのMIN(ladata)の選択)とnall!= 2'によると、クエリには2つのレコードを含める必要がありますか? – McNets

答えて

1

を使用しています:

select a.txt, count(a.txt), min(ladata) 
    , (select count(txt) from aaa b where b.txt =a.txt) 
from aaa a 
where ladata>(select MIN(ladata) from aaa) and nall != 2 
group by txt