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:
01379647
Views:
13
>>>>
>>>>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
>>
>>
>>Now I'm getting "Column 'Register.RecordId' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.". I need to be able to pull the balance as well as the rest of the register.
>
>What is the query you're trying now? You need to use this one as derived table, e.g.
>
>select Register.*, Balance from Register INNER JOIN 
>(SELECT Sequence, SUM(ISNULL(Deposit,0) + ISNULL(Payment,0)) AS Balance 
>FROM Register GROUP BY Sequence) B on Register.Sequence = B.Sequence
I'm not sure if I'm explaing what I want. This query does not return the results I want:
Payment               Deposit               Reconciled Balance
--------------------- --------------------- ---------- ------------
NULL                  750.00              NULL       750.00
18.52                 NULL                  NULL       18.52
74.28                 NULL                  NULL       74.28
42.16                 NULL                  NULL       42.16
100.00                NULL                  NULL       100.00
NULL                  150.00               NULL       150.00
112.25                NULL                  NULL       112.25
The last column should be have the balance, not the value of the despoit or payment column.
Everything makes sense in someone's mind
public class SystemCrasher :ICrashable
In addition, an integer field is not for irrational people
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform