Skip to content
Advertisement

Calculated field in condition based on other calculation

I have the following query:

 WITH CTE_TblData as(
   SELECT 
     td.EVENT_ID, 
     td.BSP,
     td.WIN_LOSE,
     (SELECT count(*) 
      FROM dbo.tblData td2 
      WHERE td2.EVENT_ID =td.EVENT_ID) AS [CountRunners],
     SUM(CASE WHEN td.EVENT_ID = td.EVENT_ID THEN 1  END) AS [Total Runners], 
     SUM(CASE WHEN td.WIN_LOSE = 1 THEN td.BSP  END) AS [WinnerPrice],
     SUM(CASE WHEN td.WIN_LOSE = 1 THEN 1 END) AS [WinnerCount]
   FROM tblData td
   WHERE td.EVENT_ID IN(146325086)
   GROUP BY td.EVENT_ID, td.BSP, td.WIN_LOSE
 )
SELECT td.event_id, 
    sum(case when td.event_id = td.event_id THEN 1 END) AS [Total Runners],
    sum(case when td.WinnerPrice IS NOT NULL THEN td.WinnerPrice END) AS [WinnerPrice],
    sum(case when td.WinnerCount IS NOT NULL THEN td.WinnerCount END) AS [WinnerCount],


    count(CASE WHEN td.BSP >13 AND td.BSP <=21 AND td.WIN_LOSE = 0 AND td.[Total Runners] >0 AND td.[Total Runners] <=1 THEN td.BSP END) AS Count13to21Runners0to1,
    sum(CASE WHEN td.BSP >13 AND td.BSP <=21 AND td.WIN_LOSE = 0 AND td.[Total Runners] >0 AND td.[Total Runners] <=1 THEN td.BSP END) AS Sum13to21Runners0to1,

    count(CASE WHEN td.BSP >13 AND td.BSP <=21 AND td.WIN_LOSE = 0 AND td.[Total Runners] >=2 AND td.[Total Runners] <=3 THEN td.BSP END) AS Count13to21Runners2to3,
    sum(CASE WHEN td.BSP >13 AND td.BSP <=21 AND td.WIN_LOSE = 0 AND td.[Total Runners] >=2 AND td.[Total Runners] <=3 THEN td.BSP END) AS Sum13to21Runners2to3

    FROM CTE_TblData td
WHERE td.EVENT_ID =146325086
GROUP BY td.event_id,  [Total Runners]

that is returning this:

╔═══════════╦═══════════════╦═════════════╦═════════════╦════════════════════════╦══════════════════════╦════════════════════════╗
║ event_id  ║ Total Runners ║ WinnerPrice ║ WinnerCount ║ Count13to21Runners0to1 ║ Sum13to21Runners0to1 ║ Count13to21Runners2to3 ║
╠═══════════╬═══════════════╬═════════════╬═════════════╬════════════════════════╬══════════════════════╬════════════════════════╣
║ 146325086 ║ 13            ║ 3.09        ║ 1           ║ 2                      ║ 30.84                ║ 0                      ║
╚═══════════╩═══════════════╩═════════════╩═════════════╩════════════════════════╩══════════════════════╩════════════════════════╝

however I seem to have an error in the logic of the case for counting the runner by every field. let me explain:

the result of the CTE at the beginning is

