Skip to content
Advertisement

Calculated field in condition based on other calculation

I have the following query:

that is returning this:

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

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:

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:

the result of the CTE is:

and the query returns:

however it is very poor in terms of performance.

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