2
請看下面的查詢。如何總結sql查詢中的數據
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 |
意味着我要爲整個月的一個值而不是給我不同的日期。請幫助我。
使用派生表,您提取「may2015」部分之日起,也有當前的情況。然後像以前一樣執行GROUP BY。 – jarlh
我想在這裏查詢。我們可以做到嗎? – Kirat
@Kirat你正在做T3組[日期],因爲日期不同,所以你得到了T3的多個值[RefDate],只是好奇有多少T3。[RefDate]在你的表中在指定的時間內? –