【问题标题】:Get the second largest value of column B if column A value is null for the largest value of column B如果 A 列的值对于 B 列的最大值为空,则获取 B 列的第二大值
【发布时间】:2019-10-17 03:44:02
【问题描述】:
情况1 表 AB A列 B列 1 4 2 5 3 6 案例2 表 AB A列 B列 1 4 2 5 空 6

对于案例 1,我需要值 6,我将从 AB 中选择 max(columnB)。 对于情况 2,我需要值 5 因为 max(columnB) 是 6 在 columnA 中有一个 NULL 值,所以我想要 columnB 的第二大值是 5 并且它对应的 columnA 值不为空。只有 columnB 的最大值在 columnA 中才会有 NULL 值。

【问题讨论】:

    标签: sql sql-server-2008


    【解决方案1】:

    这将在两种情况下都返回预期的结果:

    select max(columnB) from tableAB where columnA is not null
    

    查询语句为:获取columnB的最大值,其中columnA不为空。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-05
      • 1970-01-01
      • 1970-01-01
      • 2014-04-18
      • 1970-01-01
      相关资源
      最近更新 更多