sql - 根据选择中其他列的计数计算列与案例
问题描述
我有以下一组数据:
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;
我想我没那么远,但我不能把它带到这里的终点线。
解决方案
您可以使用申请
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)
您可以根据需要在外部应用中添加任意数量的案例。
推荐阅读
- regex - 如何在perl中查找和替换多行文本
- java - JDBi3 UnableToCreateStatementException:没有为限定类型 org.jdbi.v3.core.argument.NullArgument 的“假”注册参数工厂
- c# - 如何使用 Refit 在 dotnet 5 中使用 XOP 处理 XML SOAP?
- python - 在 Python 中解析 GATE 文档的 TextWithNodes 元素
- r - 根据其他列中的某些值添加具有升序数字开始和结束的列
- swift - 如何通过新尝试获取下载进度 await URLSession.shared.download(...)
- android - 滚动到任何大于 1 的布局时,Horizontal Recyclerview 失去焦点
- sql - 从表中显示每个出版商的最新出版书籍的查询
- vb.net - 页面上的 WebBrowser 控件链接调用 javascript
- r - 将函数应用于列表的各个部分