0

我有一个程序可以获取某些信息。我在其中添加了一个获取帐户数量的字段。为此,我使用了“计数”聚合函数。现在既然已经添加了 Count,它也应该有一个按功能分组的功能。我正在尝试将 Group By 添加到其中,但找不到将 Group By 与 Dynamic SQL 一起使用的确切语法。以下是与该过程一起使用的查询:

DECLARE @SQL  nvarchar(max),@paramlist nvarchar(max) 
SELECT @SQL = 'SELECT awt.AcctID, MIN(CAST(awt.autoEnter AS SMALLINT)) AS AutoApproved                
FROM dbo.AccountWorkflowTask awt JOIN dbo.WorkflowTask wt ON awt.WorkflowTaskID = wt.WorkflowTaskID                
WHERE (wt.TaskID IN (9, 17) AND ReasonIDExit = 1)'

SELECT @SQL=@SQL+')) '                                    
SELECT @SQL=@SQL+'SELECT a.ApplID, acct.AcctID, acct.dtApplication, ai.FName, ai.MName, ai.LName, ai.SSN, a.Email, ao.CompanyName,'                
SELECT @SQL=@SQL+'ao.DBAName, ao.TaxID, acct.AcctNum, acct.AcctAffinityNum, luA.AffinityNum, luA.AffinityName, t.TaskDesc, awt.dtEnter, 
Count(DMA.AcctNum) As NoOfAccounts   FROM   dbo.Applicant a JOIN dbo.APPLICANTACCOUNT aa ON a.ApplID = aa.ApplID'

JOIN 'TO SOME TABLE'

WHERE 'Some Condition'

现在我想使用 to Group By 子句来获取 NoOfAccounts 的计数。我无法获得它的语法。任何帮助将不胜感激。

4

2 回答 2

1

根据您的评论,如果您使用OVER 子句,则不需要 GROUP BY

改用这个

Count(DMA.AcctNum) OVER () AS NoOfAccounts
于 2012-10-31T10:11:03.163 回答
0

我得到了我的问题的答案。我必须在动态 Sql 中使用 Group By,如下所示:

SELECT @SQL=@SQL+ ' GROUP BY a.ApplID, acct.AcctID, acct.dtApplication, ai.FName, ai.MName, ai.LName, ai.SSN, a.Email, ao.CompanyName,                
ao.DBAName, ao.TaxID, acct.AcctNum, acct.AcctAffinityNum, luA.AffinityNum, luA.AffinityName, t.TaskDesc,                
awt.dtEnter, q.QueueDesc, w.WorkflowID, w.WorkflowDesc,                
luO.OriginationID, luO.OriginationDesc, aco.dtCheckOut, aco.UserCheckOut, aco.GUIDCheckout, lts.TaskStageDesc,                
acct.dtApplication,cteAge.Age, asa.SOAApplID, w.WorkflowID, luO.OriginationID, awt.userEnter, cteAutoApprove.AutoApproved,          
acctLam.Market, ful.Request, fun.SourceAccountTypeDescription, fun.FundingStatus, acct.BrokerCode,            
  ai.SSN, ao.TAXID, bup.BusPurposeDesc, ao.BusPurposeOther,                             
  note.Note, note.NoteReasonDesc, note.ReasonCode, aa.RelationshipCode, luRel.RelationshipCodeDesc, Addr.Address1, Addr.Address2, 
  Addr.City, Addr.State, Addr.Zip' 
于 2012-10-31T12:42:46.750 回答