╔═══════════╦════════╦══════════╦══════════════╦═══════════════╦═════════════╦═════════════╗
║ EVENT_ID  ║ BSP    ║ WIN_LOSE ║ CountRunners ║ Total Runners ║ WinnerPrice ║ WinnerCount ║
╠═══════════╬════════╬══════════╬══════════════╬═══════════════╬═════════════╬═════════════╣
║ 146325086 ║ 7.09   ║ 0        ║ 13           ║ 1             ║ NULL        ║ NULL        ║
╠═══════════╬════════╬══════════╬══════════════╬═══════════════╬═════════════╬═════════════╣
║ 146325086 ║ 9.52   ║ 0        ║ 13           ║ 1             ║ NULL        ║ NULL        ║
╠═══════════╬════════╬══════════╬══════════════╬═══════════════╬═════════════╬═════════════╣
║ 146325086 ║ 9.75   ║ 0        ║ 13           ║ 1             ║ NULL        ║ NULL        ║
╠═══════════╬════════╬══════════╬══════════════╬═══════════════╬═════════════╬═════════════╣
║ 146325086 ║ 12.11  ║ 0        ║ 13           ║ 1             ║ NULL        ║ NULL        ║
╠═══════════╬════════╬══════════╬══════════════╬═══════════════╬═════════════╬═════════════╣
║ 146325086 ║ 13.27  ║ 0        ║ 13           ║ 1             ║ NULL        ║ NULL        ║
╠═══════════╬════════╬══════════╬══════════════╬═══════════════╬═════════════╬═════════════╣
║ 146325086 ║ 17.57  ║ 0        ║ 13           ║ 1             ║ NULL        ║ NULL        ║
╠═══════════╬════════╬══════════╬══════════════╬═══════════════╬═════════════╬═════════════╣
║ 146325086 ║ 28.25  ║ 0        ║ 13           ║ 1             ║ NULL        ║ NULL        ║
╠═══════════╬════════╬══════════╬══════════════╬═══════════════╬═════════════╬═════════════╣
║ 146325086 ║ 32.00  ║ 0        ║ 13           ║ 1             ║ NULL        ║ NULL        ║
╠═══════════╬════════╬══════════╬══════════════╬═══════════════╬═════════════╬═════════════╣
║ 146325086 ║ 67.19  ║ 0        ║ 13           ║ 1             ║ NULL        ║ NULL        ║
╠═══════════╬════════╬══════════╬══════════════╬═══════════════╬═════════════╬═════════════╣
║ 146325086 ║ 70.00  ║ 0        ║ 13           ║ 1             ║ NULL        ║ NULL        ║
╠═══════════╬════════╬══════════╬══════════════╬═══════════════╬═════════════╬═════════════╣
║ 146325086 ║ 204.61 ║ 0        ║ 13           ║ 1             ║ NULL        ║ NULL        ║
╠═══════════╬════════╬══════════╬══════════════╬═══════════════╬═════════════╬═════════════╣
║ 146325086 ║ 210.00 ║ 0        ║ 13           ║ 1             ║ NULL        ║ NULL        ║
╠═══════════╬════════╬══════════╬══════════════╬═══════════════╬═════════════╬═════════════╣
║ 146325086 ║ 3.09   ║ 1        ║ 13           ║ 1             ║ 3.09        ║ 1           ║
╚═══════════╩════════╩══════════╩══════════════╩═══════════════╩═════════════╩═════════════╝

and from that, I use the query to calculate the different fields I need. in this case, the column “Count13to21Runners2to3” is showing null, when in fact, what I need is to count the amount of “event_id” in that bsp range (this part works) and that have an amount of this count between 2 and 3.

so, given the result of the CTE, where I have 13 event_id, of which 2 cover the condition of being “win_lose =0“) and bsp between 13 and 21 I need for the field Count13to21Runners2to3 to count them, and that the field Count13to21Runners0to1 marks them as null. I think the issue is either in the column I’m using on the case function or in the way I’m doing the CTE for the field td.[Total Runners] that I’m using.

so, to resume, I need to do the condition in a way for the 2 event_id that are in the bsp bracket to be counter in the field Count13to21Runners2to3 (as they’re 2) and to be marked as null in the Count13to21Runners0to1 but I’m not understanding how to do it, I think the issue is in a column I could add in the CTE

EDIT for clarity:

I need this line:

count(CASE WHEN td.BSP >13 AND td.BSP <=21 AND td.WIN_LOSE = 0 AND td.[Total Runners] >=2 AND td.[Total Runners] <=3 THEN td.BSP END) AS Count13to21Runners2to3,

to return 2, as I have to count the amount of event_id that are in the bracket of td.BSP (>13 and <= 21) with win_lose =0 and with show if they’re between 3 and 3 occurences (there is 2).

Advertisement

Answer

