我想從基於兩列的表中返回一組唯一記錄以及最近的發佈時間和這些組合的總次數在他們的輸出記錄之前(及時)出現了兩列。爲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
幫助的人?
樣本數據和期望的結果將有助於澄清問題。 –