Tsql 基于日期的联接查询
我有一个表,有时间刻度值,我需要能够刻度值。我试图让这一切尽可能简单,但执行速度对我来说是一个重要因素 让我给您举一个tblTSS_数据收集的示例:Tsql 基于日期的联接查询,tsql,sql-server-2005,Tsql,Sql Server 2005,我有一个表,有时间刻度值,我需要能够刻度值。我试图让这一切尽可能简单,但执行速度对我来说是一个重要因素 让我给您举一个tblTSS_数据收集的示例: SELECT TOP 5 [DataPointID] ,[DatapointDate] ,dc.[DataPointValue] FROM [tblTSS_DataCollection] dc Where DatapointID = 1093 这将返回一个非常简单的表: DataPointID DatapointDate
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.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
它将返回如下结果:
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.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
现在我需要做的是这样的事情:
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.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
这将返回:
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
但是,这样做的错误在于,表中的scaling EffectiveDate直到00:03:01才开始缩放,而不是在所有记录上开始缩放。缩放比例应为该比例,直到下一个生效日期。有时,我们会有多种规模的发生,它在一年中的不同时间变化。所以我需要Select查询来计划。。。。这就是它变得棘手的地方
看起来是这样的:
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.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
有人能帮忙吗?像这样的从句行吗
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
这样的东西可能适合你:
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