Skip to content
Advertisement

Grouping by multiple columns in a range in postgres

I have a couple tables (pega.race exists too we just don’t need any of the data for this)

What I want is to get an average result for each combination (permutation?) of speed/strength/wind/lightning/water/fire in a range. So from 0-2.25, 2.25-4.5, 4.5-6.75, and 6.75-9. They have to be within 0-9. No values exist outside that.

So I want an avg result for 6.75-9 speed, 2.25-4.5 strength, 4.5-6.75 wind, 2.25-4.5 lightning, 0-2.25 water, 0-2.25 fire and every other combination there is. I don’t have enough data to do it simply by rounding like so

So I want to extend the ranges out on these to bring the number of possibilities down by a few factors.

Looking at other posts similar to this I found solutions to group by ranges on a single column and I’m struggling to extend that to numerous columns.

Here’s an attempt I made

I think I’m almost there with this? But not sure how to break everything out into every combo.

Thanks.

Also, bonus points if you can lead me down the path to get percent 1st place. (result == 1). Extra extra bonus points if you can lead me down a more statistical route that pulls out correlations between numerous columns and a lower result/higher 1st %. This one I have no clue how to even begin.. Last (and only) stats class was 5 years ago now..

Advertisement

Answer

You needn’t to generate the series explicitly and check ranges due to regular data structure. Try using arithm opers to get the statistics by the range index 0..3. For example

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