【问题标题】:Select atribute with max value选择具有最大值的属性
【发布时间】:2021-03-29 20:34:52
【问题描述】:

如何使用子查询在 2 个日期之间选择具有最大雇主数的公司名称,主要问题是我有 2 个相同的日期和 2 个最大值,我只需要在子查询中取第一个

select Company.CompanyName from Company 
where FoundingDate between (select top (1) with ties FoundingDate
from Company order by FoundingDate desc) and '2020-11-30' 
group by Company.CompanyName

【问题讨论】:

  • 样本数据和期望的结果会有所帮助。

标签: sql tsql select sql-order-by sql-limit


【解决方案1】:
select * from Company 
where FoundingDate between (select top (1) with ties FoundingDate
from Company order by FoundingDate desc) and '2020-11-30' 
group by Company.Id, Company.CompanyName, Company.DirectorName, Company.EmplCount,    Company.FoundingDate

select * from Company
where FoundingDate between (select max(FoundingDate) from Company where EmplCount = (select max(EmplCount) from Company)) and '2020-11-30'

2种答案

【讨论】:

    【解决方案2】:

    如果您每个创建日期只有一行,那么只需 order bytop

    select top (1) with ties FoundingDate, EmplCount
    from Company 
    order by EmplCount desc
    

    如果您每个日期有不止一行,并且您想对相应的计数求和,那么您也需要聚合:

    select top (1) with ties FoundingDate, sum(EmplCount) as sumEmplCount
    from Company 
    group by FoundingDate
    order by sumEmplCount desc
    

    【讨论】:

    • 我为这个不完整的问题道歉,我想通过子查询select Company.CompanyName from Company where FoundingDate between (select top (1) with ties FoundingDate from Company order by FoundingDate desc) and '2020-11-30' group by Company.CompanyName
    • @SashaDrozdiuk:我不明白你的意思。如果上述查询没有达到您的要求,那么您可能希望edit your question 提供适当的样本数据和所需的结果,以阐明您的要求。
    猜你喜欢
    • 2017-10-03
    • 2010-10-29
    • 1970-01-01
    • 1970-01-01
    • 2013-07-20
    • 2021-08-10
    • 1970-01-01
    • 2021-05-19
    • 2015-01-14
    相关资源
    最近更新 更多