在SQL查询中获取错误

时间:2015-12-01 13:23:27

标签: sql sql-server

我的查询如下。我正在

  

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

当我尝试运行它时出现

错误。请查看下面的查询

SELECT T3.[AcctName],
       SUM(T0.[DebLTotal]/85) AS buget, sum(T0.[DebRLTotal]/85) as 'Current Expenses',T4.[Debit]
FROM OBGT T0
INNER JOIN
    (
    SELECT CASE WHEN T1.[AcctName] LIKE '%Salaries%' THEN 'Salaries'
                WHEN T1.[AcctName] LIKE '%Travel%' THEN 'Travel'
                WHEN T1.[AcctName] LIKE '%Supplies%' THEN 'Supplies'
                WHEN T1.[AcctName] LIKE '%Consultants%' THEN 'Consultants'
                WHEN T1.[AcctName] LIKE '%Patient%' THEN 'Patient Care'
                WHEN T1.[AcctName] LIKE '%Equipment%' THEN 'Equipments' 
                WHEN T1.[AcctName] LIKE '%Expense%' THEN 'Other Expenses' 
                WHEN T1.[AcctName] LIKE '%Alteration%' THEN 'Alterations/Renovations'
                ELSE 'Alterations/Renovations'
           END as [AcctName],
           [AcctCode],[Project]
    from OACT AS T1
    ) as T3 ON T0.[AcctCode] = T3.[AcctCode]
INNER JOIN OBGS T2 ON T0.[Instance] = T2.[AbsId] INNER JOIN JDT1 T4 ON T3.[AcctCode] = T4.[Account] INNER JOIN OASC T5 ON T3.[Project]=T5.[Code]
where T2.[Name] = 'Main Budget 2015'
GROUP BY T3.[AcctName],T2.[Name]

2 个答案:

答案 0 :(得分:1)

T4。[借记]不在您的分组条款中。由于T4周围没有聚合函数。[借记],你得到这个错误。

答案 1 :(得分:1)

只需在T4.[Debit]子句中添加group by或从select子句中删除它。因为避免group by中的未聚合列会导致不确定的行为。

有关详细信息:Why Must That Column Be Contained in an Aggregate Function or the GROUP BY clause?

SELECT T3.[AcctName], SUM(T0.[DebLTotal]/85) AS buget, 
       sum(T0.[DebRLTotal]/85) as 'Current Expenses',T4.[Debit]
FROM OBGT T0
INNER JOIN
    (
    SELECT CASE WHEN T1.[AcctName] LIKE '%Salaries%' THEN 'Salaries'
                WHEN T1.[AcctName] LIKE '%Travel%' THEN 'Travel'
                WHEN T1.[AcctName] LIKE '%Supplies%' THEN 'Supplies'
                WHEN T1.[AcctName] LIKE '%Consultants%' THEN 'Consultants'
                WHEN T1.[AcctName] LIKE '%Patient%' THEN 'Patient Care'
                WHEN T1.[AcctName] LIKE '%Equipment%' THEN 'Equipments' 
                WHEN T1.[AcctName] LIKE '%Expense%' THEN 'Other Expenses' 
                WHEN T1.[AcctName] LIKE '%Alteration%' THEN 'Alterations/Renovations'
                ELSE 'Alterations/Renovations'
           END as [AcctName],
           [AcctCode],[Project]
    from OACT AS T1
    ) as T3 ON T0.[AcctCode] = T3.[AcctCode]
INNER JOIN OBGS T2 ON T0.[Instance] = T2.[AbsId] 
INNER JOIN JDT1 T4 ON T3.[AcctCode] = T4.[Account] 
INNER JOIN OASC T5 ON T3.[Project]=T5.[Code]
where T2.[Name] = 'Main Budget 2015'
GROUP BY T3.[AcctName],T2.[Name],T4.[Debit]