请查看以下查询。
SELECT
AcctName = case
when substring(T1.[Segment_0], 1, 4) in ('6017' , '6018') then 'Equipments'
when substring(T1.[Segment_0],1,4) in ('6020') then 'Consultants'
else T1.[AcctName]
end ,
SUM(T0.[DebLTotal]/85) AS Budget, SUM(T0.[DebRLTotal]/85) AS 'Current Expenses',
SUM(T3.[Debit]/85) AS 'Cummulative Expanses',( SUM(T0.[DebLTotal]/85)- SUM(T3.[Debit]/85)) as 'UNEXPENDED BALANCE',T2.[Name]
,T4.[Name] ,T3.[RefDate]
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]
WHERE T3.[RefDate] BETWEEN '2015-05-01' AND '2015-05-31' AND T2.[Name] = 'Main Budget 2015' and T4.[Name] = 'Faces'
GROUP BY
case
when substring(T1.[Segment_0], 1, 4) in ('6017' , '6018') then 'Equipments'
when substring(T1.[Segment_0],1,4) in ('6020') then 'Consultants'
else T1.[AcctName]
end ,T2.[Name],T4.[Name] ,T3.[RefDate]
我得到的结果如下表所示:
| AcctName | Budget | currentExpenses | date |
|------------- |---------- |----------------- |----------- |
| equipment | 12123 | 32342 | 1may2015 |
| equipment | 34265234 | 27364723 | 31may2015 |
| consulants | 54635 | 474345 | 2may2015 |
| consultants | 643654 | 73264 | 29may2015 |
我想要的结果如下:
| AcctName | Budget | currentExpenses | date |
|------------ |---------- |----------------- |--------- |
| equipment | 12123 | 32342 | may2015 |
| consultant | 34265234 | 27364723 | may2015 |
意味着我想要整个月的一个值,而不是给我不同的日期。请帮帮我。
答案 0 :(得分:2)
这应该得到你想要的(按照RefDate的月份和年份进行分组):
SELECT
AcctName = case
when substring(T1.[Segment_0], 1, 4) in ('6017' , '6018') then 'Equipments'
when substring(T1.[Segment_0],1,4) in ('6020') then 'Consultants'
else T1.[AcctName]
end ,
SUM(T0.[DebLTotal]/85) AS Budget, SUM(T0.[DebRLTotal]/85) AS 'Current Expenses',
SUM(T3.[Debit]/85) AS 'Cummulative Expanses',( SUM(T0.[DebLTotal]/85)- SUM(T3.[Debit]/85)) as 'UNEXPENDED BALANCE',T2.[Name]
,T4.[Name] ,datename(month, T3.[RefDate]) + cast(year(T3.[RefDate]) as varchar) As MonthAndYear
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]
WHERE T3.[RefDate] BETWEEN '2015-05-01' AND '2015-05-31' AND T2.[Name] = 'Main Budget 2015' and T4.[Name] = 'Faces'
GROUP BY
case
when substring(T1.[Segment_0], 1, 4) in ('6017' , '6018') then 'Equipments'
when substring(T1.[Segment_0],1,4) in ('6020') then 'Consultants'
else T1.[AcctName]
end ,T2.[Name],T4.[Name] ,DATENAME(MONTH, (T3.[RefDate])), YEAR(T3.[REfDate])