我有一个具有时间缩放值的表,我需要能够缩放值。我试图让这个尽可能简单,但执行速度对我来说是一个很大的参与者。
让我举一个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才开始缩放,然后才开始缩放,而不是所有记录。缩放应该是那个缩放直到下一个有效日期。有时我们会有多个Scales发生,并且它会在一年中的不同时间发生变化。所以我需要选择查询来计划......这就是它变得棘手的地方。
看起来像这样:
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
有人可以帮忙吗?
答案 0 :(得分: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
答案 1 :(得分: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