简体   繁体   English

根据选择 SQL Server 上其他列的条件添加计算列

[英]Add calculated columns based on conditions of other column on select SQL Server

I need to add some columns based on calculations depending on the conditions of other columns.我需要根据其他列的条件根据计算添加一些列。

For example, I have this:例如,我有这个:

EVENT_ID               Color              WinnerPrice            WinnerCount 0 to 5                              5 to 15                              15 to 50                              BSP
---------------------- ------------ ------------- ---------------------- ----------- ----------------------------------- ------------------------------------ ------------------------------------- ----------------------
138386456              red 1                      8.707157072            1           sum of BSP when BSP between 0 and 5 sum of BSP when BSP between 5 and 15 sum of BSP when BSP between 15 and 50 8.707157072
138386458              blue         1             101.7549557            1           sum of BSP when BSP between 0 and 5 sum of BSP when BSP between 5 and 15 sum of BSP when BSP between 15 and 50 101.7549557
138386460              yellow 1                   7.532110284            1           sum of BSP when BSP between 0 and 5 sum of BSP when BSP between 5 and 15 sum of BSP when BSP between 15 and 50 7.532110284
138386464              other fields 1             52.34970073            1           sum of BSP when BSP between 0 and 5 sum of BSP when BSP between 5 and 15 sum of BSP when BSP between 15 and 50 52.34970073
138386468              other fields 1             35.86681076            1           sum of BSP when BSP between 0 and 5 sum of BSP when BSP between 5 and 15 sum of BSP when BSP between 15 and 50 35.86681076
138386468              other fields 1             44                     1           sum of BSP when BSP between 0 and 5 sum of BSP when BSP between 5 and 15 sum of BSP when BSP between 15 and 50 44
138386470              other fields 1             32                     1           sum of BSP when BSP between 0 and 5 sum of BSP when BSP between 5 and 15 sum of BSP when BSP between 15 and 50 32
138386474              other fields 1             55.55523634            1           sum of BSP when BSP between 0 and 5 sum of BSP when BSP between 5 and 15 sum of BSP when BSP between 15 and 50 55.55523634

Image version:图片版本:

在此处输入图片说明

How could I do this in a performant way?我怎么能以一种高性能的方式做到这一点? I have to add several of this type of calculated queries, but I can't wrap my head around how to do it in a way that is performant (I thought it might be done with rollup? but not getting it)我必须添加几个这种类型的计算查询,但我无法理解如何以一种高性能的方式进行(我认为它可能是通过汇总完成的?但没有得到它)

I also tried doing a scalar function for it, but it seems it is just moving the logic out of view as it's still a very poor performant.我也尝试为它做一个标量函数,但它似乎只是将逻辑移出视野,因为它的性能仍然很差。

The condition would be a sum of the BSP field grouped by a field that is not shown now, let's say条件将是按现在未显示的字段分组的 BSP 字段的总和,比方说

  • column between 0 to 5 should have sum of bsp when bsp between 0 and 5 grouped by when color = 'red'当 bsp 介于 0 和 5 之间时,0 到 5 之间的列应具有 bsp 的总和,当 color = 'red' 时
  • column between 5 to 15 should have sum of bsp when bsp between 5 and 15 grouped by color = 'blue'当 5 到 15 之间的 bsp 按颜色 = 'blue' 分组时,5 到 15 之间的列应该有 bsp 的总和
  • column between 15 to 50 should have sum of bsp when bsp between 15 and 50 grouped by color = 'yellow'当 15 到 50 之间的 bsp 按颜色 = 'yellow' 分组时,15 到 50 之间的列应该有 bsp 的总和

An example of the existing query for getting everything between 0 and 5 is:获取 0 到 5 之间的所有内容的现有查询的示例是:

SELECT 
    tblData.MENU_HINT, tblData.EVENT_NAME, 
    SUM(tblData.BSP) AS [Sum], 
    COUNT(tblData.ID) AS [count], 
    AVG(tblData.BSP) AS AVerage, 
    tblData.EVENT_ID
FROM 
    tblData
WHERE 
    (((tblData.BSP) > 0 AND (tblData.BSP) <= 5))
GROUP BY 
    tblData.MENU_HINT, tblData.EVENT_NAME, tblData.EVENT_ID;

I think you want conditional aggregation.我认为你想要条件聚合。 I'm a little confused on which calculations you actually want, but the idea is:我对你真正想要的计算有点困惑,但这个想法是:

SELECT t.MENU_HINT, t.EVENT_ID, t.EVENT_NAME,
       SUM(CASE WHEN t.BSP >= 0 AND t.BSP < 5 THEN t.BSP END) as sum_0_5,
       SUM(CASE WHEN t.color = 'Red' AND t.BSP >= 0 AND t.BSP < 5 THEN t.BSP END) as sum_red_0_5,
       . . . And so on for the calculations you want
FROM tblData t
GROUP BY t.MENU_HINT, t.EVENT_NAME, t.EVENT_ID;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM