2017-11-18 4 views
1

2つのテーブルがあり、CTEを使用する必要があります。2つのテーブルでCTEを使用する必要があります

私はdActiveDatetable_1で最大であり、table_1.dcidKala = table_2.dcidKalaため​​よう、Table_1から行のTable_2の行のセットを必要としています。 table_2の行ごとに

CREATE TABLE #Table_1 
(
    dcidKala    INT, 
    dcPercentDiscount  FLOAT, 
    dActiveDate   DATE 
) 

CREATE TABLE #Table_2 
(
    dcRow   INT, 
    dcidKala  INT, 
    dcNum   FLOAT, 
    dDateFactor  DATE 
) 

INSERT INTO #Table_1 
    (
    dcidKala, 
    dcPercentDiscount, 
    dActiveDate 
) 
VALUES 
(109,10,'2017-08-23'), 
(109, 15, '2017-10-12'), 
(100, 20, '2017-01-20'), 
(102, 20, '2017-01-20') 

INSERT INTO #Table_2 
    (
    dcRow, 
    dcidKala, 
    dcNum, 
    dDateFactor 
) 
VALUES 
(1,109,1, '2017-10-05' ), 
(2, 109, 2, '2017-10-07'), 
(3, 109, 1, '2017-10-14'), 
(4, 109, 5, '2017-10-19'), 
(5, 100, 2, '2017-01-25') 

;WITH cte AS 
(
    SELECT th.dcPercentDiscount, 
      tb.dcRow, 
      ROW_NUMBER() OVER(PARTITION BY th.dcidKala, tb.dcRow ORDER BY th.dActiveDate) AS 
      rn 
    FROM #Table_1 th 
      INNER JOIN #Table_2 tb 
       ON tb.dcidKala = th.dcidKala 
       AND tb.dDateFactor >= th.dActiveDate 
) 

SELECT * 
FROM #Table_2 t2 
     LEFT JOIN cte t3 
      ON t2.dcRow = t3.dcRow 
      AND t3.rn = 1 


DROP TABLE [#Table_1] 
DROP TABLE [#Table_2] 

--result myCode is: 
    --1 109 1 2017-10-05 10 1 1 
    --2 109 2 2017-10-07 10 2 1 
    --3 109 1 2017-10-14 10 3 1 
    --4 109 5 2017-10-19 10 4 1 
    --5 100 2 2017-01-25 20 5 1 

    --Rows 3 and Rows 4 is wrong 

    --i need this result : 
    -- on Result From table_1 on table_2 
    --1 109 1 2017-10-05 10 1 1 
    --2 109 2 2017-10-07 10 2 1 
    --3 109 1 2017-10-14 15 3 1 
    --4 109 5 2017-10-19 15 4 1 
    --5 100 2 2017-01-25 20 5 1 

dActiveDateと小さいdDateFactor

上で最大であるのtable_1からわずか1つの結果

あなたはこれを使用することができますあなたの

+0

どの[DBMS](https://en.wikipedia.org/wiki/DBMS)製品を使用していますか? Postgres?オラクル? "_SQL_"はクエリ言語であり、特定のデータベース製品の名前ではありません。 –

答えて

1

に感謝し、私を助けてください。

;WITH cte AS 
(
    SELECT 
     th.dcPercentDiscount, tb.dcRow, 
     ROW_NUMBER() OVER(PARTITION BY th.dcidKala, tb.dcRow ORDER BY th.dActiveDate DESC) AS rn 
    FROM 
     Table_1 th 
    INNER JOIN 
     Table_2 tb ON tb.dcidKala = th.dcidKala 
        AND tb.dDateFactor >= th.dActiveDate 
) 
SELECT * 
FROM Table_2 t2 
LEFT JOIN cte t3 ON t2.dcRow = t3.dcRow and t3.rn=1 
+0

応答のためにありがとう 私はDiffrence Dataでコードをテストし、残念ながら間違っています.... あなたはそれを編集できますか? –

+0

あなたの別のデータも共有できますか? –

+0

私は更新を行った。 –

1
CREATE TABLE #Table_1 (dcidKala   INT, 
         dcPercentDiscount FLOAT, 
         dActiveDate  DATE) 

CREATE TABLE #Table_2 (dcRow  INT, 
         dcidKala INT, 
         dcNum  FLOAT, 
         dDateFactor DATE) 

INSERT INTO #Table_1 (dcidKala, 
         dcPercentDiscount, 
         dActiveDate) 
VALUES (100, 10, '2017-01-01'), 
(101, 15, '2017-01-02'), 
(100, 20, '2017-01-20'), 
(102, 20, '2017-01-20') 

INSERT INTO #Table_2 (dcRow, 
         dcidKala, 
         dcNum, 
         dDateFactor) 
VALUES (1, 100, 1, '2017-01-05'), 
(2, 100, 2, '2017-01-09'), 
(3, 101, 1, '2017-01-01'), 
(4, 101, 5, '2017-01-20'), 
(5, 100, 2, '2017-01-25') 

SELECT * 
FROM [#Table_2] AS [t2] 
CROSS APPLY ( SELECT TOP 1 * 
       FROM ( SELECT TOP 1 [t1].[dcidKala], 
            [t1].[dcPercentDiscount], 
            [t1].[dActiveDate] 
          FROM [#Table_1] AS [t1] 
          WHERE [t1].[dcidKala] = [t2].[dcidKala] 
            AND [t2].[dDateFactor] >= [t1].[dActiveDate] 
          ORDER BY [t1].[dActiveDate] DESC 
          UNION ALL 
          SELECT NULL, 
            NULL, 
            NULL) t3 
       ORDER BY CASE 
          WHEN [t2].[dcidKala] IS NOT NULL THEN 0 
          ELSE 1 
         END) AS t1 

DROP TABLE [#Table_1] 
DROP TABLE [#Table_2] 
関連する問題