2010-09-10 42 views
1

我稱之爲表table1,它看起來像下面的組SQL Server中的第2005

record type tran_ref_number amount    customer_name 

    TRN     123  15000     sara 
    TRN     234  25000     inba 
    TRN     345  20000     rajiv 
    TRN     456  16000     rahul 
    TRN     567  34556     sathish 
    TRN     678  15000     ilango 
    TRN     123  15000     sara 
    TRN     234  25000     inba 
    TRN     345  20000     rajiv 
    TRN     456  16000     rahul 
    TRN     567  34556     sathish 
    TRN     678  15000     ilango 

我想要的「的基礎上,從該表中刪除記錄,如果量的總和超過70000 /每位顧客「。 結果應根據客戶名稱進行分組。

任何人有任何想法可以幫助我解決這個問題。

在此先感謝。

+0

[by子句查詢組]的可能重複(http://stackoverflow.com/questions/3677394/group-by-clause-query) – 2010-09-10 11:48:51

回答

0
select customer_name 
from table1 t 
group by customer_name 
having sum(amount) > 70000 
+0

我downvoted此答案因爲它不回答問題,需要修復或刪除。而不是downvoting在頁面上的其他2個答案,你應該看看,看看爲什麼你的答案downvoted和修復或刪除。 – 2010-09-10 11:34:06

+0

@Martin我也把這個評論標記爲冒犯性的。因爲它是。 – 2010-09-10 12:15:22

+0

不會冒犯任何合理的人。說實話,我發現你對邁克爾的回答的評論顯得更爲冒犯。他打算複製誰? – 2010-09-10 12:19:45

0
DELETE FROM Table1 
    WHERE CUSTOMER_NAME IN (SELECT customer_name 
    from table1 t 
    group by customer_name 
    having sum(amount) > 70000 
) 
+0

+1中和不明原因的downvote。 – 2010-09-10 11:29:03

+0

我低估了這個答案,因爲它是一個模仿者。 – 2010-09-10 12:12:01

1
;with cte as 
(
SELECT SUM(amount) OVER (PARTITION BY customer_name) a 
FROM table1 
) 
DELETE FROM cte WHERE a > 70000 
+0

@Martin去爬樹。 – 2010-09-10 12:02:24

+0

@Denis - 長大。 – 2010-09-10 12:07:32

+0

我低估了你的答案,因爲你低估了我的答案,因爲你認爲沒有比完整的,擴展的答案更好的答案,但事實並非如此。 – 2010-09-10 12:13:11