I have a query that returns several columns, Date, settle, acctNum, name, TCode, Amount.
The problem is that, there are records that are identical except for the Amount, which has the same absolute value on both records except that it is positive in one and negative in the other. I need to return only the row with the positive value for Amount. Would anyone have any suggestion? Please, see the below example. I need only the row with the positive amount 23450. I can't just filter out negatives because only negative that meet the conditions of the provided example are of interest. SQL Server 15.0.41
Example:
| Date | Settle | AcctNum | Name | TCode | Amount |
| ------------ | ------------ | ------------ | ------------ | ------------ | ------------ |
| Jan 1 | Y | 123456 | Mark | tabcd | 23450 |
| Jan 1 | Y | 123456 | Mark | tabcd | -23450 |
| Jan 7 | Y | 111111 | Vic | tabcd | 446623 |
| Mar 20 | Y | 999999 | Yas | tvfeh | 7778 |
| Jan 1 | Y | 123456 | Mark | tabcd | 987123 |
| Aug 9 | Y | 123456 | Mark | tabcd | 678599 |
| Dec 2 | Y | 564432 | Ali | tgghy | -987699 |
Expected result:
Date | Settle | AcctNum | Name | TCode | Amount |
---|---|---|---|---|---|
Jan 1 | Y | 123456 | Mark | tabcd | 23450 |
Jan 7 | Y | 111111 | Vic | tccwe | 446623 |
Mar 20 | Y | 999999 | Yas | tvfeh | 7778 |
Jan 1 | Y | 123456 | Mark | tabcd | 987123 |
Aug 9 | Y | 123456 | Mark | tabcd | 678599 |
Dec 2 | Y | 564432 | Ali | tgghy | -987699 |
3条答案
按热度按时间lbsnaicq1#
Something like :
zte4gxcn2#
You can use
NOT EXISTS
to skip the "duplicate" rows:Or use
EXCEPT
:cgvd09ve3#
You could make use of a window count, such as: