sql对同一结果集上的行进行平均

时间:2017-02-17 18:04:37

标签: sql-server tsql

我正在尝试做一些奇怪的事情,我有一个表在select *上有流动的选择输出(我只是简单地放4行)。 表格上的每一对行都是来自同一时间的信息,我希望从每个“事件”中获得某种平均值并将其输出到结果中。

当前结果集

VehicleId                               ActivityDateTime          SensorValue   SensorPilot
66B828BE-6EB9-4B69-AB5C-125439F065DE    2017-02-17 17:52:54.010   750           0
66B828BE-6EB9-4B69-AB5C-125439F065DE    2017-02-17 17:52:54.007   770           1
66B828BE-6EB9-4B69-AB5C-125439F065DE    2017-02-17 17:51:54.010   800           0
66B828BE-6EB9-4B69-AB5C-125439F065DE    2017-02-17 17:51:54.007   900           1

我希望有一个查询可以获得结果集,同时每个“事件”的平均值都是这样的。

VehicleId                               ActivityDateTime          SensorValue   SensorPilot
66B828BE-6EB9-4B69-AB5C-125439F065DE    2017-02-17 17:52:54.010   750           0
66B828BE-6EB9-4B69-AB5C-125439F065DE    2017-02-17 17:52:54.007   770           1
**66B828BE-6EB9-4B69-AB5C-125439F065DE  2017-02-17 17:52:54.008   760           2**
66B828BE-6EB9-4B69-AB5C-125439F065DE    2017-02-17 17:51:54.010   800           0
66B828BE-6EB9-4B69-AB5C-125439F065DE    2017-02-17 17:51:54.007   900           1
**66B828BE-6EB9-4B69-AB5C-125439F065DE  2017-02-17 17:51:54.008   850           2**

这样的事情可能吗?或者我应该尝试以另一种方式接近这个儿子?

如果出于某种原因相关的话,我试图从这个查询中查看视图

2 个答案:

答案 0 :(得分:2)

使用(Row_Number - 1)/ 2生成连续配对。然后将基集与聚合集合结合。

Use TEMPDB;
    IF OBJECT_ID('tempdb..test') IS NOT NULL
      DROP TABLE dbo.Test;
    GO

CREATE TABLE dbo.Test (
                        VehicleID VARCHAR(38),
                        ActivityDateTime DATETIME2(7),
                        SensorValue INT,
                        SensorPilot INT
                      );
INSERT INTO dbo.Test
VALUES ('66B828BE-6EB9-4B69-AB5C-125439F065DE', '2017-02-17 17:52:54.010', 750, 0),
('66B828BE-6EB9-4B69-AB5C-125439F065DE', '2017-02-17 17:52:54.007', 770, 1),
('66B828BE-6EB9-4B69-AB5C-125439F065DE', '2017-02-17 17:51:54.010', 800, 0),
('66B828BE-6EB9-4B69-AB5C-125439F065DE', '2017-02-17 17:51:54.007', 900, 1);

WITH cte
AS (SELECT  *,
            ConsecutivePairs = (ROW_NUMBER() OVER (PARTITION BY t.VehicleID ORDER BY t.ActivityDateTime) - 1) / 2
    FROM    dbo.Test AS t
   )
SELECT  cte.VehicleID,
        cte.ActivityDateTime,
        cte.SensorValue,
        cte.SensorPilot,
        cte.ConsecutivePairs
FROM    cte
UNION ALL
SELECT  cte.VehicleID,
        ActivityDateTime = DATEADD(MILLISECOND, DATEDIFF(MILLISECOND, MIN(cte.ActivityDateTime), MAX(cte.ActivityDateTime)) / 2, MIN(cte.ActivityDateTime)),
        SensorValue = AVG(cte.SensorValue),
        SensorPilot = COUNT(cte.VehicleID),
        cte.ConsecutivePairs
FROM    cte
GROUP BY cte.VehicleID,
         cte.ConsecutivePairs
ORDER BY cte.ConsecutivePairs, cte.SensorPilot       ;

答案 1 :(得分:1)

我没有太多时间,但这会让你开始......你将不得不对日期做点什么。

CREATE TABLE #temptable ( vehicleid VARCHAR(100), activitydate DATETIME, sensorvalue INT, sensorpilot INT)


INSERT INTO #temptable (vehicleid, activitydate, sensorvalue, sensorpilot)
VALUES ('66B828BE-6EB9-4B69-AB5C-125439F065DE',    '2017-02-17 17:52:54.010',   750,           0)
INSERT INTO #temptable (vehicleid, activitydate, sensorvalue, sensorpilot)
VALUES ('66B828BE-6EB9-4B69-AB5C-125439F065DE',    '2017-02-17 17:52:54.007',   770,           1)
INSERT INTO #temptable (vehicleid, activitydate, sensorvalue, sensorpilot)
VALUES ('66B828BE-6EB9-4B69-AB5C-125439F065DE',    '2017-02-17 17:51:54.010',   800,           0)
INSERT INTO #temptable (vehicleid, activitydate, sensorvalue, sensorpilot)
VALUES ('66B828BE-6EB9-4B69-AB5C-125439F065DE',    '2017-02-17 17:51:54.007',   900,           1)


SELECT NTILE(2) OVER(ORDER BY vehicleid DESC) AS RankVehicle
        , vehicleid
        , activitydate
        , sensorvalue
        , sensorpilot
FROM #temptable


UNION ALL 


SELECT A.RankVehicle, A.vehicleid, '' AS ActivityDate, AVG(A.SensorValue) AS SensorValue, 2 AS SensorPilot
FROM ( 
        SELECT NTILE(2) OVER(ORDER BY vehicleid DESC) AS RankVehicle
                , vehicleid
                , activitydate
                , AVG(sensorvalue) AS SensorValue
                , sensorpilot
        FROM #temptable
    GROUP BY vehicleid, activitydate, sensorpilot
    ) A
GROUP BY A.RankVehicle, A.vehicleid





DROP TABLE #temptable