views:

50

answers:

3

i have table called table1 and it looks like the below

    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

i want to delete records from this table on the basis of "if the sum of the amount is exceeding 70000/per customer". the result should be grouped on the basis of customer name.

Any one who has any idea about this can help me out on this issue.

thanks in advance.

A: 
select customer_name
from table1 t
group by customer_name
having sum(amount) > 70000
Denis Valeev
@Denis - I downvoted this answer as it does not answer the question and needs to be fixed or deleted. Instead of downvoting the other 2 answers on the page you should look and see why your answer got downvoted and fix or remove.
Martin Smith
@Martin I also flagged this comment as offensive. Because it is.
Denis Valeev
@Denis - Not offensive to any reasonable person. To be honest I found your comment on Michael's answer considerably more offensive. Who is he meant to have copied?
Martin Smith
A: 
  DELETE FROM Table1
  WHERE CUSTOMER_NAME IN (SELECT customer_name
     from table1 t
     group by customer_name
     having sum(amount) > 70000
  )
Michael Pakhantsov
+1 To neutralise unexplained downvote.
Martin Smith
I downvoted this answer because it is a copycat.
Denis Valeev
+1  A: 
;with cte as
(
SELECT SUM(amount) OVER (PARTITION BY customer_name) a
FROM table1
)
DELETE FROM cte WHERE a > 70000
Martin Smith
@Martin Go climb a tree.
Denis Valeev
@Denis - Grow up.
Martin Smith
I downvoted your answer, because you downvoted my answer because you think that there's no better answer than a full, expanded kind of answer, but it's not true.
Denis Valeev
And my second downvote wasn't groundless.
Denis Valeev
@Denis - The OP asks for a `delete` you give him a `select` statement. Until that issue is fixed your answer should get sorted at the bottom - no?
Martin Smith
It is solely your understanding of reality. In my understanding of reality it's better to give someone a hint to solve a problem. So no, it should not be at the bottom of the list.
Denis Valeev
It is easy to spot the inconsistency. Look closer.
Denis Valeev