用两列oracle找到排名

时间:2014-06-23 06:24:47

标签: sql oracle

我有以下查询来查找排名

SELECT a.appName,RANK() OVER (PARTITION BY a.appName, b.depName ORDER BY a.appName) 
as RANK,b.depName,Count(distinct a.userName) as visitors FROM tbl_app_webstats a 
inner join tbl_users b on  b.userName = a.userName 
where a.date_time between '01-JUN-12' and '20-JUN-14' and a.appName like '%' 
group by a.appName,b.depName order by a.appName;

但它给我的结果不正确

appName            RANK  depName   visitors
app1                1     dep1      1
app1                1     dep2      1
app1                1     dep3      2
app2                1     dep1      3
app2                1     dep2      1
app2                1     dep3      5
app3                1     dep2      2
app3                1     dep5      8
app4                1     dep1      2
app4                1     dep5      13
app5                1     dep5      2

结果sholuld就像这样

appName            RANK  depName   visitors
app1                1     dep1      1
app1                2     dep2      1
app1                3     dep3      2
app2                1     dep1      3
app2                2     dep2      1
app2                3     dep3      5
app3                1     dep2      2
app3                2     dep5      8
app4                1     dep1      2
app4                2     dep5      13
app5                1     dep5      2

知道这里可能有什么问题吗?

3 个答案:

答案 0 :(得分:1)

也许是这样的:

SELECT a.appName,RANK() OVER (PARTITION BY a.appName ORDER BY b.depName) 
......

你也可以这样做:

SELECT a.appName,ROW_NUMBER() OVER(PARTITION BY a.appName ORDER BY b.depName)
......

发表评论。你可以这样做:

WITH CTE
AS
(
    SELECT RANK() OVER(PARTITION BY appName ORDER BY depName) AS Rank
    ....
)
SELECT
    *
FROM
    CTE
WHERE
    CTE.Rank<=10;

答案 1 :(得分:1)

尝试以下查询:

SELECT a.appName,
RANK() OVER (PARTITION BY a.appName ORDER BY b.depName) 
as RANK,
b.depName,Count(distinct a.userName) as visitors FROM tbl_app_webstats a 
inner join tbl_users b on  b.userName = a.userName 
where a.date_time between '01-JUN-12' and '20-JUN-14' and a.appName like '%' 
group by a.appName,b.depName order by a.appName;

编辑:获得前10名

SELECT * FROM
(
    SELECT a.appName,
    RANK() OVER (PARTITION BY a.appName ORDER BY b.depName) 
    as RANK,
    b.depName,Count(distinct a.userName) as visitors FROM tbl_app_webstats a 
    inner join tbl_users b on  b.userName = a.userName 
    where a.date_time between '01-JUN-12' and '20-JUN-14' and a.appName like '%' 
    group by a.appName,b.depName order by a.appName
) WHERE RANK <=10
order by appName;

答案 2 :(得分:1)

你想要使用CTE方法的十大手段。 尝试仅分区单列a.appName

假设您使用分区两列意味着(

app1                     dep1      =1st Statement Combination is 1
app1                     dep2      =2nd Statement Combination is 1
app1                     dep3      =3rd Statement Combination is 1



  With CTE(A_Name,A_Rank,A_DepName,A_Visitor)
  AS
  (

    SELECT a.appName,RANK() OVER (PARTITION BY a.appName  ORDER BY a.appName) 
    as RANK,b.depName,Count(distinct a.userName) as visitors FROM tbl_app_webstats a 
    inner join tbl_users b on  b.userName = a.userName 
    where a.date_time between '01-JUN-12' and '20-JUN-14' and a.appName like '%' 
    group by a.appName,b.depName order by a.appName
)
Select * from CTE where A_Rank <=10