Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi every one i try to get those records which have balance amount is 0 and Balance amount is greater then 0 i.e balance>0 as 'Stats'
if balance amount is 0 then its show its stats is 'Paid' if balance amount greater then 0 then it show stats as 'Painding'
please any one tell me how can i do this
I try this but fail to get result as i want

What I have tried:

SELECT [InvoiceNo]
      ,[Date]      
      ,[CustmID]    
      ,[TotalDue]	   
     ,case Balance when Balance>0 then 'Pending'  when  Balance==0 then  'Paid'	  	   
  FROM [dbo].[Sale_Bill_details]
Posted
Updated 2-Nov-18 3:16am
Comments
CHill60 2-Nov-18 5:12am    
Show some sample data - from your comment to Solution 1 it sounds as if you do not have a column called Balance

Could you have possible meant to use the TotalDue instead of Balance? This would change your statement to look like this:
SQL
SELECT [InvoiceNo], [Date], [CustmID], [TotalDue]
,  [Status] = CASE
		WHEN [TotalDue] >0 THEN 'Pending'
		WHEN [TotalDue] =0 THEN 'Paid'	  	   
END

FROM [dbo].[Sale_Bill_details]

However; I have a problem with this, as it does not compensate for someone who overpaid and has a negative balance. You should be using an ELSE statement OR alternatively change the second line:
SQL
-- using ELSE
SELECT [InvoiceNo], [Date], [CustmID], [TotalDue]
,  [Status] = CASE
		WHEN [TotalDue] > 0 THEN 'Pending'
		WHEN [TotalDue] < 0 THEN 'Paid'
        ELSE                     'Credit Due'
END

FROM [dbo].[Sale_Bill_details]

-- alternative 
SELECT [InvoiceNo], [Date], [CustmID], [TotalDue]
,  [Status] = CASE
		WHEN [TotalDue] > 0 THEN 'Pending'
		WHEN [TotalDue] <=0 THEN 'Paid'	  	   
END

FROM [dbo].[Sale_Bill_details]


But I still have problems with this- as [Status] ("Pending", "Paid", "Credit Due") is a presentation item; and generally should neither be saved nor calculated within the database.
Separation of Concerns to me says that the DB is used to display the data. The application and it's friend end return the data, and choose what to display from it.

Addendum After reviewing the code due to an error pointed out to me, I found two errors in your CASE statement1. If you define the condition in the opening statement, it cannot be used in the WHEN statement
2. TSQL uses a single equals sign for equality and assignment, your second WHEN uses ==
3. There is no END to the CASE statement.
SQL
,	case Balance when Balance >0 then 'Pending'  when Balance == 0 then  'Paid'
-- above line will error out. SQL Studio and Sql Ops Studio will flag the errors
,	case         when Balance >0 then 'Pending'  when Balance =  0 then  'Paid' END


Proof of Concept along, incorporating my recommended CASE statement along with the original one (will need to be commented out) and the simple fix to the bad statement
SQL
DECLARE @Sample TABLE (InvoiceNo INT NULL, [Date] DATE NULL, Custom ID INT NULL, TotalDue MONEY NULL, Balance MONEY NULL)

INSERT @Sample
VALUES (1, GetDate(), 1, 123, 123)
,      (2, GetDate(), 2, 123, 0)
,      (3, GetDate(), 3, 123, -123)
,      (4, GetDate(), 4, 0, null )

SELECT [InvoiceNo]
      ,[Date]      
      ,[CustomID]    
      ,[TotalDue]	   
     , [Status] = case
		when Balance > 0 then 'Pending'
		when Balance = 0 then 'Paid'
		when Balance < 0 then 'Credit Due'
		else                  'na' 
	END

,	case Balance when Balance >0 then 'Pending'  when Balance == 0 then  'Paid'
,	case         when Balance >0 then 'Pending'  when Balance =  0 then  'Paid' END
FROM @Sample

Running this (with the bad line commented out) will return something like this:
InvoiceNo	Date	    CustomID	TotalDue	Status       (No column name)
1	        2018-11-02	1	          123.00    Pending      Pending
2	        2018-11-02	2	          123.00    Paid         Paid
3	        2018-11-02	3	          123.00    Credit Due   NULL
4	        2018-11-02	4	            0.00    na           NULL
 
Share this answer
 
v4
Comments
Richard Deeming 2-Nov-18 10:30am    
There shouldn't be a "CASE" after the "END". :)
CASE (Transact-SQL) | Microsoft Docs[^]
MadMyche 2-Nov-18 10:43am    
Fixed, I really should learn not to copy/paste without reviewing all the code. And now I see that the original post doesn't have an END on the CASE either.
SELECT [InvoiceNo]
      ,[Date]      
      ,[CustmID]    
      ,[TotalDue]	   
     ,  case 
		when Balance>0 then 'Pending'  
		when  Balance=0 then  'Paid'	  	   
	end case
  FROM [dbo].[Sale_Bill_details]
 
Share this answer
 
Comments
Fahid Zahoor 2-Nov-18 5:04am    
Still give error under all the columns, the error is invalid column name
when i use case then this error come when i remove case then error also removed automatically me b some problems still occur in case
Any Suggestion

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900