前任:
Date - up,
1/2 - 1.1.127.0 ,
1/3 - 1.1.127.1,
1/3 - 1.1.127.0,
1/4 - 1.1.127.3,
1/4 - 1.1.127.5,
1/5 - 1.1.127.3,
輸出:
Date-count,
1/2 - 1,
1/3 - 1,
1/4 - 2,
1/5 -0
每天登錄新的唯一 ip
uj5u.com熱心網友回復:
您想計算某個日期存在多少 IP,而該日期在前一個日期沒有出現。您想為此使用分析函式。
新 ID 的數量是一個日期的不同 ID 的總數減去前一個日期的數量。為了得到這個,首先選擇每行的運行計數。然后聚合每個日期以獲得每個日期的不同 ID 數量。然后使用LAG來獲得每天的差額。
select
date,
max(cnt) - lag(max(cnt)) over (order by date) as new_ips
from
(
select date, count(distinct ip) over (order by date) as cnt
from mytable
) running_counts
group by date
order by date;
沒有分析函式也是如此,這可能更具可讀性:
select date, count(distinct ip) as cnt
from mytable
where not exists
(
select null
from mytable before
where before.date < mytable.date
and before.id = mytable.id
)
group by date
order by date;
如果表DISTINCT中沒有重復項(具有相同日期和 IP 的兩行),則不需要后一個查詢。
uj5u.com熱心網友回復:
您還可以使用左連接使用以下解決方案。
with t (dt, ip) as (
select to_date( '1/2', 'MM/DD' ), '1.1.127.0' from dual union all
select to_date( '1/3', 'MM/DD' ), '1.1.127.1' from dual union all
select to_date( '1/3', 'MM/DD' ), '1.1.127.0' from dual union all
select to_date( '1/4', 'MM/DD' ), '1.1.127.3' from dual union all
select to_date( '1/4', 'MM/DD' ), '1.1.127.5' from dual union all
select to_date( '1/5', 'MM/DD' ), '1.1.127.3' from dual
)
select t.DT, count( decode(t2.IP, null, 1, null) ) cnt
from t
left join t t2
on ( t2.DT < t.DT and t2.IP = t.IP )
group by t.DT
order by 1
;
演示
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/418780.html
標籤:
