【问题标题】:country with the highest number of tests(oracle plsql)测试次数最多的国家(oracle plsql)
【发布时间】:2021-01-15 20:43:16
【问题描述】:

数据模型:

您好,我正在尝试获取“测试次数最多的国家/地区”。

查询:

我尝试使用一张桌子.. 好的...但是我如何使用“countryname”获得它?我应该如何使用内部连接来做到这一点?

【问题讨论】:

  • 顺便说一句,在我看来,简单地在“DATE”(DATE_) 中添加下划线以防止使用保留字并不是一个很好的命名约定。我希望将所有列命名为 adjective_noun。更具描述性/自我记录,并且没有机会尝试使用保留词或关键字。我还认为,单个测试将具有足够的属性来证明拥有自己的 TESTS 表是合理的,每个独特的测试都有一个独特的行。因此,存储“total_tests”是一个设计缺陷,因为它是从 TESTS 表中派生的。

标签: sql oracle analytic-functions


【解决方案1】:

按照你说的加入。

select s.countryname,
       s.date_,
       s.total_tests
from (select 
         row_number() over (order by a.total_tests desc) rn,
         a.date_,
         a.total_tests,
         c.countryname
      from cases_by_countries a join country c 
        on c.countryid = a.country_id       
     ) s
where s.rn = 1;     
       

【讨论】:

    【解决方案2】:

    如果你只需要最高的,你应该试试这个

    select c.CountryID, ts.Total_Tests
    from Country c
        inner join (
        select top(1) Country_ID, Total_Tests
        from CASES_BY_COUNTRIES
        order by Total_Tests desc
    ) ts on c.CountryID = ts.Country_ID
    

    【讨论】:

    • "top" 命令在 mssql 中使用.. 也许它工作.. 谢谢
    猜你喜欢
    • 1970-01-01
    • 2018-10-16
    • 2015-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多