2017-11-16 5 views
0

私はそれの下の1行と1行の差を計算し、結果をxmlにする必要があるテーブルを持っています。それは毎日の仕事なので、私はそれが再帰的な仕事の一種である必要があります。私の現在のテーブルのSQL Server 2008のLead()およびLag()関数でAlternateを使用する方法はありますか?

構造は以下の通りです:

CREATE TABLE #Temp 
(

    , CurrentDateTime  DateTime 
    , ID      INT 
    , ThisYearToDateTotal INT 
    , ThisYearToDateCBT  INT 
    , ThisYearToDateManual INT 
    , ThisYearToDateScanned INT 
    , InProcess    INT 
    , InputRequired   INT 
)` 

これまでのところ、私は以下のようにコードを書いている: enter image description here

:以下のように

SELECT 
    Today_CurrentDateTime 
, Today_Total 
, Today_CBT 
, Today_Manual 
, Today_Scanned 
, Today_InProcess 
, Today_InputRequired 
, Yesterday_Total 
, Yesterday_CBT 
, Yesterday_Manual 
, Yesterday_Scanned 
, Yesterday_InProcess 
, Yesterday_InputRequired 
, (TD.Today_Total   - YD.Yesterday_Total)   AS Diff_in_Total 
, (TD.Today_CBT   - YD.Yesterday_CBT)   AS Diff_in_CBT 
, (TD.Today_Manual  - YD.Yesterday_Manual)   AS Diff_in_Manual 
, (TD.Today_Scanned  - YD.Yesterday_Scanned)  AS Diff_in_Scanned 
, (TD.Today_InProcess  - YD.Yesterday_InProcess)  AS Diff_in_InProcess 
, (TD.Today_InputRequired - YD.Yesterday_InputRequired) AS Diff_in_InputRequired 
FROM #YesterdayData AS YD 
INNER JOIN #TodayData AS TD ON TD.Today_ID = YD.Yesterday_ID 

と出力を取得します

ここでは、別の固定テーブルを作成できないという制限があります。そのため、毎日の差分を計算することができないのです1週間で

ヘルプ

+0

は、このブログのエントリを見てみましょう:https://blog.sqlauthority.com/2011/11/24/sql-server-solution-to-puzzle-simulate-lead-and-lag-without- using-sql-server-2012-analytic-function/ –

+0

CTE with ROW_NUMBER()。あなたのデータのIDは何ですか?グループ化できる行ごとに個別の値がありますか?これらの列名はどこから取得していますか?あなたはすべてのコードを表示していません – scsimon

+0

@RigertaDemiri IDはID列です。私は3つ以上の#Tempテーブルを作成し、異なる列名で名前を変更しました。 – Peter

答えて

0

サブクエリを使用してLAGとLEADを模倣するのはかなり簡単です。

#Tempテーブルについては、ThisYearToDateTotalの前または次の行の値(order by CurrentDateTime)を取得してください。ここで

は簡単な例です:

SELECT ID, 
     CurrentDateTime, 
     ThisYearToDateTotal, 
     (
      SELECT TOP 1 ThisYearToDateTotal 
      FROM #Temp as tLag 
      WHERE tLag.ID = tMain.Id -- partition by 
      AND tLag.CurrentDateTime < tMain.CurrentDateTime 
      ORDER BY CurrentDateTime DESC 
     ) As Lag_ThisYearToDateTotal, 
     (
      SELECT TOP 1 ThisYearToDateTotal 
      FROM #Temp as tLead 
      WHERE tLead.ID = tMain.Id -- partition by 
      AND tLead.CurrentDateTime > tMain.CurrentDateTime 
      ORDER BY CurrentDateTime 
     ) As Lead_ThisYearToDateTotal 
FROM #Temp as tMain 
+0

あなたの助けをありがとう...鉛、ラグは私のために模倣することは決して簡単でした。 – Peter

1

日付がIDフィールドIDの順に入力されている場合は、以前のIDで同じ#TEMPテーブルとインナー参加することができます。

CREATE TABLE #Temp 
( CurrentDateTime  DateTime 
    , ID      INT 
    , ThisYearToDateTotal INT 
    , ThisYearToDateCBT  INT 
    , ThisYearToDateManual INT 
    , ThisYearToDateScanned INT 
    , InProcess    INT 
    , InputRequired   INT 
) 

INSERT INTO #Temp VALUES 
('2017-11-14 07:50:25.230', 1, 400000, 50000, 20000, 30000, 1000, 700) 
,('2017-11-15 07:50:25.230', 2, 460000, 53000, 26000, 38000, 2000, 1400) 
,('2017-11-16 07:53:01.943', 3, 469692, 53904, 26755, 389033, 2026, 1489) 
,('2017-11-17 07:53:01.943', 4, 469692, 53904, 26755, 389033, 2026, 1489) 

DELETE FROM #Temp WHERE ID = 3 

SELECT T.CurrentDateTime 
    , TPrev.ThisYearToDateTotal - T.ThisYearToDateTotal [Total Diff] 
    , TPrev.ThisYearToDateCBT - T.ThisYearToDateCBT [ThisYearToDateCBT Diff] 
    , TPrev.ThisYearToDateManual - T.ThisYearToDateManual [ThisYearToDateManual Diff] 
    , TPrev.ThisYearToDateScanned - T.ThisYearToDateScanned [ThisYearToDateScanned Diff] 
    , TPrev.InProcess - T.InProcess [InProcess Diff] 
    , TPrev.InputRequired - T.InputRequired [InputRequired Diff] 
    FROM #Temp AS T LEFT JOIN #Temp AS TPrev ON TPrev.ID = (SELECT MAX(T2.ID) 
                  FROM #Temp T2 
                  WHERE T2.ID > T.ID) 
ORDER BY T.ID 

--DROP TABLE #Temp 
+0

仲間に感謝@zorkolotその完全で理解しやすい!ちょっと変わったINNER JOIN #Temp AS TPrev ON T.ID + 1 = TPrev.ID – Peter

+1

これにはギャップがないID列が必要です。 ID値が1,2,3の3つの行があり、IDが2の行が削除された場合、クエリは結果をまったく取得しません。 –

+0

@ ZoharPeledありがとう、私はクエリを編集しました。 – Zorkolot

関連する問題