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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Complex nested query question

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-01-01 : 23:17:06
Charles writes "I can get the following query to work in two seperate procs, but not combined. I need the result set to include the total of the BBF part of the query (which works by itself). I have tried several methods, so far the one below has the best results. Any tips or help would be appreciated. Is this even the best way to accomplish the job? The error I get is "No column was specified for column 23 of 'AllTransactions'."
--CODE--
ALTER PROCEDURE dbo.Statements
-- This stored procedure will develop end of month statement data.
-- --------------------------------------------------------------------------------------------------------------------------------
-- Steps include:
-- Retrieve the active accounts NOTE: All active accounts must be retrieved since
-- we need those with no activity as they may
-- have a balance brought forward.
-- Retrieve the current transactions
-- Retrieve the Balance Brought Forward (BBF)

/*
(
@BeginDate SMALLDATETIME,
@EndDate SMALLDATETIME
)
*/
AS

SELECT -- This select is to hold the results of all of the other selects
CustNmbr,
StmtName,
CustName,
Address1,
Address2,
Address3,
Country,
City,
State,
Zip,
Balance,
Bucket1,
Bucket2,
Bucket3,
Bucket4,
PostDate,
DocNumbr,
ChekNmbr,
OrderTransAmount,
BBF

FROM
(
SELECT -- This select gets the customer data, the aging data, and the transactions
RTRIM(Accounts.CUSTNMBR) AS CUSTNMBR,
RTRIM(Accounts.STMTNAME) AS STMTNAME,
RTRIM(Accounts.CUSTNAME) AS CUSTNAME,
RTRIM(Accounts.ADDRESS1) AS ADDRESS1,
RTRIM(Accounts.ADDRESS2) AS ADDRESS2,
RTRIM(Accounts.ADDRESS3) AS ADDRESS3,
RTRIM(Accounts.COUNTRY) AS COUNTRY,
RTRIM(Accounts.CITY) AS CITY,
RTRIM(Accounts.STATE) AS STATE,
RTRIM(Accounts.ZIP) AS ZIP,
RTRIM(Accounts.INACTIVE) AS INACTIVE,
RTRIM(Aging.CUSTBLNC) AS Balance,
RTRIM(Aging.AGPERAMT_1) AS Bucket1,
RTRIM(Aging.AGPERAMT_2) AS Bucket2,
RTRIM(Aging.AGPERAMT_3) AS Bucket3,
RTRIM(Aging.AGPERAMT_4) AS Bucket4,
RTRIM(CAST(CurrentTX.POSTDATE as SMALLDATETIME)) AS POSTDATE,
RTRIM(CurrentTX.DOCNUMBR) AS DOCNUMBR,
RTRIM(CurrentTX.CHEKNMBR) AS CHEKNMBR,
RTRIM(CurrentTX.RMDTYPAL) AS RMDTYPAL,
RTRIM(CurrentTX.ORTRXAMT) AS ORTRXAMT,
CONVERT(MONEY,CurrentTX.ORTRXAMT * _UPIRMstatementsDocType.Multiplier) AS [OrderTransAmount],
(SELECT --CUSTNMBR ,
SUM(BBF) AS BBF
-- Calculates the BBF on the Open Transactions (OTX)
FROM (
SELECT
OTX.CUSTNMBR,
CONVERT(MONEY,ISNULL(SUM(OTX.ORTRXAMT * _UPIRMstatementsDocType.Multiplier), 0) ) AS BBF
FROM UPI.DBO.RM30101 AS OTX
INNER JOIN dbo._UPIRMstatementsDocType ON OTX.RMDTYPAL = dbo._UPIRMstatementsDocType.RMDTYPAL
WHERE OTX.VOIDSTTS = 0
--AND OTX.DOCDATE < @EndDate
GROUP BY OTX.CUSTNMBR
UNION
-- Calculates the BBF on the Current Transactions (CurTX)
SELECT CurTX.CUSTNMBR,
CONVERT(MONEY,ISNULL(SUM(CurTX.ORTRXAMT * _UPIRMstatementsDocType.Multiplier), 0) ) AS BBF
FROM UPI.DBO.RM20101 AS CurTX
INNER JOIN dbo._UPIRMstatementsDocType ON CurTX.RMDTYPAL = dbo._UPIRMstatementsDocType.RMDTYPAL
WHERE CurTX.VOIDSTTS = 0
--AND CTX.DOCDATE < @EndDate
--AS BBF
GROUP BY CurTX.CUSTNMBR )X
WHERE X.CUSTNMBR = Accounts.CUSTNMBR
GROUP BY X.CUSTNMBR)
FROM
UPI.DBO.rm00101 Accounts

aclarke
Posting Yak Master

133 Posts

Posted - 2002-01-02 : 01:07:33
Hey. It looks like your problem might be that you have no alias for on of your subqueries. Instead of this near the end:

GROUP BY X.CUSTNMBR)


try something like this

GROUP BY X.CUSTNMBR) as foo


You've done it elsewhere, but probably just missed it here. Your error tells you the problem is in column 23, so it's in there somewhere!!!

Go to Top of Page

ToddV
Posting Yak Master

218 Posts

Posted - 2002-01-02 : 09:36:53
AClarke is right on about your error. I would also suggest(particularily if you are running this for all accounts) trying to change this from the correlated sub-select to get the BBF to using a derived table approach.

Select Col1,
BBF
FROM Accounts A
JOIN (SELECT SUM(BBF), AccountNBR FROM ... GROUP BY AccountNbr) AS B
ON A.AccountNbr = B.AcountNbr



Go to Top of Page
   

- Advertisement -