日期:2014-05-18 浏览次数:20518 次
IF EXISTS (SELECT 1 FROM SYSOBJECTS WHERE name = 'tba') BEGIN DROP TABLE tba END GO CREATE TABLE tba ( OperationAreaName VARCHAR(100), StationName VARCHAR(100), ReceiveTime VARCHAR(100), InPipePressure FLOAT, AccumulateWater INT ) GO INSERT INTO tba SELECT '第二采油作业区', '枣三注水站', '2012-07-03 09:00:00.000', 0.4, 67 UNION SELECT '第二采油作业区', '枣三注水站', '2012-07-03 10:00:00.000', 0.4, 58 UNION SELECT '第二采油作业区', '枣四注水站', '2012-07-03 09:00:00.000', 0.4, 45 UNION SELECT '第二采油作业区', '枣四注水站', '2012-07-03 10:00:00.000', 0.4, 45 UNION SELECT '第二采油作业区', '枣五注水站', '2012-07-03 09:00:00.000', 0.4, 80 UNION SELECT '第二采油作业区', '枣五注水站', '2012-07-03 10:00:00.000', 0.4, 79 UNION SELECT '第二采油作业区', '家五接转站', '2012-07-03 09:00:00.000', 0.4, 57 UNION SELECT '第二采油作业区', '家五接转站', '2012-07-03 10:00:00.000', 0.4, 56 GO SELECT OperationAreaName,ReceiveTime,SUM(InPipePressure * AccumulateWater) / SUM(AccumulateWater * 1.0) AS value FROM tba GROUP BY OperationAreaName,ReceiveTime
------解决方案--------------------
表示看不太懂你的逻辑
9:00
(((67*0.4)+(45*0.4)+(80*0.4)+(57*0.4))/(67+45+57))
10:00为什么不是:
(((58*0.4)+(45*0.4)+(79*0.4)+(56*0.4))/(58+45+56))
------解决方案--------------------
--> 测试数据:[test] if object_id('[test]') is not null drop table [test] create table [test]( [OperationAreaName] varchar(14), [StationName] varchar(10), [ReceiveTime] datetime, [InPipePressure] numeric(2,1), [AccumulateWater] int ) go insert [test] select '第二采油作业区','枣三注水站','2012-07-03 09:00:00.000',0.4,67 union all select '第二采油作业区','枣三注水站','2012-07-03 10:00:00.000',0.4,58 union all select '第二采油作业区','枣四注水站','2012-07-03 09:00:00.000',0.4,45 union all select '第二采油作业区','枣四注水站','2012-07-03 10:00:00.000',0.4,45 union all select '第二采油作业区','枣五注水站','2012-07-03 09:00:00.000',0.4,80 union all select '第二采油作业区','枣五注水站','2012-07-03 10:00:00.000',0.4,79 union all select '第二采油作业区','家五接转站','2012-07-03 09:00:00.000',0.4,57 union all select '第二采油作业区','家五接转站','2012-07-03 10:00:00.000',0.4,56 go select OperationAreaName, ReceiveTime, sum(InPipePressure*AccumulateWater)/SUM([AccumulateWater]) as Total from test group by OperationAreaName, ReceiveTime /* OperationAreaName ReceiveTime Total ----------------------------- 第二采油作业区 2012-07-03 09:00:00.000 0.400000 第二采油作业区 2012-07-03 10:00:00.000 0.400000 */