Advertisement

06.30.2008 at 08:57AM PDT, ID: 23527318 | Points: 500
[x]
Attachment Details

Query with aggregate function

Asked by deedub84 in SQL Server 2005

Tags: MS SQL Server

Our accounting system uses SQL Server as the backbone.  I'm attempting to create a view that will show both the accounts receivable transaction amounts and a running total.  My current code is below.  I'm getting an error: "Column dbo.ARTran.CustId is invalid in select clause because it is not  contained in either an aggregate function or a group by clause."  I understand what the error is saying, but not how to fix it.

In essence, I want:

CustID, Period, Amount, Running Total.

Thanks,
Deedub84Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
SELECT     TOP (100) PERCENT T.CustId, T.ProjectID, T.TaskID, C.Name, T.PerPost, T.TranDate, T.TranDesc, T.BatNbr, C.Status, CASE WHEN (T .drcr = 'C') 
                      THEN - (T .tranamt) ELSE T .tranamt END AS Amount, CASE WHEN (T .TranType = 'IN') THEN 'Invoice' ELSE 'Payment' END AS TransactionType, 
                      T.Acct AS AccountNum, dbo.Account.Descr AS AcctDesc,
                          (SELECT     SUM( CASE WHEN (T .drcr = 'C') THEN - (T .tranamt) ELSE T .tranamt END) AS Expr1
                            FROM          dbo.ARTran AS S
                            WHERE      (CustId = C.CustId) AND (PerPost <= T.PerPost) AND (Acct = T.Acct)) AS PTDTotal
FROM         dbo.Customer AS C INNER JOIN
                      dbo.ARTran AS T ON C.CustId = T.CustId INNER JOIN
                      dbo.Account ON T.Acct = dbo.Account.Acct
WHERE     (C.Status = 'A') AND (T.Acct = '11551')
ORDER BY T.CustId, T.PerPost
[+][-]06.30.2008 at 09:18AM PDT, ID: 21900515

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.30.2008 at 09:59AM PDT, ID: 21900912

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628