This is the code I ended up with:

 WITH CTE_TblData as(
SELECT 
td.EVENT_ID, 
       td.MENU_HINT, 
       td.EVENT_NAME,
       td.BSP,
       td.EVENT_DT AS Expr1003, 
       td.WIN_LOSE,
       (SELECT count(*) FROM dbo.tblData td2 WHERE td2.EVENT_ID =td.EVENT_ID) AS [CountRunners],
       SUM(CASE WHEN td.EVENT_ID = td.EVENT_ID THEN 1  END) AS [Total Runners], 
       SUM(CASE WHEN td.WIN_LOSE = 1 THEN td.BSP  END) AS [WinnerPrice],
       SUM(CASE WHEN td.WIN_LOSE = 1 THEN 1 END) AS [WinnerCount],
       td.[Race No] AS RaceNo, 
       replace(LEFT(SUBSTRING(td.EVENT_NAME, CHARINDEX(' ', td.EVENT_NAME) + 1, LEN(td.EVENT_NAME)), CHARINDEX(' ', SUBSTRING(td.EVENT_NAME, CHARINDEX(' ', td.EVENT_NAME) + 2, LEN(td.EVENT_NAME)))),'m','') AS Distance, 
       REVERSE(LEFT(REVERSE(td.EVENT_NAME), CHARINDEX(' ', REVERSE(td.EVENT_NAME)) - 1)) AS Class, 
       FORMAT(CONVERT(DATETIME, td.EVENT_DT, 103), 'dddd') AS [Day],
       DATEPART(WEEKDAY, CONVERT(DATETIME, td.EVENT_DT, 103)) AS [DayNumber]
FROM tblData td
WHERE td.EVENT_ID IN(145814087)
GROUP BY td.EVENT_ID, 
         td.MENU_HINT, 
         td.EVENT_NAME, 
         td.BSP,
         td.EVENT_DT, 
         td.[Race No],
         td.WIN_LOSE
         )



    SELECT td.event_id, td.MENU_HINT, td.EVENT_NAME, FORMAT (td.Expr1003, 'dd/MM/yyyy ') as Expr1003, 
    isnull((SELECT count(td2.BSP - CASE WHEN td2.WIN_LOSE =1 THEN 1 end)  FROM dbo.tblData td2 WHERE td2.EVENT_ID = td.EVENT_ID AND td2.BSP >34 AND td2.BSP <55 HAVING count(td2.BSP) >=4   ),0) AS CountWinnerPrice34to55Runners4to1000,
    (SELECT sum(td2.BSP - CASE WHEN td2.WIN_LOSE =0 THEN td2.BSP ELSE 0 end)  FROM dbo.tblData td2 WHERE td2.EVENT_ID = td.EVENT_ID AND td2.BSP >34 AND td2.BSP <55 HAVING count(td2.BSP) >=4   ) AS SumWinnerPrice34to55Runners4to1000,
    isnull((SELECT count(td2.BSP - CASE WHEN td2.WIN_LOSE =0 THEN 1 end)  FROM dbo.tblData td2 WHERE td2.EVENT_ID = td.EVENT_ID AND td2.BSP >34 AND td2.BSP <55 HAVING count(td2.BSP) >=4   ),0) AS CountWinnerNotPrice34to55Runners4to1000,
    (SELECT sum(td2.BSP - CASE WHEN td2.WIN_LOSE =1 THEN td2.BSP ELSE 0 end)  FROM dbo.tblData td2 WHERE td2.EVENT_ID = td.EVENT_ID AND td2.BSP >34 AND td2.BSP <55 HAVING count(td2.BSP) >=4   ) AS SumWinnerNotPrice34to55Runners4to1000,
    isnull((SELECT count(td2.BSP - CASE WHEN td2.WIN_LOSE =1 THEN 1 end)  FROM dbo.tblData td2 WHERE td2.EVENT_ID = td.EVENT_ID AND td2.BSP >55 AND td2.BSP <89 HAVING count(td2.BSP) IN(0,1)   ),0) AS CountWinnerPrice55to89Runners0to1,
    (SELECT sum(td2.BSP - CASE WHEN td2.WIN_LOSE =0 THEN td2.BSP ELSE 0 end)  FROM dbo.tblData td2 WHERE td2.EVENT_ID = td.EVENT_ID AND td2.BSP >55 AND td2.BSP <89 HAVING count(td2.BSP) IN(0,1)   ) AS SumWinnerPrice55to89Runners0to1,
    isnull((SELECT count(td2.BSP - CASE WHEN td2.WIN_LOSE =0 THEN 1 end)  FROM dbo.tblData td2 WHERE td2.EVENT_ID = td.EVENT_ID AND td2.BSP >55 AND td2.BSP <89 HAVING count(td2.BSP) IN(0,1)   ),0) AS CountWinnerNotPrice55to89Runners0to1,
    (SELECT sum(td2.BSP - CASE WHEN td2.WIN_LOSE =1 THEN td2.BSP ELSE 0 end)  FROM dbo.tblData td2 WHERE td2.EVENT_ID = td.EVENT_ID AND td2.BSP >55 AND td2.BSP <89 HAVING count(td2.BSP) IN(0,1)   ) AS SumWinnerNotPrice55to89Runners0to1,
    isnull((SELECT count(td2.BSP - CASE WHEN td2.WIN_LOSE =1 THEN 1 end)  FROM dbo.tblData td2 WHERE td2.EVENT_ID = td.EVENT_ID AND td2.BSP >55 AND td2.BSP <89 HAVING count(td2.BSP) IN(2,3)   ),0) AS CountWinnerPrice55to89Runners2to3,
    (SELECT sum(td2.BSP - CASE WHEN td2.WIN_LOSE =0 THEN td2.BSP ELSE 0 end)  FROM dbo.tblData td2 WHERE td2.EVENT_ID = td.EVENT_ID AND td2.BSP >55 AND td2.BSP <89 HAVING count(td2.BSP) IN(2,3)   ) AS SumWinnerPrice55to89Runners2to3,
    isnull((SELECT count(td2.BSP - CASE WHEN td2.WIN_LOSE =0 THEN 1 end)  FROM dbo.tblData td2 WHERE td2.EVENT_ID = td.EVENT_ID AND td2.BSP >55 AND td2.BSP <89 HAVING count(td2.BSP) IN(2,3)   ),0) AS CountWinnerNotPrice55to89Runners2to3




