2013-10-18 81 views
0

對於我的下一個把戲,我想只選擇每個客戶端的最近的事件。我想要一個,而不是四個000017的事件。TSQL OVER(PARTITION BY ...)

OK c_id e_date e_ser e_att e_recip Age c_cm e_staff rn 
--> 000017 2013-04-02 00:00:00.000 122 1 1 36 90510 90510 15 
--> 000017 2013-02-26 00:00:00.000 122 1 1 36 90510 90510 20 
--> 000017 2013-02-12 00:00:00.000 122 1 1 36 90510 90510 24 
--> 000017 2013-01-29 00:00:00.000 122 1 1 36 90510 90510 27 
--> 000188 2012-11-02 00:00:00.000 160 1 1 31 1289 1289 44 
--> 001713 2013-10-01 00:00:00.000 142 1 1 26 2539 2539 1 
--> 002531 2013-07-12 00:00:00.000 190 1 1 61 1689 1689 21 
--> 002531 2013-06-14 00:00:00.000 190 1 1 61 1689 1689 30 
--> 002531 2013-06-07 00:00:00.000 190 1 1 61 1689 1689 31 
--> 002531 2013-05-28 00:00:00.000 122 1 1 61 1689 1689 33 

這裏是一個讓我到這個階段查詢(也許你有一些建議,以改善這個問題,以及,多餘的嵌套查詢創建T2表可能是過度的。)謝謝大家!

SELECT TOP(10)* 
FROM (

    SELECT * 
    FROM (

    SELECT (SELECT CASE WHEN 
    (e_att IN (1,2) 
    AND e_date > DATEADD(month, -12, getdate()) 
    AND e_ser NOT IN (100,115) 
    AND e_recip NOT IN ('2','7') 
    AND (((e_recip = '3') AND (DATEDIFF(Year, c_bd, GetDate())>10)) OR (e_recip <> '3')) 
    AND c_cm = e_staff) 
    THEN '-->' 
    WHEN 1=1 THEN '' 
    END 
    ) AS 'OK' 
    ,c_id, e_date, e_ser, e_att, e_recip, DATEDIFF(Year, c_bd, GetDate()) AS 'Age', c_cm, e_staff 
    ,row_number() OVER (PARTITION BY c_id ORDER BY e_date DESC) rn    
    FROM events INNER JOIN client ON e_case_no = c_id 
    LEFT OUTER JOIN doc ON doc.doc_dbid = client.c_id 
    WHERE client.c_id IN (/* confidential query */) 
    AND e_date > DATEADD(month, -12, getdate()) 
    AND e_ser BETWEEN 11 AND 1000 
    GROUP BY  c_id, e_date, e_ser, e_att, e_recip, c_bd, c_cm, e_staff 
    ) t1 
) t2 
WHERE   OK = '-->' 
ORDER BY  c_id, e_date DESC 
+0

問題是什麼? – McGarnagle

+0

嗨McGarnagle,如何爲每個客戶選擇最近的事件?謝謝。 – PowderSnorkel

+0

我剛剛嘗試添加另一個PARTITION BY到最外層選擇,但我得到一個錯誤「無效的列名'rn2'。」 – PowderSnorkel

回答

2

它看起來像下面產生的行號,按日期排序,每個客戶端:

) t1 
    WHERE rn = 1 
) t2 
+0

甜!你達人! (或女性)嗯,這種評論可能會讓一個人陷入麻煩。不管怎樣,謝謝。 – PowderSnorkel

+1

@PowderSnorkel「男人」在這個網站上是一個相當安全的賭注,我想。 – McGarnagle

+0

:)週末愉快! – PowderSnorkel

0

,row_number() OVER (PARTITION BY c_id ORDER BY e_date DESC) rn    

因此增加where rn=1應該每個客戶端產生最近的事件這裏是你的原始查詢了一些改進:

SELECT TOP(10) * 
FROM (

    SELECT '-->' AS 'OK' -- always this see where. 
    ,c_id, e_date, e_ser, e_att, e_recip, DATEDIFF(Year, c_bd, GetDate()) AS 'Age', c_cm, e_staff 
    ,row_number() OVER (PARTITION BY c_id ORDER BY e_date DESC) rn    
    FROM events INNER JOIN client ON e_case_no = c_id 
    LEFT OUTER JOIN doc ON doc.doc_dbid = client.c_id 
    WHERE client.c_id IN (/* confidential query */) 
      -- this part was in case and then filtered for later, if we put it in where now more efficient 
      (e_att IN (1,2) AND e_date > DATEADD(month, -12, getdate()) 
      AND e_ser NOT IN (100,115) 
      AND (((e_recip = '3') AND DATEDIFF(Year, c_bd, GetDate()>10)) OR e_recip NOT IN ('2', '3', '7')) 
      AND c_cm = e_staff) 


    AND e_date > DATEADD(month, -12, getdate()) 
    AND e_ser BETWEEN 11 AND 1000 
    GROUP BY  c_id, e_date, e_ser, e_att, e_recip, c_bd, c_cm, e_staff 
) t2 
ORDER BY  c_id, e_date DESC 

而且除去一些非必要的括號,如果從CASE語句移動的東西的,你不需要它過濾外部查詢,這使得它更簡單。

添加在ROW_NUMBER聲明從McGarnagle的答案,你應該得到你想要的結果。

相關問題