我想从基于两列的表中返回一组唯一记录以及最近的发布时间和这些组合的总次数在他们的输出记录之前(及时)出现了两列。为SQL中的每个唯一组合列计算行
所以我想要得到的是沿着这些路线的东西:
select col1, col2, max_posted, count from T
join (
select col1, col2, max(posted) as posted from T where groupid = "XXX"
group by col1, col2) h
on (T.col1 = h.col1 and
T.col2 = h.col2 and
T.max_posted = h.tposted)
where T.groupid = 'XXX'
计数需要是次发生col1和col2上的每个组合前max_posted输出每个记录的数量。 (我希望我解释说,正确:)
编辑:在尝试下面的建议为:
select dx.*,
count(*) over (partition by dx.cicd9, dx.cdesc order by dx.tposted) as cnt
from dx
join (
select cicd9, cdesc, max(tposted) as tposted from dx where groupid ="XXX"
group by cicd9, cdesc) h
on (dx.cicd9 = h.cicd9 and
dx.cdesc = h.cdesc and
dx.tposted = h.tposted)
where groupid = 'XXX';
伯爵始终返回“1”。此外,您如何计算tposted
之前发生的记录?
这也失败了,但我希望你能得到在那里我当家:
WITH H AS (
SELECT cicd9, cdesc, max(tposted) as tposted from dx where groupid = 'XXX'
group by cicd9, cdesc),
J AS (
SELECT count(*) as cnt
FROM dx, h
WHERE dx.cicd9 = h.cicd9
and dx.cdesc = h.cdesc
and dx.tposted <= h.tposted
and dx.groupid = 'XXX'
)
SELECT H.*,J.cnt
FROM H,J
帮助的人?
样本数据和期望的结果将有助于澄清问题。 –