FROM CTE_TblData td
WHERE td.class not in ('S', 'M', 'Trot', 'Pace')
GROUP BY td.event_id, td.MENU_HINT, td.EVENT_NAME, td.Expr1003, td.RaceNo, td.Distance, td.Class, td.[Day]
Order by td.Expr1003 desc, td.raceno desc
--RETURN 0 

the result of the CTE is:

+-----------+---------------------------+--------------+-------+------------+----------+--------------+---------------+-------------+-------------+--------+----------+-------+--------+-----------+
| EVENT_ID  | MENU_HINT                 | EVENT_NAME   | BSP   | Expr1003   | WIN_LOSE | CountRunners | Total Runners | WinnerPrice | WinnerCount | RaceNo | Distance | Class | Day    | DayNumber |
+-----------+---------------------------+--------------+-------+------------+----------+--------------+---------------+-------------+-------------+--------+----------+-------+--------+-----------+
| 145814087 | AUS / Coff (AUS) 22nd Jul | R7 1415m CL3 | 4.20  | 2018-07-22 | 1        | 12           | 1             | 4.20        | 1           | 7      | 1415     | CL3   | Sunday | 1         |
+-----------+---------------------------+--------------+-------+------------+----------+--------------+---------------+-------------+-------------+--------+----------+-------+--------+-----------+
| 145814087 | AUS / Coff (AUS) 22nd Jul | R7 1415m CL3 | 4.47  | 2018-07-22 | 0        | 12           | 1             | NULL        | NULL        | 7      | 1415     | CL3   | Sunday | 1         |
+-----------+---------------------------+--------------+-------+------------+----------+--------------+---------------+-------------+-------------+--------+----------+-------+--------+-----------+
| 145814087 | AUS / Coff (AUS) 22nd Jul | R7 1415m CL3 | 7.80  | 2018-07-22 | 0        | 12           | 1             | NULL        | NULL        | 7      | 1415     | CL3   | Sunday | 1         |
+-----------+---------------------------+--------------+-------+------------+----------+--------------+---------------+-------------+-------------+--------+----------+-------+--------+-----------+
| 145814087 | AUS / Coff (AUS) 22nd Jul | R7 1415m CL3 | 8.60  | 2018-07-22 | 0        | 12           | 1             | NULL        | NULL        | 7      | 1415     | CL3   | Sunday | 1         |
+-----------+---------------------------+--------------+-------+------------+----------+--------------+---------------+-------------+-------------+--------+----------+-------+--------+-----------+
| 145814087 | AUS / Coff (AUS) 22nd Jul | R7 1415m CL3 | 10.99 | 2018-07-22 | 0        | 12           | 1             | NULL        | NULL        | 7      | 1415     | CL3   | Sunday | 1         |
+-----------+---------------------------+--------------+-------+------------+----------+--------------+---------------+-------------+-------------+--------+----------+-------+--------+-----------+
| 145814087 | AUS / Coff (AUS) 22nd Jul | R7 1415m CL3 | 15.95 | 2018-07-22 | 0        | 12           | 1             | NULL        | NULL        | 7      | 1415     | CL3   | Sunday | 1         |
+-----------+---------------------------+--------------+-------+------------+----------+--------------+---------------+-------------+-------------+--------+----------+-------+--------+-----------+
| 145814087 | AUS / Coff (AUS) 22nd Jul | R7 1415m CL3 | 18.50 | 2018-07-22 | 0        | 12           | 1             | NULL        | NULL        | 7      | 1415     | CL3   | Sunday | 1         |
+-----------+---------------------------+--------------+-------+------------+----------+--------------+---------------+-------------+-------------+--------+----------+-------+--------+-----------+
| 145814087 | AUS / Coff (AUS) 22nd Jul | R7 1415m CL3 | 30.45 | 2018-07-22 | 0        | 12           | 1             | NULL        | NULL        | 7      | 1415     | CL3   | Sunday | 1         |
+-----------+---------------------------+--------------+-------+------------+----------+--------------+---------------+-------------+-------------+--------+----------+-------+--------+-----------+
| 145814087 | AUS / Coff (AUS) 22nd Jul | R7 1415m CL3 | 34.20 | 2018-07-22 | 0        | 12           | 1             | NULL        | NULL        | 7      | 1415     | CL3   | Sunday | 1         |
+-----------+---------------------------+--------------+-------+------------+----------+--------------+---------------+-------------+-------------+--------+----------+-------+--------+-----------+
| 145814087 | AUS / Coff (AUS) 22nd Jul | R7 1415m CL3 | 36.63 | 2018-07-22 | 0        | 12           | 1             | NULL        | NULL        | 7      | 1415     | CL3   | Sunday | 1         |
+-----------+---------------------------+--------------+-------+------------+----------+--------------+---------------+-------------+-------------+--------+----------+-------+--------+-----------+
| 145814087 | AUS / Coff (AUS) 22nd Jul | R7 1415m CL3 | 40.00 | 2018-07-22 | 0        | 12           | 1             | NULL        | NULL        | 7      | 1415     | CL3   | Sunday | 1         |
+-----------+---------------------------+--------------+-------+------------+----------+--------------+---------------+-------------+-------------+--------+----------+-------+--------+-----------+
| 145814087 | AUS / Coff (AUS) 22nd Jul | R7 1415m CL3 | 45.55 | 2018-07-22 | 0        | 12           | 1             | NULL        | NULL        | 7      | 1415     | CL3   | Sunday | 1         |
+-----------+---------------------------+--------------+-------+------------+----------+--------------+---------------+-------------+-------------+--------+----------+-------+--------+-----------+

and the query returns:

+-----------+---------------------------+--------------+------------+--------------------------------------+------------------------------------+-----------------------------------------+
| event_id  | MENU_HINT                 | EVENT_NAME   | Expr1003   | CountWinnerPrice34to55Runners4to1000 | SumWinnerPrice34to55Runners4to1000 | CountWinnerNotPrice34to55Runners4to1000 |
+-----------+---------------------------+--------------+------------+--------------------------------------+------------------------------------+-----------------------------------------+
| 145814087 | AUS / Coff (AUS) 22nd Jul | R7 1415m CL3 | 22/07/2018 | 0                                    | 0.00                               | 4                                       |
+-----------+---------------------------+--------------+------------+--------------------------------------+------------------------------------+-----------------------------------------+

however it is very poor in terms of performance.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement