2012-12-10 76 views
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縮放應該開始,而不是所有記錄。縮放比例應該是該比例,直到下一個生效日期爲止。有時候我們會有多個標準發生,並在一年中的不同時間發生變化。所以我需要選擇查詢來計劃這個......這是棘手的地方。

將目光像這樣:

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 

是否有人可以幫忙嗎?

回答

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 
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 
+0

這不起作用 –