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
 Creating a new column based on stated criteria

Author  Topic 

scottydogg84
Starting Member

5 Posts

Posted - 2011-11-08 : 08:14:25
Hello all,

I'm having trouble trying to get the required results with the following SQL code:

SELECT     bh_bildat, bh_bilnum, bh_biltot, bh_type, bh_payamt, bh_biltot - bh_payamt AS 'Amount O/S'
FROM cabilhis
WHERE (bh_matter = '00067603')

This is basically from an Invoice table which shows the invoice type (bh_type - This will either be 'b' for bill or 'a' for credit note), invoice amount (bh_biltot) and amount paid (bh_payamt).

I have been trying to achieve a new column that if bh_type = 'b' and the bill has been paid in full to return 'Yes' and if not paid in full to return 'No'. If bh_type does not equal 'b', I want to return nothing, so ' '.

So to clarify:
IF bh_type = 'b' and 'Amount O/S' = '0' THEN return 'Yes'
IF bh_type = 'b' and 'Amount O/S' > '0' THEN return 'No'
IF bh_type <> 'b' THEN return ' '

I would be grateful for any help on this.

Thanks in advance

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-08 : 08:29:17
[code]
SELECT bh_bildat, bh_bilnum, bh_biltot, bh_type, bh_payamt, bh_biltot - bh_payamt AS 'Amount O/S',
CASE WHEN bh_type = 'b' AND bh_biltot - bh_payamt = 0 THEN 'Yes' WHEN bh_type = 'b' AND bh_biltot - bh_payamt <> 0 THEN 'No' ELSE '' END AS Yourcol
FROM cabilhis
WHERE (bh_matter = '00067603')
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

scottydogg84
Starting Member

5 Posts

Posted - 2011-11-08 : 10:17:34
Thanks. Worked great
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-08 : 10:22:25
wc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -