我正试图获得以下结果。是指我生成查询的所有运营成本和工资等的总和。这写在下面
SELECT T1.[Segment_0],SUM(T0.[DebLTotal]) AS BUDGET, SUM(T3.[Debit]) AS DEBIT,
AcctName = case when substring(T1.[Segment_0], 1, 4) between '6001' and '6027' then 'Operating Cost'
when substring(T1.[Segment_0],1,4) between '5101' and '5104' then 'Travel'
when substring (T1.[Segment_0],1,4) between '5001' and '5001' then 'Salaries'
else T1.[AcctName] end
FROM OBGT T0 INNER JOIN OACT T1 ON T0.[AcctCode]=T1.[AcctCode] INNER JOIN OBGS T2
ON T0.[Instance] = T2.[AbsId] INNER JOIN JDT1 T3 ON T1.[AcctCode] = T3.[Account] INNER JOIN OASC T4 ON T1.[Project]=T4.[Code]
GROUP BY T1.[AcctCode],T1.[Segment_0],T1.[AcctName]
SELECT T1.[Segment_0],SUM(T0.[DebLTotal]) AS BUDGET, SUM(T3.[Debit]) AS DEBIT,
AcctName = case when substring(T1.[Segment_0], 1, 4) between '6001' and '6027' then 'Operating Cost'
when substring(T1.[Segment_0],1,4) between '5101' and '5104' then 'Travel'
when substring (T1.[Segment_0],1,4) between '5001' and '5001' then 'Salaries'
else T1.[AcctName] end
FROM OBGT T0 INNER JOIN OACT T1 ON T0.[AcctCode]=T1.[AcctCode] INNER JOIN OBGS T2
ON T0.[Instance] = T2.[AbsId] INNER JOIN JDT1 T3 ON T1.[AcctCode] = T3.[Account] INNER JOIN OASC T4 ON T1.[Project]=T4.[Code]
GROUP BY T1.[AcctCode],T1.[Segment_0],T1.[AcctName]
我期待此查询的以下结果。但我得到的价值就像第二张桌子。请看第一张下面的第二张表。
这是我得到的结果。请看下表
+--+-----------------+--+-----------------+-------------------------------------+
| | ITEM | | BUDGET | CURRENT BUDGET EXPENSES OCT |
+--+-----------------+--+-----------------+-------------------------------------+
| | | | | |
| | Personnel Costs | | | |
| | Salaries | | $4,888,724.00 | $383,153.89 |
| | Operatingcost | | $540,218.00 | $53,192.71 |
+--+-----------------+--+-----------------+-------------------------------------+
答案 0 :(得分:1)
您必须从T1.[Segment_0]
子句中删除SELECT
。此外,修改GROUP BY
子句并改为使用CASE
表达式:
SELECT
SUM(T0.[DebLTotal]) AS BUDGET,
SUM(T3.[Debit]) AS DEBIT,
AcctName =
CASE
WHEN SUBSTRING(T1.[Segment_0], 1, 4) BETWEEN '6001' AND '6027' THEN 'Operating Cost'
WHEN SUBSTRING(T1.[Segment_0], 1, 4) BETWEEN '5101' AND '5104' THEN 'Travel'
WHEN SUBSTRING(T1.[Segment_0], 1 ,4) BETWEEN '5001' AND '5001' THEN 'Salaries'
ELSE T1.[AcctName]
END
FROM OBGT T0
INNER JOIN OACT T1
ON T0.[AcctCode] = T1.[AcctCode]
INNER JOIN OBGS T2
ON T0.[Instance] = T2.[AbsId]
INNER JOIN JDT1 T3
ON T1.[AcctCode] = T3.[Account]
INNER JOIN OASC T4
ON T1.[Project]=T4.[Code]
GROUP BY
CASE
WHEN SUBSTRING(T1.[Segment_0], 1, 4) BETWEEN '6001' AND '6027' THEN 'Operating Cost'
WHEN SUBSTRING(T1.[Segment_0], 1, 4) BETWEEN '5101' AND '5104' THEN 'Travel'
WHEN SUBSTRING(T1.[Segment_0], 1 ,4) BETWEEN '5001' AND '5001' THEN 'Salaries'
ELSE T1.[AcctName]
END