繁体   English   中英

Oracle SQL 查找流行查询

[英]Oracle SQL find popular query

我在 SQL 中有一个名为 Questionnaire 的表,其中有名为 ID、Newspaper 和 CreditCards 的列。 我需要输出至少有 3 张信用卡的 ID 中最受欢迎的报纸。

例子:

ID       Credit Cards    Newspaper
----------------------------------------
10354    3               The Independent 
12154    4               The Independent 
11354    2               The Times 
14587    3               The Daily Mail 
19874    5               The Sunday news 
16847    1               The Independent

你能帮忙用一个 sql 命令来输出上面提到的查询吗?

select *
from (
    select newspaper, 
           rank() over (order by count(*) desc) as rnk
    from Questionnaire
    where credit_cards >= 3
    group by newspaper
) t
where rnk = 1

如果两份报纸具有相同的“流行度”,则两者都将被退回。

SQLFiddle 演示: http ://sqlfiddle.com/#!4/16dcb/1

如果您只想获取最流行的 Newspaper[s],那么这可以解决查询。

select 
    newspaper
    , count(1) as fct 
from 
  Questionnaire 
where 
  CreditCards >= 3
group by newspaper 
having fct = 
            (
              select 
                max(ct) 
              from 
                ( 
                    select 
                        newspaper 
                        ,count(1) as ct 
                    from 
                        Questionnaire 
                    where 
                        CreditCards >= 3 
                    group by newspaper
                )
             )
/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM