我的SQL怎么了?

时间:2015-08-26 09:55:29

标签: sql sql-server ssms

我必须编辑存储过程并添加按部门排序结果的功能。编辑后,我收到一个错误:

  

Msg 8120,Level 16,State 1,Procedure   acc_qry_profit_loss_this_year_by_dept,第543行专栏   “#tmp_rsl.department”在选择列表中无效,因为它不是   包含在聚合函数或GROUP BY子句中。

我相信这个错误不在第543行,也可能只是我的眼睛。 以下是从第485行到第557行的代码:

-- Blank line
  INSERT INTO #tmp_rsl(acc_id,parent_code,acc_code,description,acclevel,
    yr_balance,ly_balance,mn_balance,lm_balance,rsl_type,show_line)
  SELECT 0,'','' as acc_code,'' as acc_name,1,0,0,0,0,'BLANK_LINE' as rsl_type,0 as show_line

    --OI    Other Income
  SET @rsl_acc_type='OI'
  INSERT INTO #tmp_rsl(acc_id,department,department_name,parent_code,acc_code,description,acclevel,
    yr_balance,ly_balance,mn_balance,lm_balance,rsl_type,show_line)
  SELECT acc_id,department,department_name,parent_code,acc_code,acc_name,acclevel,
    ISNULL(yr_credit,0)-ISNULL(yr_debit,0),
    ISNULL(ly_credit,0)-ISNULL(ly_debit,0),
    ISNULL(mn_credit,0)-ISNULL(mn_debit,0),ISNULL(lm_credit,0)-ISNULL(lm_debit,0),
    (CASE WHEN attribute=99 THEN 'CAT_HEAD' WHEN attribute=0 THEN 'FOLDER' ELSE 'DETAIL' END) as rsl_type,0
  FROM #tmp_acc_list WHERE acctype=@rsl_acc_type AND acclevel=1 

  INSERT INTO #tmp_rsl(acc_id,department,department_name,parent_code,acc_code,description,acclevel,
    yr_balance,ly_balance,mn_balance,lm_balance,rsl_type,show_line)
  SELECT acc_id,department,department_name,parent_code,acc_code,acc_name,acclevel,
    ISNULL(yr_credit,0)-ISNULL(yr_debit,0),
    ISNULL(ly_credit,0)-ISNULL(ly_debit,0),
    ISNULL(mn_credit,0)-ISNULL(mn_debit,0),ISNULL(lm_credit,0)-ISNULL(lm_debit,0),
    (CASE WHEN attribute=99 THEN 'CAT_HEAD' WHEN attribute=0 THEN 'FOLDER' ELSE 'DETAIL' END) as rsl_type,0
  FROM #tmp_acc_list WHERE acctype=@rsl_acc_type AND acclevel<>1 order by department,acc_code

  INSERT INTO #tmp_rsl(acc_id,department,department_name,parent_code,acc_code,description,acclevel,
    yr_balance,ly_balance,mn_balance,lm_balance,rsl_type,show_line)
  SELECT 0,department,department_name,'','' as acc_code,'' as acc_name,acclevel,
    ISNULL(yr_credit,0)-ISNULL(yr_debit,0),
    ISNULL(ly_credit,0)-ISNULL(ly_debit,0),
    ISNULL(mn_credit,0)-ISNULL(mn_debit,0),ISNULL(lm_credit,0)-ISNULL(lm_debit,0),
    'CAT_SUMM' as rsl_type,1
  FROM #tmp_acc_list WHERE acctype=@rsl_acc_type AND acclevel=1

  --EX  Expenses
  SET @rsl_acc_type='EX'
  INSERT INTO #tmp_rsl(acc_id,department,department_name,parent_code,acc_code,description,acclevel,
    yr_balance,ly_balance,mn_balance,lm_balance,rsl_type,show_line)
  SELECT acc_id,department,department_name,parent_code,acc_code,acc_name,acclevel,
    ISNULL(yr_debit,0)-ISNULL(yr_credit,0),
    ISNULL(ly_debit,0)-ISNULL(ly_credit,0),
    ISNULL(mn_debit,0)-ISNULL(mn_credit,0),ISNULL(lm_debit,0)-ISNULL(lm_credit,0),
    (CASE WHEN attribute=99 THEN 'CAT_HEAD' WHEN attribute=0 THEN 'FOLDER' ELSE 'DETAIL' END) as rsl_type,0
  FROM #tmp_acc_list WHERE acctype=@rsl_acc_type AND acclevel=1 

  INSERT INTO #tmp_rsl(acc_id,department,department_name,parent_code,acc_code,description,acclevel,
    yr_balance,ly_balance,mn_balance,lm_balance,rsl_type,show_line)
  SELECT acc_id,department,department_name,parent_code,acc_code,acc_name,acclevel,
    ISNULL(yr_debit,0)-ISNULL(yr_credit,0),
    ISNULL(ly_debit,0)-ISNULL(ly_credit,0),
    ISNULL(mn_debit,0)-ISNULL(mn_credit,0),ISNULL(lm_debit,0)-ISNULL(lm_credit,0),
    (CASE WHEN attribute=99 THEN 'CAT_HEAD' WHEN attribute=0 THEN 'FOLDER' ELSE 'DETAIL' END) as rsl_type,0
  FROM #tmp_acc_list WHERE acctype=@rsl_acc_type AND acclevel<>1 order by department,acc_code

  INSERT INTO #tmp_rsl(acc_id,department,department_name,parent_code,acc_code,description,acclevel,
    yr_balance,ly_balance,mn_balance,lm_balance,rsl_type,show_line)
  SELECT 0,department,department_name,'','' as acc_code,'' as acc_name,acclevel,
    ISNULL(yr_debit,0)-ISNULL(yr_credit,0),
    ISNULL(ly_debit,0)-ISNULL(ly_credit,0),
    ISNULL(mn_debit,0)-ISNULL(mn_credit,0),ISNULL(lm_debit,0)-ISNULL(lm_credit,0),
    'CAT_SUMM' as rsl_type,1
  FROM #tmp_acc_list WHERE acctype=@rsl_acc_type AND acclevel=1

  INSERT INTO #tmp_rsl(acc_id,department,department_name,parent_code,acc_code,description,acclevel,
    yr_balance,ly_balance,mn_balance,lm_balance,rsl_type,show_line)
  SELECT 0,department,department_name,'','NP' as acc_code,'NET PROFIT/(LOSS)' as acc_name,1,
    SUM((CASE WHEN acc_code='CS' OR acc_code='EX' THEN -1*yr_balance ELSE yr_balance END)),
    SUM((CASE WHEN acc_code='CS' OR acc_code='EX' THEN -1*ly_balance ELSE ly_balance END)),
    SUM((CASE WHEN acc_code='CS' OR acc_code='EX'  THEN -1*mn_balance ELSE mn_balance END)),
    SUM((CASE WHEN acc_code='CS' OR acc_code='EX'  THEN -1*lm_balance ELSE lm_balance END)),
    'CAT_NETPROFIT' as rsl_type,1
  FROM #tmp_rsl WHERE rsl_type='CAT_HEAD'

