2013-07-23 15 views
0

我想返回一個結果集,該結果集返回具有多個供應商更改的ID的開始生效日期和結束生效日期。爲此,我正在查看一個記錄該id,供應商id和交易發生日期的交易表。如果id已切換供應商,我想退休舊協會並記錄新協會。我的意圖是插入一個新行,其中最新的切換日期作爲開始生效日期,並將空值作爲最終生效日期。要完成該事件,我想要更新最後一個前一行,並在最後生效日期填入最新的切換日期。在我有事務的情況下,但是ID沒有切換供應商時,我想忽略該行。SQL:未能按查詢返回有效的開始/結束日期

我對單個id有效,但是,當我添加第二個id時,順序/分區不起作用。

以下是生成測試行的腳本。注意用於單個id的sql。

-- Note: use this to emulate the known switched suppliers table 

    create table #switched 
    (lcard bigint); 

    insert into #switched (lcard) values (700382) 
    insert into #switched (lcard) values (832019) 

    select * from #switched 

    -- Note: this temp data represents a previously grouped/partitioned table 
      --  prepped for this next phase of action 

    create table #PartitionTest 
    ( FauxId int, 
     lcard bigint, 
     suppId int, 
     switchDate datetime 
    ); 


    INSERT INTO #PartitionTest (FauxId,lcard,suppId,switchDate) VALUES (1,700382,506,cast('Jun 23 2013 12:00AM' as datetime)) 
    INSERT INTO #PartitionTest (FauxId,lcard,suppId,switchDate) VALUES (2,700382,49401,cast('May 22 2013 12:00AM' as datetime)) 
    INSERT INTO #PartitionTest (FauxId,lcard,suppId,switchDate) VALUES (3,700382,49401,cast('May 4 2013 12:00AM' as datetime)) 
    INSERT INTO #PartitionTest (FauxId,lcard,suppId,switchDate) VALUES (4,700382,49401,cast('May 2 2013 12:00AM' as datetime)) 
    INSERT INTO #PartitionTest (FauxId,lcard,suppId,switchDate) VALUES (5,700382,49401,cast('Apr 26 2013 12:00AM' as datetime)) 
    INSERT INTO #PartitionTest (FauxId,lcard,suppId,switchDate) VALUES (6,700382,49401,cast('Mar 15 2013 12:00AM' as datetime)) 
    INSERT INTO #PartitionTest (FauxId,lcard,suppId,switchDate) VALUES (1,832019,27088,cast('Jun 18 2013 12:00AM' as datetime)) 
    INSERT INTO #PartitionTest (FauxId,lcard,suppId,switchDate) VALUES (2,832019,232,cast('May 24 2013 12:00AM' as datetime)) 
    INSERT INTO #PartitionTest (FauxId,lcard,suppId,switchDate) VALUES (3,832019,232,cast('May 23 2013 12:00AM' as datetime)) 
    INSERT INTO #PartitionTest (FauxId,lcard,suppId,switchDate) VALUES (4,832019,232,cast('May 22 2013 12:00AM' as datetime)) 
    INSERT INTO #PartitionTest (FauxId,lcard,suppId,switchDate) VALUES (5,832019,232,cast('May 21 2013 12:00AM' as datetime)) 
    INSERT INTO #PartitionTest (FauxId,lcard,suppId,switchDate) VALUES (6,832019,232,cast('May 17 2013 12:00AM' as datetime)) 
    INSERT INTO #PartitionTest (FauxId,lcard,suppId,switchDate) VALUES (7,832019,232,cast('May 16 2013 12:00AM' as datetime)) 

    -- Note: Order results by lcard, then order the supplier id by 
     --  the transaction date found. FauxId is from the previous partitioning 

    select * from #PartitionTest 
    order by lcard,fauxId, suppId, switchDate 

    -- This is the statement that is failing when attempting to utilize 
     -- the ids in #switched as the criterion processing sets of ids. 

    ;with sifted 
     as (select *, 
      row_number() over (ORDER BY switchDate) - row_number() over (PARTITION BY suppId ORDER BY switchDate) as G 
     from #PartitionTest 
     where lcard in 
     (select lcard 
      from #switched 
     ) 
     -- // DEBUG TEST: specific card holder(s) 
     -- NOTE: when both lcards are used, the beginEffDate/endEffDate goal fails 
     -- and lcard in ('8320198','7003824') 
     -- NOTE: this represents the intent 
     and lcard in ('832019') 
     ), 
     refined as 
     (select lcard, 
       suppId, 
       MIN(switchDate) BeginEffDate, 
       ROW_NUMBER() OVER (ORDER BY min(switchDate)) as OrgSplit 
     from sifted 
      group by lcard,suppId, G) 
    select a.lcard, 
      a.suppId, 
      a.BeginEffDate, 
      b.BeginEffDate as EndEffDate 
    from refined a 
      left join refined b 
      on a.OrgSplit + 1 = b.OrgSplit 
    order by a.lcard, a.suppId 



    -- drop table #switched; 
    -- drop table #PartitionTest; 

