【问题标题】:Calculate column based of count of other column in select with case根据选择与案例中其他列的计数计算列
【发布时间】:2020-03-15 19:29:17
【问题描述】:

我有以下一组数据:

EVENT_ID    MENU_HINT                   EVENT_NAME      SELECTION_ID EVENT_DT   WIN_LOSE    BSP
144705336   AUS / KemG (AUS) 16th Jun   R3 1000m 2yo    19276642    16-Jun-18   0           46.91005891
144705336   AUS / KemG (AUS) 16th Jun   R3 1000m 2yo    19052159    16-Jun-18   0           9.2
144705336   AUS / KemG (AUS) 16th Jun   R3 1000m 2yo    19276641    16-Jun-18   0           11
144705336   AUS / KemG (AUS) 16th Jun   R3 1000m 2yo    19276644    16-Jun-18   0           7.698731493
144705336   AUS / KemG (AUS) 16th Jun   R3 1000m 2yo    19276646    16-Jun-18   0           421.7295978
144705336   AUS / KemG (AUS) 16th Jun   R3 1000m 2yo    19276645    16-Jun-18   0           89.22199353
144705336   AUS / KemG (AUS) 16th Jun   R3 1000m 2yo    19276647    16-Jun-18   0           150
144705336   AUS / KemG (AUS) 16th Jun   R3 1000m 2yo    19276643    16-Jun-18   0           48.90986662
144705336   AUS / KemG (AUS) 16th Jun   R3 1000m 2yo    19276650    16-Jun-18   1           3.466233972
144705336   AUS / KemG (AUS) 16th Jun   R3 1000m 2yo    19276640    16-Jun-18   0           2.88

为此,我需要计算一些额外的列,例如:

CountWinnerNotPrice89to1000Runners7to12

SumWinnerNotPrice89to1000Runners7to12

CountWinnerNotPrice89to1000Runners7to12的逻辑是:event_id的计数,条件如下:

  • BSP 必须介于 89 和 1000 之间(3 行覆盖此)
  • “win_lose”必须为 0(因为它是“无价格”)
  • event_id 的总数必须在 7 到 12 之间(跑步者的数量)

所以这个列的结果应该是3,对于SumWinnerNotPrice89to1000Runners7to12同样的条件适用,但是我必须返回BSP列的SUM,所以结果是660.95

这是一个例子,有了这些数据,我有几列,如果有的话,还有一个例子

CountWinnerNotPrice7to15Runners7to12
SumWinnerNotPrice7to15Runners7to12

那么结果,因为 BSP 将在 7 和 15 之间,是

CountWinnerNotPrice7to15Runners7to12 = 3

SumWinnerNotPrice7to15Runners7to12 = 27.89

我必须做很多此类列,但我不了解执行此类条件的正确方法。

我尝试在选择中使用 CASE,但是当我必须考虑跑步者的数量时,我无法让它工作。我还尝试了标量函数(返回数据需要很长时间)

我认为应该在不必进行递归搜索的情况下完成逻辑(我认为没有 CTE),因为我有大约 900 列要添加,这肯定会杀死它。

用一个案例来做这件事的逻辑是什么?我想我错过了一些东西,因为我尝试过类似于:

SELECT COUNT(td.EVENT_NAME), 
       td.SELECTION_NAME, 
       SUM(CASE
               WHEN(td.BSP >= 89
                    AND td.BSP < 1000)
                   AND td.WIN_LOSE = 0
           AND COUNT(td.EVENT_NAME) > 7 --this doesn't work
           AND COUNT(td.EVENT_NAME) <= 12 --this doesn't work
               THEN td.BSP
           END) AS SumWinnerRunners
FROM tblData td
WHERE td.EVENT_ID = 144705336
GROUP BY td.SELECTION_NAME, 
         td.BSP;

我想我没那么远,但我不能把它带到终点线。

【问题讨论】:

    标签: sql sql-server tsql user-defined-functions


    【解决方案1】:

    你可以使用申请

    select 
      tblcnt.cnt
      ,td.selection_name
      ,case 
         when cnt between 7 and 12
         then SumWinnerNotPrice89to1000Runners7to12
         else 0
       end as SumWinnerNotPrice89to1000Runners7to12
      ,case 
         when cnt between 7 and 12
         then CountWinnerNotPrice89to1000Runners7to12
         else 0
       end as CountWinnerNotPrice89to1000Runners7to12
    from tblData td
    outer apply (
      select 
        count(*)
        ,sum(
          case 
            when tdi.BSP >= 89 
            and  tdi.BSP < 1000
            and  tdi.WIN_LOSE = 0
          then tdi.BSP
          else 0
        )
        ,sum(
          case 
            when tdi.BSP >= 89 
            and  tdi.BSP < 1000
            and  tdi.WIN_LOSE = 0
          then 1
          else 0
        )
      from tblData tdi
      where tdi.event_id = td.event_id
    ) tblcnt(cnt,SumWinnerNotPrice89to1000Runners7to12,CountWinnerNotPrice89to1000Runners7to12)
    

    您可以根据需要在外部应用中添加任意数量的案例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-11
      • 2019-04-17
      • 2023-03-15
      • 2020-11-16
      • 2014-11-04
      • 2022-07-08
      相关资源
      最近更新 更多