那么,是否有人可以指出可能的错误,如果它存在于我所显示的代码部分中?

我正在使用Microsoft的SQL Server Management Studio 2014。

3 个答案:

答案 0 :(得分:1)

错误中的行号通常会产生误导,特别是对于存储过程。它可以很好地指示错误所在的查询部分(特别是如果有数百行),但您应该关注错误的描述。

  

列'#tmp_rsl.department'在选择列表中无效,因为它   不包含在聚合函数或GROUP BY中   子句。

选择列表包含一个不包含在聚合函数或group by子句中的字段(部门)。 只有一个带有聚合函数的查询,它也没有group by子句。

  INSERT INTO #tmp_rsl(acc_id,department,department_name,parent_code,acc_code,description,acclevel,
    yr_balance,ly_balance,mn_balance,lm_balance,rsl_type,show_line)
  SELECT 0,department,department_name,'','NP' as acc_code,'NET PROFIT/(LOSS)' as acc_name,1,
    SUM((CASE WHEN acc_code='CS' OR acc_code='EX' THEN -1*yr_balance ELSE yr_balance END)),
    SUM((CASE WHEN acc_code='CS' OR acc_code='EX' THEN -1*ly_balance ELSE ly_balance END)),
    SUM((CASE WHEN acc_code='CS' OR acc_code='EX'  THEN -1*mn_balance ELSE mn_balance END)),
    SUM((CASE WHEN acc_code='CS' OR acc_code='EX'  THEN -1*lm_balance ELSE lm_balance END)),
    'CAT_NETPROFIT' as rsl_type,1
  FROM #tmp_rsl WHERE rsl_type='CAT_HEAD'

