thenna thenna - 2 months ago 10
SQL Question

Check Duplicate Records value negative and Positive In SQL server

I using SQL Server i have record Expense sheet.In this table have more than 4000o Lines Of Query . I want Short duplicate Record SQL .if Which row have same value negative and positive Record

Example:

this is my Table

Expense Id Amount:
1234 567
4141 677
3434 667
1234 - 567


but this Table I have Two Expense ID same . but value Differ Negative and Positive i want
STORE
this value LIke

Expense Id Amount:
1234 567

1234 - 567


In table I
Expense Id
and
Amount
I have Same Expense Id and but value is Positive and negative .but is this reverse value. i want sort Like this All table get all rows in SQL.how can i get all rows ?

Answer

Does this do what you want?

select e.*
from expense e
where exists (select 1
              from expense e2
              where e2.expenseid = e.expenseid and e2.amount = - e.amount
             );