select *
from stat.cause_code_descriptions
INNER JOIN stat.stat_dial
on stat.cause_code_descriptions.CODE = stat.stat_dial.cause
where called like '%3623.1348'
and begdt > sysdate-500
and rownum <=100
order by begdt desc
此查询返回类似
的内容login code called begdtu(unix timestamp)
oeanwel 4 VLAN:3623.1348 1336383493
oe192034 0 VLAN:3623.1348 1336382883
oe192034 2 VLAN:3623.1348 1336382640
oe192034 45 VLAN:3623.1348 1336380257
oeanwel 4 VLAN:3623.1348 1336379883
oe220850 20 VLAN:3623.1348 1336378666
oe194752 4 VLAN:3623.1348 1336378507
oeanna2510 45 VLAN:3623.1348 1336377516
oeanwel 4 VLAN:3623.1348 1336376273
oe237185 45 VLAN:3623.1348 1336374506
oe237185 4 VLAN:3623.1348 1336372662
oe237185 3 VLAN:3623.1348 1336370819
oe239364 3 VLAN:3623.1348 1336367329
oeanna2510 45 VLAN:3623.1348 1336366115
我正在寻找的是返回任何login
的最后(最新)记录。
对于不重复的记录,我的查询效果很好,但对于oe192034
和oe237185
登录,它会显示所有记录。
我尝试了group by
和distinct
,但它不起作用。请帮忙
答案 0 :(得分:1)
你需要一个窗口函数ROW_NUMBER:
select *
from
(
select ccd.*, sd.*, row_number() over (partition by login order by begdtu desc) rn
from stat.cause_code_descriptions ccd
INNER JOIN stat.stat_dial sd
on ccd.CODE = sd.cause
where called like '%3623.1348'
) dt
where rn = 1
order by begdt desc
答案 1 :(得分:0)
SELECT *
FROM (
select *,ROW_NUMBER() OVER (PARTITION BY login ORDER BY begdt DESC) RN
from stat.cause_code_descriptions
INNER JOIN stat.stat_dial on stat.cause_code_descriptions.CODE = stat.stat_dial.cause
where called like '%3623.1348' and begdt > sysdate-500 and rownum <=100 )
WHERE RN=1;
答案 2 :(得分:0)
试试这个:
SELECT
LOGIN,
CODE,
CALLED,
BEGDTU
FROM
(SELECT
LOGIN,
CODE,
CALLED,
BEGDTU,
FIRST_VALUE ( LOGIN )
OVER (PARTITION BY CODE, CALLED ORDER BY BEGDTU DESC)
AS FIRST_ROW_ID
FROM
STAT.CAUSE_CODE_DESCRIPTIONS
INNER JOIN
STAT.STAT_DIAL
ON STAT.CAUSE_CODE_DESCRIPTIONS.CODE = STAT.STAT_DIAL.CAUSE
WHERE
CALLED LIKE '%3623.1348'
AND FIRST_ROW_ID = 1)
WHERE
BEGDT > SYSDATE
- 500
AND ROWNUM <= 100
ORDER BY
BEGDT DESC