oracle SQL Server 2005中的分析函数

6rvt4ljy  于 2023-11-17  发布在  Oracle
关注(0)|答案(3)|浏览(167)

我有一个查询,它在Oracle中工作,但在SQL Server 2005中不工作.

select user_name
       , url
       , count(*)
       ,first_value(count(*)) over (partition by user_name 
                                    order by count(*) desc) max_total
from urls
group by user_name, url
order by max_total desc,user_name

字符串
结果:

jc3wubiy

jc3wubiy1#

因为我的评论很大,我会把它作为答案写下来:(
@tanging它不是很正确,但有一个正确的路径...也许我已经解决了这个问题的查询,但我想它的分析功能....我的查询是这样的

select urls.user_name
       ,urls.url
       ,count(*) ct
       ,max_amount 
from urls
     ,(select user_name
              ,max(amount) max_amount
       from (select user_name
                    ,url
                    ,count(*) amount
             from urls
             group by user_name,url)  t1
       group by user_name) t2
where urls.user_name=t2.user_name
group by urls.user_name,urls.url,max_amount
order by max_amount desc,urls.user_name,ct desc

字符串

lf3rwulv

lf3rwulv2#

@tanging这是测试数据。

create table urls(
user_name varchar2(100),
url varchar2(100)
);

insert into urls
values('mariami','google.com');
insert into urls
values('mariami','google.com');
insert into urls
values('mariami','google.com');
insert into urls
values('giorgi','google.com');
insert into urls
values('giorgi','google.com');
insert into urls
values('giorgi','facebook.com');
insert into urls
values('giorgi','facebook.com');
insert into urls
values('giorgi','facebook.com');
insert into urls
values('giorgi','facebook.com');
insert into urls
values('mariami','facebook.com');
insert into urls
values('a','facebook.com');

字符串
查询结果为:x1c 0d1x
查询结果为:

sycxhyv7

sycxhyv73#

WITH    q AS
        (
        SELECT  user_name, url, COUNT(*) AS cnt
        FROM    urls
        GROUP BY
                user_name, url
        )
SELECT  *
FROM    q qo
CROSS APPLY
        (
        SELECT  TOP 1 cnt
        FROM    q qi
        WHERE   qi.user_name = qo.user_name
        ORDER BY
                cnt DESC
        ) qi

字符串

相关问题