psql 查询计数应该是条件下的全局字段
psql query count should be global field on condition
Table 架构和数据:
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
到return:
txt count min count
b 1 March, 03 2016 00:00:00 1
a 3 March, 03 2016 00:00:00 3
我需要来自具有相同 txt 的行的部分 count 和来自 nall[=37 的行的日期=] 不同于 2.
但保持 returning 从整个 table:
计数
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
这是解决方案:
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
Table 架构和数据:
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
到return:
txt count min count b 1 March, 03 2016 00:00:00 1 a 3 March, 03 2016 00:00:00 3
我需要来自具有相同 txt 的行的部分 count 和来自 nall[=37 的行的日期=] 不同于 2.
但保持 returning 从整个 table:
计数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
这是解决方案:
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