2013-04-27 52 views
1

我正在使用SQL Server 2008.我有一個表AdvanceEntry顯示數據爲分類帳

-------------------------------------------------------------------------------- 
    Code | PaidDate |  Amount  | ReceiveDate | ReceiveAmount 
-------------------------------------------------------------------------------- 
    102 | 15-04-2004 |  3000  |  20-04-2004 |  2000 
    104 | 23-05-2006 |  1000  |  NULL  |  0.00 
    104 | 25-05-2005 |  1500  |  12-06-2005 |  500 

當任何人釘貸款然後貸款量被存儲在Amount柱和日期存儲在PaidDate和人代碼存儲在Code柱。當那人還給那麼量存儲在ReceiveAmount和日期存儲在ReceiveDate量。

現在我想創建一個類似特定代碼的分類賬的報表。

例如碼102

---------------------------------------------------------------------------- 
PaidDate/ReceiveDate | Amount | ReceiveAmount | Balance 
---------------------------------------------------------------------------- 
    15-04-2004    | 3000  |  0   | 3000 
    20-04-2004    |  0  |  2000  | 1000 

而對於代碼104

---------------------------------------------------------------------------- 
PaidDate/ReceiveDate | Amount | ReceiveAmount | Balance 
---------------------------------------------------------------------------- 
    23-05-2006    | 1000  |  0   | 1000 
    25-05-2005    | 1500  |  0   | 2500 
    12-06-2005    |  0  |  500   | 2000 

我怎樣才能做到這一點?請幫我..謝謝

+0

這是TSQL代碼也許可能,但它會好得多使用報告工具或自定義應用程序格式化數據的介紹。 TSQL不是用於格式化或顯示數據的好語言。 – Pondlife 2013-04-29 16:47:32

回答

1

下面是做這件事的一種方法:

with Paid as 
(
    select Code 
    , PaidDate 
    , Amount 
    from AdvanceEntry 
    where PaidDate is not null 
), Received as 
(
    select Code 
    , ReceiveDate 
    , ReceiveAmount 
    from AdvanceEntry 
    where ReceiveDate is not null 
), Details as 
(
    select Code = coalesce(p.Code, r.Code) 
    , CodeDate = coalesce(p.PaidDate, r.ReceiveDate) 
    , Amount = sum(p.Amount) 
    , ReceiveAmount = sum(r.ReceiveAmount) 
    from Paid p 
    full join Received r on p.PaidDate = r.ReceiveDate and p.Code = r.Code 
    group by coalesce(p.Code, r.Code) 
    , coalesce(p.PaidDate, r.ReceiveDate) 
) 
select d.Code 
    , PayReceiveDate = d.CodeDate 
    , Amount = isnull(d.Amount, 0.0) 
    , ReceiveAmount = isnull(d.ReceiveAmount, 0.0) 
    , Balance = isnull(b.Balance, 0.0) 
from Details d 
    outer apply (select Balance = sum(isnull(b.Amount, 0.0) - isnull(b.ReceiveAmount, 0.0)) 
       from Details b where d.Code = b.Code and d.CodeDate >= b.CodeDate) b 
order by d.Code, d.CodeDate 

SQL Fiddle with demo

它也像你有你的數據有輕微錯字;我已經在小提琴中稍微改變它以獲得預期的結果。

另外值得一提的是,如果你只得到一個薪酬/接收每碼天行動就可以擺脫查詢中不包含任何GROUP BY

0

嘗試這種(未經測試):

;with cte as (
    select Code, PaidDate as Date, Amount as Dr, 0 as Cr, Amount as Net 
    from Data where PaidDate is not null 
    union all 
    select Code, ReceivedData as Date, 0 as Dr, -ReceivedAmount as Cr, -ReceivedAmount as Net 
    from Data where ReceivedDate is not null 
) 
select 
    t1.*, sum(t2.Net) as Balance 
from cte as t1 
left join cte as t2 on t2.Code = t1.Code and t2.Date <= t1.Date 
group by 
    t1.Code, t1.Date 
having t1.Code = @Code 
+0

它的工作,但沒有給我想要的結果。 – Prashant16 2013-04-27 20:45:00

+0

@ Prashant16:怎麼這樣? – 2013-04-27 20:51:37