DB2将多行选择为一行

时间:2013-05-06 16:36:53

标签: sql database db2

我有以下内容:

UserID | Number
1 | 100
1 | 200
1 | 300
2 | 20
2 | 21
2 | 22
3 | 333
......
.....
1000| 23
.......

and I want to get the following result:
UserID | Average
1 | 200
2 | 21
3 | 300
......
......
1000 | 200
......

这些价​​值只是为了了解我在说什么! 提前谢谢!

2 个答案:

答案 0 :(得分:0)

使用AVG聚合:

SELECT UserId, AVG(Number)
FROM YourTable
GROUP By UserId

SQL Fiddle Demo

答案 1 :(得分:0)

SELECT UserID, AVG(Number) AS Average
FROM YourTable
GROUP BY UserID