Sql 聚合函数,两列之差求和时group by子句错误
我有一个大问题,我正在研究:Sql 聚合函数,两列之差求和时group by子句错误,sql,sql-server,Sql,Sql Server,我有一个大问题,我正在研究: if object_id('tempdb..#mappings') is not null drop table #mappings; Create table #mappings ( GLEntity VARCHAR(MAX), NoteOwner VARCHAR(MAX) ) INSERT INTO #mappings VALUES('351', 'Ajax E Master Trust I, a Delaware Trust, Wilming
if object_id('tempdb..#mappings') is not null drop table #mappings;
Create table #mappings
(
GLEntity VARCHAR(MAX),
NoteOwner VARCHAR(MAX)
)
INSERT INTO #mappings
VALUES('351', 'Ajax E Master Trust I, a Delaware Trust, Wilmington Savings Fund Society, FSB, Trustee'),
('346', 'U.S. Bank National Association, as Indenture Trustee on behalf of and with respect to Ajax Mortgage Loan Trust 2016-1, Mortgage-Backed Notes, Series 2016-1'),
('352', 'U.S. Bank National Association, as Indenture Trustee on behalf of and with respect to Ajax Mortgage Loan Trust 2016-2, Mortgage-Backed Notes, Series 2016-2'),
('542', 'U.S. Bank National Association, as Indenture Trustee on behalf of and with respect to Ajax Mortgage Loan Trust 2014-A, Mortgage-Backed Notes, Series 2014-A'),
('543', 'U.S. Bank National Association, as Indenture Trustee on behalf of and with respect to Ajax Mortgage Loan Trust 2014-B, Mortgage-Backed Notes, Series 2014-B'),
('544', 'U.S. Bank National Association, as Indenture Trustee on behalf of and with respect to Ajax Mortgage Loan Trust 2015-A, Mortgage-Backed Notes, Series 2015-A'),
('545', 'U.S. Bank National Association, as Indenture Trustee on behalf of and with respect to Ajax Mortgage Loan Trust 2015-B, Mortgage-Backed Notes, Series 2015-B'),
('546', 'U.S. Bank National Association, as Indenture Trustee on behalf of and with respect to Ajax Mortgage Loan Trust 2015-C, Mortgage-Backed Notes, Series 2015-C'),
('547', 'U.S. Bank National Association, as Indenture Trustee on behalf of and with respect to Ajax Mortgage Loan Trust 2016-A, Mortgage-Backed Notes, Series 2016-A'),
('548', 'U.S. Bank National Association, as Indenture Trustee on behalf of and with respect to Ajax Mortgage Loan Trust 2016-B, Mortgage-Backed Notes, Series 2016-B'),
('549', 'U.S. Bank National Association, as Indenture Trustee on behalf of and with respect to Ajax Mortgage Loan Trust 2016-C, Mortgage-Backed Notes, Series 2016-C'),
('550', 'U.S. Bank National Association, as Indenture Trustee on behalf of and with respect to Ajax Mortgage Loan Trust 2017-A, Mortgage-Backed Notes, Series 2017-A'),
('551', 'U.S. Bank National Association, as Indenture Trustee on behalf of and with respect to Ajax Mortgage Loan Trust 2017-B, Mortgage-Backed Notes'),
('552', 'U.S. Bank National Association, as Indenture Trustee on behalf of and with respect to Ajax Mortgage Loan Trust 2017-C, Mortgage-Backed Notes'),
('553', 'U.S. Bank National Association, as Indenture Trustee on behalf of and with respect to Ajax Mortgage Loan Trust 2017-D, Mortgage-Backed Notes'),
('601', 'U.S. Bank National Association, as Indenture Trustee on behalf of and with respect to Ajax Mortgage Loan Trust 2018-A Mortgage-Backed Notes'),
('602', 'U.S. Bank National Association, as Indenture Trustee on behalf of and with respect to Ajax Mortgage Loan Trust 2018-B, Mortgage-Backed Notes'),
('557', 'Ajax Mortgage Loan Trust 2018-C, Mortgage-Backed Securities, Series 2018-C, by U.S. Bank National Association, as Indenture Trustee'),
('557', 'U.S. Bank National Association, as Indenture Trustee on behalf of and with respect to Ajax Mortgage Loan Trust 2018-C, Mortgage-Backed Securities, Series 2018-C'),
('603', 'U.S. Bank National Association, as Indenture Trustee on behalf of and with respect to Ajax Mortgage Loan Trust 2018-D, Mortgage-Backed Securities, Series 2018-D'),
('603', 'Ajax Mortgage Loan Trust 2018-D, Mortgage-Backed Securities, Series 2018-D, by U.S. Bank National Association, as Indenture Trustee'),
('604', 'Ajax Mortgage Loan Trust 2018-E, Mortgage-Backed Securities, Series 2018-E, by U.S. Bank National Association, as Indenture Trustee'),
('604', 'U.S. Bank National Association, as Indenture Trustee on behalf of and with respect to Ajax Mortgage Loan Trust 2018-E, Mortgage-Backed Securities, Series 2018-E'),
('605', 'Ajax Mortgage Loan Trust 2018-F, Mortgage-Backed Securities, Series 2018-F, by U.S. Bank National Association, as Indenture Trustee'),
('605', 'U.S. Bank National Association, as Indenture Trustee on behalf of and with respect to Ajax Mortgage Loan Trust 2018-F, Mortgage-Backed Securities, Series 2018-F'),
('606', 'U.S. Bank National Association, as Indenture Trustee on behalf of and with respect to Ajax Mortgage Loan Trust 2018-F, Mortgage-Backed Securities, Series 2018-F'),
('608', 'Ajax Mortgage Loan Trust 2019-B, Mortgage-Backed Securities, Series 2019-B, by U.S. Bank National Association, as Indenture Trustee'),
('607', 'Ajax Mortgage Loan Trust 2019-A, Mortgage-Backed Securities, Series 2019-A, by U.S. Bank National Association, as Indenture Trustee'),
('607', 'U.S. Bank National Association, as Indenture Trustee on behalf of and with respect to Ajax Mortgage Loan Trust 2019-A, Mortgage-Backed Securities, Series 2019-A'),
('609', 'Ajax Mortgage Loan Trust 2019-C, Mortgage-Backed Securities, Series 2019-C, by U.S. Bank National Association, as Indenture Trustee'),
('609', 'U.S. Bank National Association, as Indenture Trustee on behalf of and with respect to Ajax Mortgage Loan Trust 2019-C, Mortgage-Backed Securities, Series 2019-C'),
('561', 'Ajax Mortgage Loan Trust 2019-D, Mortgage-Backed Securities, Series 2019-D, by U.S. Bank National Association, as Indenture Trustee'),
('561', 'U.S. Bank National Association, as Indenture Trustee on behalf of and with respect to Ajax Mortgage Loan Trust 2019-D, Mortgage-Backed Securities, Series 2019-D')
DECLARE @reportingDate date = '5/31/2019',
@ArchiveDate date = '6/30/2019',
@securitization VARCHAR(MAX) = '2018-B';
WITH RptPop AS
(
SELECT
la.Account [LOAN_NBR]
,la.PmtPI [SCHED_P&I]
,la.NoteRate [NOTE_INT_RATE]
,la.NextDueDate [BORR_NEXT_PAY_DUE_DATE]
,la.PaidToDate [PIF_DATE]
,s.ActlBegPrinBal [ACTL_BEG_PRIN_BAL]
,s.SchedBegPrinBal [SCHED_BEG_PRIN_BAL]
,s.BeginNonInterestBearingDeferredPrincipal [BEGIN_NON_INTEREST_BEARING_DEFERRED_PRINCIPAL]
,s.Begin3rdPtyEscAdvanceBal [BEGIN_3RD_PTY_ESC_ADVANCE_BAL]
,s.BeginServicingEscAdvanceBal [BEGIN_SERVICING_ESC_ADVANCE_BAL]
,s.SchedGrossInterestAmt [SCHED_GROSS_INTEREST_AMT]
,s.NetIntRate [NET_INT_RATE]
,s.SchedNetInt [SCHED_NET_INT]
,s.ActlNetInt [ACTL_NET_INT]
,s.ActlEndPrinBal [ACTL_END_PRIN_BAL]
,s.SchedPrinamt [SCHED_PRIN_AMT]
,s.SchedEndPrinBal [SCHED_END_PRIN_BAL]
,s.LoanLossAmt [LOAN_LOSS_AMT]
,s.ActualEndingBalanceTotalDebtOwed [ACTUAL_ENDING_BALANCE_TOTAL_DEBT_OWED]
,s.EndingNonInterestBearingDeferredPrincipalBal [ENDING_NON_INTEREST_BEARING_DEFERRED_BAL]
,s.End3rdPtyEscAdvanceBal [END_3RD_PTY_ESC_ADVANCE_BAL]
,s.EndEscrowAdvanceBal [END_ESCROW_ADVANCE_BAL]
,la.NoteOwner
FROM esp.MARS_DW.dbo.vw_Loans_Archive la
LEFT JOIN esp.Securitization.dbo.SecuritizationReporting s on s.LoanNbr = la.Account
JOIN #mappings mp on mp.NoteOwner = la.NoteOwner
WHERE
la.ArchiveDate = @ArchiveDate
AND s.ReportingDate = @reportingDate
AND la.NoteOwner LIKE '%2018-B%'
)
,
gl_entries_for_month AS
(
SELECT
a.name [Account]
,ac.AccountNo
,le.LocationID
,act.Code
,dd.Date
,CASE WHEN EntryType = -1 THEN fle.AMOUNT ELSE 0 END AS CreditAmount
,CASE WHEN EntryType = 1 THEN fle.AMOUNT ELSE 0 END AS DebitAmount
FROM DWH.GL.FactLedgerEntry fle WITH(NOLOCK)
JOIN DWH.GL.DimJournals j WITH (NOLOCK)
ON j.JournalID = fle.JournalID
JOIN DWH.GL.DimAssets a WITH (NOLOCK)
ON a.AssetID = fle.AssetID
JOIN DWH.GL.DimAccounts ac WITH(NOLOCK)
ON ac.AccountID = fle.AccountID
JOIN DWH.GL.DimLocationEntity le WITH(NOLOCK)
ON le.LocationEntityID = fle.LocationID
JOIN DWH.GL.DimActivity act WITH(NOLOCK)
ON act.ActivityID = fle.ActivityID
JOIN DWH.dbo.DimDate dd WITH (NOLOCK)
ON dd.DateID = fle.BatchDateID
JOIN MARS.dbo.vw_Loans vl WITH(NOLOCK)
ON vl.Account = a.Name
WHERE fle.EntryState = 'Posted'
AND dd.Date BETWEEN DATEADD(month, DATEDIFF(month, 0, @ArchiveDate), 0) AND @ArchiveDate
)
SELECT
DISTINCT RptPop.[LOAN_NBR]
,CASE WHEN gl_entries_for_month.Code IN ('PMTI', 'SPYO', 'PMTO')
AND gl_entries_for_month.LocationID = mp.GLEntity
AND gl_entries_for_month.AccountNo IN ('18125','18130')
THEN SUM(DebitAmount - CreditAmount) ELSE 0 END AS [TEST]
FROM
RptPop
JOIN gl_entries_for_month on gl_entries_for_month.Account = RptPop.[LOAN_NBR]
LEFT JOIN #mappings mp on mp.NoteOwner = RptPop.NoteOwner and mp.GLEntity = gl_entries_for_month.LocationID
WHERE RptPop.LOAN_NBR = '######-513'
GROUP BY RptPop.LOAN_NBR
我在代码中的这一部分遇到错误:
CASE WHEN gl_entries_for_month.Code IN ('PMTI', 'SPYO', 'PMTO')
AND gl_entries_for_month.LocationID = mp.GLEntity
AND gl_entries_for_month.AccountNo IN ('18125','18130')
THEN SUM(DebitAmount - CreditAmount) ELSE 0 END AS [TEST]
我认为,通过RptPop.LOAN_NBR添加GROUP,我不会出现以下错误:
Msg 8120, Level 16, State 1, Line 121
Column 'gl_entries_for_month.Code' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 8120, Level 16, State 1, Line 121
Column 'gl_entries_for_month.Code' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 8120, Level 16, State 1, Line 121
Column 'gl_entries_for_month.Code' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 8120, Level 16, State 1, Line 122
Column 'gl_entries_for_month.LocationID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 8120, Level 16, State 1, Line 122
Column '#mappings.GLEntity' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 8120, Level 16, State 1, Line 123
Column 'gl_entries_for_month.AccountNo' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 8120, Level 16, State 1, Line 123
Column 'gl_entries_for_month.AccountNo' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
我是SQL新手,如果有人知道如何帮助我,我将不胜感激。我试图自己解决这个问题,但我没有在网上吃午餐研究解决方案。只需将
SUM
移到案例
声明之外:
SUM(CASE
WHEN gl_entries_for_month.Code IN ('PMTI', 'SPYO', 'PMTO')
AND gl_entries_for_month.LocationID = mp.GLEntity
AND gl_entries_for_month.AccountNo IN ('18125','18130')
THEN DebitAmount - CreditAmount
ELSE 0
END) AS [TEST]
只需将
SUM
移到CASE
语句之外:
SUM(CASE
WHEN gl_entries_for_month.Code IN ('PMTI', 'SPYO', 'PMTO')
AND gl_entries_for_month.LocationID = mp.GLEntity
AND gl_entries_for_month.AccountNo IN ('18125','18130')
THEN DebitAmount - CreditAmount
ELSE 0
END) AS [TEST]