=========================================== =======================

EDITED

這裏是理想的結果:

enter image description here

+0

你的例子只有一個'select'。然而你說你想做一個'insert'和'update'。你是什​​麼意思,它「工作」爲一個單一的ID?你能給出你提供的數據預期輸出的例子嗎? –

+0

@Gordon Linoff,你是對的 - 我沒有添加任何插入/更新,只是爲了方便查看將從本質上驅動兩個操作的select語句。 – plditallo

回答

0

在SQL Server 2012中,我有dense_rank()選項,而不是使用partition/over進行工作。 解決方案:

WITH R AS (
    SELECT 
     lcard, 
     suppId, 
     switchDate, 
     DENSE_RANK() OVER(PARTITION BY lcard ORDER BY switchDate) - 
     DENSE_RANK() OVER(PARTITION BY lcard ORDER BY suppId, switchDate) AS grp 
    FROM 
     #PartitionTest 
    ), 
    S AS (
    SELECT 
     lcard, 
     suppId, 
     MAX(switchDate) AS dt, 
     ROW_NUMBER() OVER(PARTITION BY lcard ORDER BY MAX(switchDate)) AS rn 
    FROM 
     R 
    GROUP BY 
     lcard, 
     suppId, 
     grp 
    ) 
    SELECT 
     A.lcard, 
     A.suppId, 
     A.dt AS BeginEffDate, 
     B.dt AS EndEffDate 
    FROM 
     S AS A 
     LEFT OUTER JOIN 
     S AS B 
     ON A.lcard = B.lcard 
     AND A.rn = B.rn - 1 
    ORDER BY 
     lcard, 
     BeginEffDate 
    GO 
0

所有你有要做的就是把sifted CTE中的一行改爲:

 row_number() over (ORDER BY switchDate) - row_number() over (PARTITION BY lcard, suppId ORDER BY switchDate) as G 

注意lcard,添加到PARTITION BY。

整個語句將是這樣的:

;with sifted 
    as (select *, 
     row_number() over (ORDER BY switchDate) - row_number() over (PARTITION BY lcard, suppId ORDER BY switchDate) as G 
    from #PartitionTest 
    where lcard in 
    (select lcard 
     from #switched 
    ) 
    -- // DEBUG TEST: specific card holder(s) 
    -- NOTE: when both lcards are used, the beginEffDate/endEffDate goal fails 
    -- and lcard in ('8320198','7003824') 
    -- NOTE: this represents the intent 
    and lcard in (832019,700382) 
    ), 
    refined as 
    (select lcard, 
      suppId, 
      MIN(switchDate) BeginEffDate, 
      ROW_NUMBER() OVER (ORDER BY min(switchDate)) as OrgSplit 
    from sifted 
     group by lcard,suppId, G) 

選擇a.lcard, a.suppId, a.BeginEffDate, b.BeginEffDate作爲EndEffDate 精製而成一個 左加入精製b 在a.OrgSplit + 1 = b.OrgSplit order by a.lcard,a.suppId

只要我看到混合了lcard,suppId組合,我知道問題是

+0

我們得到了相同的結果集70082,506,2013-06-23,null是正確的 - 700382,49401的2個條目應該減少到只有1:700382,49401,2013-03-15,2013 -06-23。 – plditallo

+0

...和... 832019應減至2-832019,27088,2013-06-23,null和832019,232,2013-05-16,2013-06-23。感謝您在這裏看到的 – plditallo

+0

。 – plditallo

相關問題