使用Pivot的月份数据与前几个月的所需余额总和

时间:2014-03-13 16:43:51

标签: sql sql-server tsql

我有以下数据:

Party Balance  Month 
---------------------       
ABC   500   Aug 2013  
ABC   300   Sep 2013  
ABC   200   Nov 2013  
ABC   400   Dec 2013  
ABC   100   Jan 2013  

我尝试了这个查询:

select 
    ahead as Party, [Nov 2013], [Dec 2013], [Jan 2014] 
from 
    (select 
         ahead, month, balance 
     from aging) as a
PIVOT
   (SUM (balance)
    FOR [month] in ([Nov 2013], [Dec 2013], [Jan 2014])
   ) as Pivot_Table
group by 
    ahead, [Nov 2013], [Dec 2013], [Jan 2014]

结果返回如下:

Party     Nov 2013   Dec 2013   Jan 2013 
-----------------------------------------
 ABC        200         400       100

所需的结果如下:

Party     Nov 2013   Dec 2013   Jan 2013   Old balance   total Balance
-----------------------------------------------------------------------
 ABC        200         400       100          800            1500

任何人都可以帮我解决这个问题=

3 个答案:

答案 0 :(得分:1)

SQL Fiddle

MS SQL Server 2008架构设置

CREATE TABLE Test_Table(Party VARCHAR(10),Balance INT,[Month] VARCHAR(20))
INSERT INTO Test_Table VALUES 
('ABC',500,'Aug 2013'),  
('ABC',300,'Sep 2013'),  
('ABC',200,'Nov 2013'),  
('ABC',400,'Dec 2013'),  
('ABC',100,'Jan 2013')

查询1

;WITH Totals
AS
  (
  SELECT Party, SUM(Balance) TotalBalance
  FROM Test_Table
  GROUP BY Party
  ),
Pvt
AS
 (
    select  Party
          ,[Nov 2013]
          ,[Dec 2013]
          ,[Jan 2013] 
    FROM Test_Table as t
    PIVOT
    (SUM (balance)
    FOR 
    [month]
    in ([Nov 2013],[Dec 2013],[Jan 2013])
    ) as Pivot_Table
)  
SELECT     p.Party
          ,p.[Nov 2013]
          ,p.[Dec 2013]
          ,p.[Jan 2013]  
          ,(t.TotalBalance) -(p.[Nov 2013]+ p.[Dec 2013]+p.[Jan 2013])  AS OldBalance
FROM pvt p INNER JOIN Totals t
ON p.Party = t.Party

<强> Results

| PARTY | NOV 2013 | DEC 2013 | JAN 2013 | OLDBALANCE |
|-------|----------|----------|----------|------------|
|   ABC |      200 |      400 |      100 |        800 |

答案 1 :(得分:1)

DECLARE @MyTable TABLE ([Party] varchar(max), [Balance] money, [Date] date)
INSERT @MyTable VALUES
('ABC',500,'Aug 2013'),
('ABC',300,'Sep 2013'),
('ABC',200,'Nov 2013'),
('ABC',400,'Dec 2013'),
('ABC',100,'Jan 2014');

WITH t AS (
  SELECT
    [Party],[Balance],[Date],
    SUM(CASE WHEN [Date] < 'Nov 2013' THEN [Balance] END)
      OVER(PARTITION BY [Party]) [OldBalance],
    SUM([Balance])
      OVER(PARTITION BY [Party]) [TotalBalance]
  FROM @MyTable
)
SELECT [Party],[Nov 2013],[Dec 2013],[Jan 2014],[OldBalance],[TotalBalance]
FROM t
PIVOT(SUM([Balance]) FOR [Date] IN ([Nov 2013],[Dec 2013],[Jan 2014])) p

答案 2 :(得分:0)

计算另一个可以加入当前查询的派生表中的旧余额。

尝试

SELECT m.Party
    ,m.[Nov 2013]
    ,m.[Dec 2013]
    ,m.[Jan 2014]
    ,t.[Old Balance]
FROM (
    SELECT ahead AS Party
        ,[Nov 2013]
        ,[Dec 2013]
        ,[Jan 2014]
    FROM aging AS a
    PIVOT
    (
        SUM(balance) 
        FOR [month] IN ([Nov 2013], [Dec 2013], [Jan 2014])
    ) AS p
) m
JOIN (
    SELECT ahead AS Party
        ,SUM(Balance) AS [Old Balance]
    FROM aging
    WHERE Month < '20131101' -- hopefully this is a date or you have another column as date 
) t ON t.Party = m.Party