Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Compute Balance In Query
Message
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
01379592
Message ID:
01379601
Views:
25
>>>I have a checkbook register table with Deposit and Payment columns. I want to compute the balance in a query, but I'm unsure of the syntax. Can someone point me in the right direction?
>>>
>>>Thanks
>>
>>What seems to be the problem? Is it SQL Server table?
>>
>>select AccountNumber, sum(Deposit + Payment) as allPayments from Register group by AccountNumber
>>
>>Or do you want to use the balance? What is the create table script?
>
>I want to compute the checkbook balance, row by row, in a query. The balance should come back as a computed column.
>
>Here's the table"
>
>
>CREATE TABLE Register
>	(RecordId		INT IDENTITY PRIMARY KEY NOT NULL,
>	 Sequence		INT				NOT NULL,
>	 Date			DATETIME		NOT NULL,
>	 TransType		VARCHAR(5)		NOT NULL,
>	 TransDesc		VARCHAR(MAX)	NOT NULL,
>	 Memo			VARCHAR(MAX)	NULL,
>	 Payment		MONEY			NULL,
>	 Deposit		MONEY			NULL,
>	 Reconciled		BIT				NULL)
>
>
>This returns all NULLs:
>
>
>SELECT SUM(Deposit + Payment) AS Balance FROM Register GROUP BY Sequence ORDER BY Sequence 
>
>
>

Change it to

SELECT SUM(ISNULL(Deposit,0) + ISNULL(Payment,0)) AS Balance FROM Register GROUP BY Sequence ORDER BY Sequence

Also it's not a good idea to use MONEY type.

See http://forums.asp.net/t/1380719.aspx
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform