SQL使用group by语句更新多个值不起作用

时间:2015-12-08 11:48:42

标签: sql sql-server tsql

我已经尝试过检查类似的帖子但只是无法正确使用,请帮忙。

此查询返回正确的数据,但更新语句会抱怨,因为它不仅仅是

SELECT 
    classificationchartofaccountname, 
    -SUM(ISNULL(HLD.Nominal, 0))  AS 'Total' 
FROM
    [dbsFPM].[dbo].[rpt_Holdings] HLD 
INNER JOIN 
    [dbsFPM].[dbo].[rpt_FundAccounts] FA ON HLD.Username = FA.Username 
                                         AND HLD.ClientPortfolioCode = FA.ClientPortfolioCode 
                                         AND FA.ClassificationChartOfAccountName = HLD.InstrumentName 
WHERE 
    HLD.username =  @username 
    AND FA.Username = @username 
    AND FA.Narration = 'IncomeStatement' 
    AND FA.AccountType = 'Expense'  
GROUP BY 
    ClassificationChartOfAccountName)

返回值:

classificationchartofaccountname    Total

General Expenses on ZAR 17578.38

lc Audit 2014 on ZAR    6533.12

lc Trustee Fees - Other on ZAR  840.41

lc Trustee Fees-ZAR 38524.34

Management Fees-ZAR 6452680.040

Performance Fees-ZAR    175.80

更新语句我想按如下方式使用错误:

  

Msg 512,Level 16,State 1,Line 1
  子查询返回的值超过1。这是不允许的   子查询跟随=,!=,<,< =,>,> =或当子查询用作   表达。

以下是更新:

UPDATE [rpt_FundAccounts]
SET ReconNarration = 'ExpenseAnalysis_Period', 
    Recon = (SELECT -SUM(ISNULL(HLD.Nominal, 0)) 
             FROM [dbo].[rpt_Holdings] HLD 
             INNER JOIN [dbo].[rpt_FundAccounts] FA ON HLD.Username = FA.Username 
                                                    AND HLD.ClientPortfolioCode = FA.ClientPortfolioCode 
                                                    AND FA.ClassificationChartOfAccountName = HLD.InstrumentName 
             WHERE HLD.username =  @Username 
               AND FA.Username = @Username 
               AND FA.Narration = 'IncomeStatement' 
               AND FA.AccountType = 'Expense' 
               /* and ClassificationChartOfAccountName = 'Management Fees-ZAR'*/ 
             GROUP BY ClassificationChartOfAccountName)
WHERE 
    username = @username  
    AND Narration = 'IncomeStatement' 
    AND AccountType = 'Expense' 
    /*and ClassificationChartOfAccountName = 'Management Fees-ZAR' */

问题是,我想更新所有ClassificationChartOfAccountNames及其各自的总计[rpt_Holdings]

请帮助,对不起,我还是初学者!

ClassificationChartOfAccountName

Bank Charge Fees on GBP
Bank_Interest Charges on ZAR
Distribution Paid
Expenses
Foreign Withholding Tax
General Expenses on EUR
General Expenses on USD
General Expenses on ZAR
lc Audit 2012 on ZAR
lc Audit 2013 Final on ZAR
lc Audit 2013 on ZAR
lc Audit 2014 on ZAR
lc Scrip - SBSA on ZAR
lc Trustee Fees - Other on ZAR
lc Trustee Fees-ZAR
Management Fees-ZAR
Performance Fees-ZAR
Realised Foreign Exchange Gain
Realised GainLoss
Realised Pull To Par

1 个答案:

答案 0 :(得分:0)

此处的问题出现在子查询中的GROUP BY语句中。它使得输出不是一个SUM值,而是每个ClassificationChartOfAccountName的带有SUM的表。但是您无法使用TABLE更新标量值,并且您收到错误 - Subquery returned more than 1 value

如果要根据行ClassificationChartOfAccountName更新包含SUM的表,则应在更新的表行和子查询之间建立链接。因此子查询只输出一个值。 我认为它应该是这样的:

UPDATE [rpt_FundAccounts] FA 
SET ReconNarration = 'ExpenseAnalysis_Period', 
    Recon = (select -sum(isnull(HLD.Nominal,0)) 
             from [dbo].[rpt_Holdings] HLD 
             WHERE  HLD.Username =  FA.Username 
                    and HLD.ClientPortfolioCode = FA.ClientPortfolioCode 
                    and FA.ClassificationChartOfAccountName = HLD.InstrumentName
             )
WHERE username = @username  
      and Narration = 'IncomeStatement' 
      and AccountType = 'Expense'