简体   繁体   中英

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

  • column between 0 to 5 should have sum of bsp when bsp between 0 and 5 grouped by when color = 'red'
  • column between 5 to 15 should have sum of bsp when bsp between 5 and 15 grouped by color = 'blue'
  • column between 15 to 50 should have sum of bsp when bsp between 15 and 50 grouped by color = 'yellow'

An example of the existing query for getting everything between 0 and 5 is:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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