2016-03-18 137 views
5

我有這個SQL查詢,我用它來查找每月每個帳戶的試算平衡移動。SQL - 返回累計每月列值

/* Fixed Assets 1 */ 
SELECT * FROM 
(
    SELECT T0.AcctCode AS 'SAP Code', T0.AcctName AS 'Description', 
    MONTH(T1.RefDate) AS Month, SUM(T1.Debit - T1.Credit) AS 'Amount' 
    FROM OACT T0 
    LEFT JOIN JDT1 T1 ON T0.[AcctCode] = T1.[Account] 
    WHERE T0.AcctCode LIKE '111%' AND T0.Levels = 5 
    AND (T1.RefDate BETWEEN DATEADD(yy, DATEDIFF(yy,0,{?AsAtDate}), 0) AND {?AsAtDate}) 
    GROUP BY T0.AcctCode, T0.AcctName, T0.FatherNum, T1.RefDate) AS q 
    PIVOT 
(
SUM(Amount) 
FOR [Month] IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12]) 
) AS query 

這從六月返回以下結果:

enter image description here

我想包括這樣的所有行按月累積結果該行 1次的結果應該是:

1, 111110, Building Gross Value, 633604.23, 637764.23, 645313.03, 649061.78, 651097.78, 651097.78, 651397.78 

我沒有成功實現這一目標。如何修改查詢來實現此目的?

其他數據:

查詢:

SELECT CAST(T0.TaxDate AS Date), T0.Account, T1.AcctName, T0.Debit, T0.Credit 
FROM JDT1 T0 INNER JOIN OACT T1 
ON T0.Account = T1.AcctCode 
WHERE T0.[Account] = '111110' 

返回:

enter image description here

+0

提供一些測試數據 – TheGameiswar

+0

我已經添加了一些信息 –

回答

3

需要外選擇在其中添加列轉移到另一個:

SELECT AcctCode AS 'SAP Code', AcctName AS 'Description', 
col1 AS '1', 
col1 + col2 AS '2', 
col1 + col2 + col3 AS '3', 
... FROM (
    SELECT AcctCode, AcctName, 
    [1] AS col1, 
    [2] AS col2, 
    [3] AS col3, 
    ... FROM 
    (
     SELECT T0.AcctCode, T0.AcctName, 
     MONTH(T1.RefDate) AS Month, SUM(T1.Debit - T1.Credit) AS 'Amount' 
     FROM OACT T0 
     LEFT JOIN JDT1 T1 ON T0.[AcctCode] = T1.[Account] 
     WHERE T0.AcctCode LIKE '111%' AND T0.Levels = 5 
     AND (T1.RefDate BETWEEN DATEADD(yy, DATEDIFF(yy,0,{?AsAtDate}), 0) AND {?AsAtDate}) 
     GROUP BY T0.AcctCode, T0.AcctName, T0.FatherNum, T1.RefDate) AS q 
     PIVOT 
    (
    SUM(Amount) 
    FOR [Month] IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12]) 
    ) AS query 
) 
+0

嗯,試試你的方式沒有返回任何結果......只是空值。 –

+0

我編輯了我的答案 - 在旋轉不工作時在select中添加列,但是您可以選擇這些列並使用select中的另一個select來將列添加到另一個列 – dey

0

最直接的方法是轉儲樞軸的結果到一個臨時表,或如果數據集相當小,則爲表變量。然後,只需使用UNION從PIVOT獲取累積結果和子類別。這是一個使用表變量的例子。

/* Fixed Assets 1 */ 
SELECT * 
INTO #FixedAssets FROM 
(
    SELECT T0.AcctCode AS 'SAP Code', T0.AcctName AS 'Description', 
    MONTH(T1.RefDate) AS Month, SUM(T1.Debit - T1.Credit) AS 'Amount' 
    FROM OACT T0 
    LEFT JOIN JDT1 T1 ON T0.[AcctCode] = T1.[Account] 
    WHERE T0.AcctCode LIKE '111%' AND T0.Levels = 5 
    AND (T1.RefDate BETWEEN DATEADD(yy, DATEDIFF(yy,0,{?AsAtDate}), 0) AND {?AsAtDate}) 
    GROUP BY T0.AcctCode, T0.AcctName, T0.FatherNum, T1.RefDate) AS q 
    PIVOT 
(
SUM(Amount) 
FOR [Month] IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12]) 
) AS query 
GO 

SELECT 
     [SAP Code] = '111110' 
    , [Description] = 'Building Gross Value' 
    , [1]   = SUM([1]) 
    , [2]   = SUM([2]) 
    , [3]   = SUM([3]) 
    , [4]   = SUM([4]) 
    , [5]   = SUM([5]) 
    , [6]   = SUM([6]) 
    , [7]   = SUM([7]) 
    , [8]   = SUM([8]) 
    , [9]   = SUM([9]) 
    , [10]   = SUM([10]) 
    , [11]   = SUM([11]) 
    , [12]   = SUM([12]) 
UNION 
SELECT * FROM #FixedAssets 
ORDER BY 1 
0

如果我明白你的問題正確,則需要按月累積結果在一排用逗號分隔的借記值

create table a 
(dateval date, acc int,accname varchar(100), debit decimal(10,2)) 

insert into a 
values 
('20150630', 111110, 'Building Gross Value', 633604.230), 
('20150731', 111110, 'Building Gross Value', 2760.000000), 
('20150730', 111110, 'Building Gross Value', 1400.000000), 
('20150808', 111110, 'BUIIdlngGrossValue', 1890.00), 
('20150811', 111110, 'BUIIdlnanossValue', 180.00), 
('20150811', 111110, 'Building Gross Value', 375.000000), 
('20150819', 111110, 'Building Gross Value', 2622.200000), 
('20150821', 111110, 'Building Gross Value', 360.000000), 
('20150822', 111110, 'Building Gross Value', 21.600000), 
('20150824', 111110, 'Building Gross Value', 100.000000), 
('20150825', 111110, 'Building Gross Value', 770.000000), 
('20150829', 111110, 'Building Gross Value', 100.000000), 
('20150831', 111110, 'Building Gross Value', 340.000000), 
('20150831', 111110, 'Building Gross Value', 790.000000), 
('20150924', 111110, 'Building Gross Value' ,918.750000), 
('20150928', 111110, 'Building Gross Value', 2830.000000), 
('20151005', 111110, 'Building Gross Value', 1411.000000), 
('20151023', 111110, 'Building Gross Value', 625.000000) 

現在下面提到的查詢會以逗號分隔值給出一行結果。

with cte as 
(
select * from 
(
    select a.dateval,b.acc,b.accname,sum(b.debit) as debit,row_number() over (partition by year(a.dateval),month(a.dateval) order by a.dateval desc) as rn 
    from (select distinct dateval from a) as a 
    inner join a as b 
    on a.dateval >= b.dateval 
    group by a.dateval,b.acc,b.accname 
) as a 
where rn = 1 
) 
select acc,accname,stuff((select ',' + cast(debit as varchar(1000)) from cte as b where a.acc = b.acc for xml path ('')),1,1,'') 
from cte as a 
group by acc,accname