【问题标题】:SQL Use lookup to return max in group by?SQL 使用查找返回分组中的最大值?
【发布时间】:2013-06-24 16:01:08
【问题描述】:

我正在尝试在状态列表中返回“最大值”。但是,我想为字符串值分配一个排序值,以便最大值由我自己的排名返回,而不是按字母顺序。

这是我的代码:

    select x.wbs1, x.wbs2, x.wbs3, x.custstatus
    from (
          select wbs1,wbs2,wbs3,custstatus=MAX(custstatus)
          from Projects_CRStatus
          where custsentdate >= 'June 1, 2001' AND custsentdate <= 'June 30, 2013'
          AND CustStatus IN ('RECEIVED AT VENDOR', 'CONFIRMATION SENT',
          'IC PENDING','CONFIRMATION RECEIVED','CANCELLED')
          group by wbs1,wbs2,wbs3 ) x
    inner join (
          select wbs1,wbs2,wbs3,custsentdate=max(custsentdate)
          from Projects_CRStatus
          group by wbs1,wbs2,wbs3) y
    on (x.wbs1=y.wbs1 and x.wbs2=y.wbs2 and x.wbs3=y.wbs3)

    ORDER BY CustEnrollmentID

我想做的是对 CustStatus 的值进行排名,这样我就不会返回 CustStatus 的最高字母结果,而是按此顺序获得最高级的状态。

  1. '在供应商处收到'
  2. '确认已发送'
  3. 'IC 待定'
  4. '收到确认'
  5. '取消'

【问题讨论】:

  • 澄清一下,我正在寻找 x 中每个 wbs1、wbs2、wbs3 分组中的排名靠前的状态。

标签: sql group-by max


【解决方案1】:

您使用了 rank 这个词,但我猜您实际上是在询问如何order 查询结果。如果是这样,您可以在ORDER BY 子句中使用CASE 表达式:

ORDER BY CASE WHEN CustStatus = 'RECEIVED AT VENDOR' then 1
              WHEN CustStatus = 'CONFIRMATION SENT' then 2
              WHEN CustStatus = 'IC PENDING' then 3
              WHEN CustStatus = 'CONFIRMATION RECEIVED' then 4
              WHEN CustStatus = 'CANCELLED' then 5
              ELSE 6
         END, CustEnrollmentID

CASE 表达式的最后一项(ELSE 条件)只是为了安全起见。

更新:根据您后续的 cmets,这里是一个使用 ROW_NUMBER 函数返回“顶级状态”的查询:

select wbs1, wbs2, wbs3, custstatus

from (
   select x.wbs1, x.wbs2, x.wbs3, x.custstatus,
      ROW_NUMBER () OVER(PARTITION BY x.wbs1, x.wbs2, x.wbs3 
                      ORDER BY CASE
                      WHEN x.CustStatus = 'RECEIVED AT VENDOR' then 1
                      WHEN x.CustStatus = 'CONFIRMATION SENT' then 2
                      WHEN x.CustStatus = 'IC PENDING' then 3
                      WHEN x.CustStatus = 'CONFIRMATION RECEIVED' then 4
                      WHEN x.CustStatus = 'CANCELLED' then 5
                      ELSE 6 END) as rn
   from (
      select wbs1,wbs2,wbs3,custstatus=MAX(custstatus)
      from Projects_CRStatus
      where custsentdate >= 'June 1, 2001' 
        AND custsentdate <= 'June 30, 2013'
        AND CustStatus IN ('RECEIVED AT VENDOR', 'CONFIRMATION SENT'
            ,'IC PENDING','CONFIRMATION RECEIVED','CANCELLED')
        group by wbs1,wbs2,wbs3 ) x
   inner join (
      select wbs1,wbs2,wbs3,custsentdate=max(custsentdate)
      from Projects_CRStatus
      group by wbs1,wbs2,wbs3) y
    on (x.wbs1=y.wbs1 and x.wbs2=y.wbs2 and x.wbs3=y.wbs3)
    ) z

WHERE RN = 1

【讨论】:

  • 我正在寻找“分组依据”中的顶级状态结果。所以,我正在寻找每个 wbs1、wbs2、wbs3 组合的排名靠前的状态。
  • @GabeN。哦。您使用的是什么风格的 SQL?你熟悉ROW_NUMBER 开窗功能吗?如果您的数据库支持它,那将是可行的方法。
  • 我正在使用 SQL Server。我使用了 ROW_NUMBER/ Partition OVER 方法,但得到了多个结果。
  • @GabeN。所以你想要 wbs1、wbs2 和 wbs3 的每个组合的“顶级状态”(这将是分区)?
【解决方案2】:

最好有一个包含此状态代码数据的表并加入该表,这样就可以按此状态表列排序。

【讨论】:

    猜你喜欢
    • 2019-08-02
    • 2019-11-26
    • 2019-04-14
    • 1970-01-01
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多