MS SQL - 具有特定列的平均行数

时间:2013-01-11 18:58:13

标签: tsql

嘿大家我想知道如何通过以下表格中的选择来创建平均值。

MinutesToLoad |Environment | Server | Exchange | StartTime |EndTime

140 ENVIRONMENT_A | Server2 | TAI | 2013-01-06 17:22:44.000 | 2013-01-06 19:42:53.000
135 ENVIRONMENT_A | Server2 | TAI | 2013-01-07 17:23:21.000 | 2013-01-07 19:38:37.000
130 ENVIRONMENT_A | Server2 | TAI | 2013-01-08 17:24:03.000 | 2013-01-08 19:34:17.000
130 ENVIRONMENT_A | Server2 | TAI | 2013-01-09 17:24:42.000 | 2013-01-09 19:34:45.000
140 ENVIRONMENT_A | Server1 | TAI | 2013-01-06 17:22:44.000 | 2013-01-06 19:42:53.000
135 ENVIRONMENT_A | Server1 | TAI | 2013-01-07 17:23:21.000 | 2013-01-07 19:38:37.000
130 ENVIRONMENT_A | Server1 | TAI | 2013-01-08 17:24:03.000 | 2013-01-08 19:34:17.000
130 ENVIRONMENT_A | Server1 | TAI | 2013-01-09 17:24:42.000 | 2013-01-09 19:34:45.000
1430    ENVIRONMENT_A | Server1 | SET | 2013-01-07 00:03:01.000 | 2013-01-07 23:53:37.000
1431    ENVIRONMENT_A | Server1 | SET | 2013-01-08 00:03:36.000 | 2013-01-08 23:54:14.000
1430    ENVIRONMENT_A | Server1 | SET | 2013-01-09 00:04:14.000 | 2013-01-09 23:54:55.000
1430    ENVIRONMENT_A | Server2 | SET | 2013-01-07 00:03:01.000 | 2013-01-07 23:53:37.000
1431    ENVIRONMENT_A | Server2 | SET | 2013-01-08 00:03:36.000 | 2013-01-08 23:54:14.000
1430    ENVIRONMENT_A | Server2 | SET | 2013-01-09 00:04:14.000 | 2013-01-09 23:54:55.000
1   ENVIRONMENT_A | Server2 | QXI | 2013-01-08 03:23:57.000 | 2013-01-08 03:24:02.000
1   ENVIRONMENT_A | Server1 | QXI | 2013-01-08 03:23:57.000 | 2013-01-08 03:24:02.000

MinutesToLoad在我的选择程序中实际上是DATEDIFF(MINUTE,startTime,EndTime),但其余部分来自表格。

目标结果是将此结果看起来像这样:

MinutesToLoadOverLast10Days - Environment - Server - Exchange 
133                          ENVIRONMENT_A   Server2 TAI 
133                          ENVIRONMENT_A   Server1 TAI 
1430                         ENVIRONMENT_A   Server1 SET
1430                         ENVIRONMENT_A   Server2 SET
1                            ENVIRONMENT_A   Server2 QXI
1                            ENVIRONMENT_A   Server1 QXI

我知道我将使用AVG功能,但我在试图找出使用它来获得我想要的结果时迷失了。

提前谢谢。

1 个答案:

答案 0 :(得分:0)

Select avg(MinutesToLoadOverLast10Days),Environment, Server,Exchange 
FROM YOUR_TABLE_NAME
GROUP BY Environment, Server,Exchange