2012-10-20 20 views
0

我米試圖實現標誌設置爲低於SQL - 從組標誌二傳手最大

p_id  mon_year  e_id  flag 
----  ---------  ----- ----- 
1   2011/11  20  0 
1   2011/11  21  1 
1   2012/01  22  1 
1   2012/02  23  0 
1   2012/02  24  0 
1   2012/02  25  1 
2   2011/11  28  0 
2   2011/11  29  1 
2   2012/01  30  1 

分組通過的p_id,E_ID和mon_year我的表的條件下,該標誌設置爲當月的最後一個值。

我米困惑我怎樣才能實現這一點

我試圖通過使用ROW_NUMBER和分區獨立出來的值來實現這一點。通過再利用ROW_NUMBER查詢尋找到實現

輸出,我已經得到的是如下:

分組該值將設置標誌柱的

p_id  mon_year  e_id  row 
----  ---------  ----- ----- 
1   2011/11  20  1 
1   2011/11  21  2 
1   2012/01  22  1 
1   2012/02  23  1 
1   2012/02  24  2 
1   2012/02  25  3 
2   2011/11  28  1 
2   2011/11  29  2 
2   2012/01  30  1 

最大。但我真的搞砸了如何實現它。任何幫助都會有用。

謝謝!

+0

怎麼辦你的意思是「爲條件實現標誌設置」? –

+0

你可以使用你的分區的技巧,但改變順序desc - 然後,而不是最大值,你想第一個,它將永遠是1. – JohnLBevan

+0

設置標誌(1)只爲最後一個mon_year值提供p_id是相同的e_id是不同的。 – user1141584

回答

0

我認爲這是你要去的。 。 。輸出正是你的例子匹配:

declare @t table (p_id int, [year] int, [month] int, [day] int) 
insert @t select 1, 2011, 11, 20 
    union select 1, 2011, 11, 21 
    union select 1, 2012, 01, 22 
    union select 1, 2012, 02, 23 
    union select 1, 2012, 02, 24 
    union select 1, 2012, 02, 25 
    union select 2, 2011, 11, 28 
    union select 2, 2011, 11, 29 
    union select 2, 2012, 01, 30 

select p_id, [year], [month], [day] 
, case when r=1 then 1 else 0 end flag 
from 
(
    select p_id, [year], [month], [day] 
    , row_number() over (partition by p_id, [year], [month] order by [day] desc) r 
    from @t 
) x 
order by p_id, [year], [month], [day] 

輸出:

p_id year month day  flag 
1  2011 11  20  0 
1  2011 11  21  1 
1  2012 1  22  1 
1  2012 2  23  0 
1  2012 2  24  0 
1  2012 2  25  1 
2  2011 11  28  0 
2  2011 11  29  1 
2  2012 1  30  1 
0

嘗試按降序排序。這樣,你不必尋找最大的ROW_NUMBER,但是當ROW_NUMBER是1時);

這樣的事情(我沒有完全理解你想達到什麼,所以這可能不是100%準確的) :

WITH r_MyTable 
AS 
(
    SELECT 
     *, 
     ROW_NUMBER() OVER (PARTITION BY mon_year ORDER BY p_id, e_id DESC) AS GroupRank 
    FROM MyTable 
) 
UPDATE r_MyTable 
SET flag = CASE WHEN GroupRank = 1 THEN 1 ELSE 0 END; 
0

您可以使用E_ID最大語句獲取最後一個值月,代碼如下:

IF OBJECT_ID('tempdb..#tmptest') IS NOT NULL 
     DROP TABLE #tmptest 

    SELECT 
     * 
    INTO 
     #tmptest 
    FROM 
    (
     SELECT '1' p_id,   '2011/11' mon_year,  '20' e_id,  '0' flag UNION ALL 
     SELECT '1',   '2011/11',  '21',  '1' UNION ALL 
     SELECT '1',   '2012/01',  '22',  '1' UNION ALL 
     SELECT '1',   '2012/02',  '23',  '0' UNION ALL 
     SELECT '1',   '2012/02',  '24',  '0' UNION ALL 
     SELECT '1',   '2012/02',  '25',  '1' UNION ALL 
     SELECT '2',   '2011/11',  '28',  '0' UNION ALL 
     SELECT '2',   '2011/11',  '29',  '1' UNION ALL 
     SELECT '2', '2012/01',  '30',  '1' 
    ) as tmp 

    SELECT 
     tmptest.* 
    FROM 
     (
      SELECT 
       MAX(e_id) e_id 
       ,p_id 
       ,mon_year 
      FROM 
       #tmptest 
      GROUP BY 
       p_id,mon_year 
     ) tblLastValueEID 
    INNER JOIN 
     #tmptest tmptest 
    ON 
     tmptest.p_id = tblLastValueEID.p_id 
    AND 
     tmptest.mon_year = tblLastValueEID.mon_year 
    AND 
     tmptest.e_id = tblLastValueEID.e_id