I have a table containing order details. I would like to be able to select the average attribute for a certain item.
For example, select “item a”, now find the average color of “item a”. If there were 10 orders of “item a” and the colors ordered broke down as follows:
4 – black
2 – blue
2 – red
1 – orange
1 – white
I would like for it to return “black”. Is there any such statement that could do that?
Also, would it then be possible to weigh the average, for example giving the last 3 orders of “item a” a weight of 2, instead of 1. So if the last 3 orders were all yellow, it would essentially count as 6?
You can
group byon color, and then select the first row:You could give some rows a higher weight with a subquery. This one gives the last 3 orders a higher weight: