SQL Query - SUM of data in transaction (for each transaction)
I have this table :
Trans_ID Name Value 开发者_运维技巧Total_Item
100 I1 0.33333333 3
100 I2 0.33333333 3
400 I1 0.33333333 3
400 I2 0.33333333 3
800 I1 0.25 4
800 I2 0.25 4
900 I1 0.33333333 3
900 I2 0.33333333 3
1000 I1 0.2 5
1000 I2 0.2 5
i need to make it into :
ITEM VALUE
I1,I2 0.28999998
Value is calculated from sum of each 2 item in all transaction / total transaction
EX: item I1 & I2 at trans 100
(0.33333333 + 0.33333333) = 0.666666666
trans 400
(0.33333333 + 0.33333333) = 0.666666666
trans 800
(0.25+0.25) = 0.5
trans 900
(0.33333333 + 0.33333333) = 0.666666666
trans 1000
(0.2+0.2) = 0.4
So Value will be:
(0.666666666+0.666666666+0.5+0.666666666+0.4)/10= 0.28999998
*since total transaction in this table is example table 10. there's aprox 50k transaction in my real table
please note that Total_item value is fixed for each transaction and there's no mistake (note that trans 100 only have 2 item and i put 3 in total item)
i'm working with ms access (but general sql query is fine)
If you are correct about your process, then all the grouping you are using is not necessary - the value is the same without the grouping. That is:
((T100_I1 + T100_I2) + (T400_I1 + T400_I2)) / 4 = (T100_I1 + T100_I2 + T400_I1 + T400_I2) / 4 =
In other words, to get the value you described you want, you just need to sum all the values and divide by their count.
select sum(value)/count(*)
from table
精彩评论