此查询的SUM部分是聚合函数。 您需要在FROM之后添加GROUP BY。 可能需要一些部门,dept_name, acc_code包含在聚合函数

答案 1 :(得分:0)

你在查询中做了这样的事......

select EmpId, count(LocationId)
from Employee

或者与之相反的事情

select EmpId, LocationId
from Employee
Group by EmpId

在这两种情况下都会引发以下错误

  

列'Employee.EmpId'在选择列表中无效,因为它是   不包含在聚合函数或GROUP BY子句中。

如果仔细查看您的查询,您会使用一些汇总函数,如SUM(在您上次查询中)等。

答案 2 :(得分:0)

问题是您在最终的insert语句中使用了聚合函数(SUM)。你可能会发现this文章很有用..

与此同时..

INSERT INTO #tmp_rsl(acc_id,department,department_name,parent_code,acc_code,description,acclevel,yr_balance,ly_balance,mn_balance,lm_balance,rsl_type,show_line)
SELECT 0,department,department_name,'','NP' as acc_code,'NET PROFIT/(LOSS)' as acc_name,1,
  SUM((CASE WHEN acc_code='CS' OR acc_code='EX' THEN -1*yr_balance ELSE yr_balance END)),
  SUM((CASE WHEN acc_code='CS' OR acc_code='EX' THEN -1*ly_balance ELSE ly_balance END)),
  SUM((CASE WHEN acc_code='CS' OR acc_code='EX'  THEN -1*mn_balance ELSE mn_balance END)),
  SUM((CASE WHEN acc_code='CS' OR acc_code='EX'  THEN -1*lm_balance ELSE lm_balance END)),
  'CAT_NETPROFIT' as rsl_type,1
FROM #tmp_rsl WHERE rsl_type='CAT_HEAD'

应该是

INSERT INTO #tmp_rsl(acc_id,department,department_name,parent_code,acc_code,description,acclevel,yr_balance,ly_balance,mn_balance,lm_balance,rsl_type,show_line)
SELECT 0,department,department_name,'','NP' as acc_code,'NET PROFIT/(LOSS)' as acc_name,1,
  SUM((CASE WHEN acc_code='CS' OR acc_code='EX' THEN -1*yr_balance ELSE yr_balance END)),
  SUM((CASE WHEN acc_code='CS' OR acc_code='EX' THEN -1*ly_balance ELSE ly_balance END)),
  SUM((CASE WHEN acc_code='CS' OR acc_code='EX'  THEN -1*mn_balance ELSE mn_balance END)),
  SUM((CASE WHEN acc_code='CS' OR acc_code='EX'  THEN -1*lm_balance ELSE lm_balance END)),
  'CAT_NETPROFIT' as rsl_type,1
FROM #tmp_rsl WHERE rsl_type='CAT_HEAD'
GROUP BY department,department_name