开发者

SP for calculating the Average of every 2 rows

I have a spreadsheet that contains a country code and its currency rate with respect to my country. Each country code appears twice in the Excel sheet with 2 different values. I want to get the av开发者_运维知识库erage of the 2 values and save the average to the first row. I was thinking of doing so by inserting the first row, reading the second row, and if the country code exists, do the average and save to the first, otherwise add the row to the list. How is such feasible in SQL code?


If I understand your needs, you could try:

UPDATE your_table
SET field1 = (field1 + field2)/2

EDITED (after reading your comment)
If you update only first row for every CountryCode you have another problem: second row remain there. So probably the best thing you could do is creating another table and fill it with correct values only:

INSERT INTO new_table (CountryCode, CurrencyRate)
SELECT CountryCode, AVG(CurrencyRate)
FROM your_table
GROUP BY CountryCode
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