2

我有一个具有时间缩放值的表,我需要能够缩放这些值。我试图使这尽可能简单,但执行速度对我来说是一个重要因素。

让我举一个 tblTSS_DataCollection 的例子:

SELECT TOP 5
   [DataPointID]
  ,[DatapointDate]
  ,dc.[DataPointValue] 
FROM [tblTSS_DataCollection] dc
Where DatapointID = 1093

这将返回一个非常简单的表:

DataPointID DatapointDate            DataPointValue
1093        2012-07-29 00:00:01.000  0.01869818
1093        2012-07-29 00:01:01.000  0.01882841
1093        2012-07-29 00:02:01.000  0.01895865
1093        2012-07-29 00:03:01.000  0.01908888
1093        2012-07-29 00:04:01.000  0.01921912

现在我有另一个名为 tblTSS_ScaleSettings 的表,如下所示:

SELECT [ID]
      ,[DatapointID]
      ,[EffectiveDate]
      ,[ScaleType]
      ,[ScaleValue]
FROM [tblTSS_ScaleSettings]

这将返回如下结果:

ID  DatapointID EffectiveDate            ScaleType   ScaleValue
1   1093        2012-07-29 00:03:01.000  *           10.0000

现在我需要能够做的是这样的事情:

SELECT TOP 5 
       dc.[DataPointID]
      ,[DatapointDate]
      ,dc.[DataPointValue] AS [DVOld]
      ,CASE sc.ScaleType
            WHEN '*' THEN dc.[DataPointValue] * sc.ScaleValue
            WHEN '/' THEN dc.[DataPointValue] / sc.ScaleValue
            WHEN '+' THEN dc.[DataPointValue] + sc.ScaleValue
            WHEN '-' THEN dc.[DataPointValue] - sc.ScaleValue
            ELSE dc.[DataPointValue]
        END
        AS [DatapointValue]
 FROM [tblTSS_DataCollection] dc
 JOIN [tblTSS_ScaleSettings] sc
 on sc.DatapointID = dc.DatapointID
 Where dc.DatapointID = 1093

哪个会返回:

DataPointID DatapointDate            DVOld        DatapointValue
1093        2012-07-29 00:00:01.000  0.01869818   0.1869818
1093        2012-07-29 00:01:01.000  0.01882841   0.1882841
1093        2012-07-29 00:02:01.000  0.01895865   0.1895865
1093        2012-07-29 00:03:01.000  0.01908888   0.1908888
1093        2012-07-29 00:04:01.000  0.01921912   0.1921912

但是,这有什么问题是因为表中的缩放 EffectiveDate 直到 00:03:01 才开始缩放,而不是在所有记录上开始缩放。在下一个生效日期之前,缩放比例应该是那个比例。有时我们会发生多个规模,并且在一年中的不同时间发生变化。所以我需要 Select Query 来计划……这就是它变得棘手的地方。

看起来像这样:

DataPointID DatapointDate            DVOld        DatapointValue
1093        2012-07-29 00:00:01.000  0.01869818   0.01869818
1093        2012-07-29 00:01:01.000  0.01882841   0.01882841
1093        2012-07-29 00:02:01.000  0.01895865   0.01895865
1093        2012-07-29 00:03:01.000  0.01908888   0.1908888
1093        2012-07-29 00:04:01.000  0.01921912   0.1921912

有人可以帮忙吗?

4

2 回答 2

1

这样的事情可能对你有用:

SELECT TOP 5 
       dc.DataPointID
      ,DatapointDate
      ,dc.DataPointValue AS DVOld
      ,CASE sc.ScaleType
            WHEN '*' THEN dc.DataPointValue * sc.ScaleValue
            WHEN '/' THEN dc.DataPointValue / sc.ScaleValue
            WHEN '+' THEN dc.DataPointValue + sc.ScaleValue
            WHEN '-' THEN dc.DataPointValue - sc.ScaleValue
            ELSE dc.DataPointValue
        END
        AS DatapointValue
 FROM tblTSS_DataCollection dc
 LEFT JOIN tblTSS_ScaleSettings sc
 ON sc.DatapointID = dc.DatapointID
 AND sc.EffectiveDate = (
    SELECT MAX(EffectiveDate)
    FROM tblTSS_ScaleSettings
    WHERE DatapointID = dc.DatapointID
        AND EffectiveDate <= dc.DatapointDate
 )
 WHERE dc.DatapointID = 1093
于 2012-12-10T21:05:13.647 回答
0

像这样的from条款会起作用吗?

 FROM [tblTSS_DataCollection] dc
 JOIN sc  on sc.DatapointID = dc.DatapointID inner join
(
    select datapointid, max(effectivedate) as max_dt
    from
        [tblTSS_ScaleSettings] 
    where
        effectiveDate <= getdate()
    group by datapointID
) mx on
sc.datapointid = mx.datapointid and
sc.effectivedate = mx.max_dt

您输入的内容应相当于:

SELECT TOP 10
       dc.[DataPointID]
      ,[DatapointDate]
      ,dc.[DataPointValue] AS [DVOld]
      ,EffectiveDate
    ,ScaleType
    ,ScaleValue
    ,CASE ScaleType 
            WHEN '*' THEN dc.[DataPointValue] * ScaleValue 
            WHEN '/' THEN dc.[DataPointValue] / ScaleValue 
            WHEN '+' THEN dc.[DataPointValue] + ScaleValue 
            WHEN '-' THEN dc.[DataPointValue] - ScaleValue 
            ELSE dc.[DataPointValue]
        END
        AS [DatapointValue]

  FROM [tblTSS_DataCollection] dc inner join
(select DatapointID, max(EffectiveDate) as max_effective,
from tblTSS_ScaleSettings ts
    where ts.EffectiveDate <= dc.DatapointDate
group by DatapointID) mx
on dc.datapointid = mx.datapointid and
EffectiveDate = max_effective 
  Where dc.DatapointID = 1093
于 2012-12-10T20:46:36.560 回答