Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Select same field twice

Author  Topic 

Vack
Aged Yak Warrior

530 Posts

Posted - 2011-08-05 : 13:22:28
SQL 2008 R2

Table: OEHDRHST
Example of Records

Type Cus_no Sls_amt
O 1234 200.00
C 1234 50.00



Can I write a select statement that would give me the following:


Cus_no Sls_amt
1234 150.00



In the Table where ever the type is C the amount needs to be negative.

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-08-05 : 13:27:29
SELECT Cus_no, SUM(CASE WHEN Type='C' THEN -Sls_Amt ELSE Sls_Amt END) Sls_amt
FROM OEHDRHST
GROUP BY Cus_no
Go to Top of Page
   

- Advertisement -