两个表的累计和

时间:2014-06-27 08:13:25

标签: sql sql-server sql-server-2008

我有两张Bill& amp;付款。我已经从这两张表中显示了资产负债表。

表格中的数据是:

tblBill

enter image description here

tblPayment

enter image description here

我目前的输出是:

enter image description here

我正在尝试使用的查询是:

select Particulars,Date,BillAmount,0'PaidAmount' from tblBill
union
select Particulars,Date,0'BillAmount',PaidAmount from tblPayment
order by Date

但是,我需要这种格式的输出:

enter image description here

是否可以获得所需的格式?

4 个答案:

答案 0 :(得分:3)

你去了: 假设一天只有一笔交易......

With Tb1 as
(select Date,Particulars,BillAmount,0'PaidAmount' from tblBill
union
select Date,Particulars,0'BillAmount',PaidAmount from tblPayment
)

SELECT T1.Particulars,T1.[Date],T1.[BillAmount],T1.[PaidAmount],(Sum(T2.BillAmount) - Sum(T2.PaidAmount)) as Balance FROM Tb1 as T1
            INNER JOIN
                Tb1 as T2
                ON T1.[date] >= T2.[date]
                Group By T1.Particulars,T1.[Date],T1.[BillAmount],T1.[PaidAmount]
                Order by [Date]

如果一天内有多笔交易......

WITH Tb0 as
( SELECT [Date],Particulars,BillAmount,0'PaidAmount' from tblBill
        UNION
        SELECT [Date],Particulars,0'BillAmount',PaidAmount from tblPayment
)

, Tb1 as
( 
    SELECT Date,Particulars,BillAmount,PaidAmount,Row_Number() over (order by [Date] asc) as [OrderId]
        FROM    
            Tb0
)

SELECT T1.Particulars,T1.[Date],T1.[BillAmount],T1.[PaidAmount],(Sum(T2.BillAmount) - Sum(T2.PaidAmount)) as Balance FROM Tb1 as T1
            INNER JOIN
                Tb1 as T2
                ON T1.[OrderId] >= T2.[OrderId]
                Group By T1.Particulars,T1.[Date],T1.[BillAmount],T1.[PaidAmount]
                Order by [Date]

答案 1 :(得分:0)

您需要加入两个表格。首先,两个表之间必须有一个链接(比如说,customerid存在以显示涉及哪个客户)。

然后,你可以做。

CREATE VIEW vwTransactionHistory as 
SELECT customerid, Particulars, [DATE], BillAmount, PaidAmount, 
    (SELECT SUM(BillAmount) FROM tblBill x WHERE x.customerid=temp1.customerid and x.date<=temp1.date) as bill2date, (SELECT SUM(PaidAmount) FROM tblPayment y WHERE y.customerid = temp1.customerid and y.date<=temp1.date) as Pay2Date
FROM
(
    select customerid, Particulars,[Date],BillAmount,0 AS 'PaidAmount' from tblBill
    union
    select customerid,Particulars,[Date],0 AS 'BillAmount',PaidAmount from tblPayment
) AS temp1
GROUP BY customerid, Particulars,[Date],BillAmount,PaidAmount

然后你可以做

    SELECT TOP 1000 [customerid]
      ,[Particulars]
      ,[DATE]
      ,[BillAmount],[PaidAmount], isnull(bill2date,0) - isnull(pay2date,0) as Balance  
FROM [vwTransactionHistory]

请记住,您不需要创建视图。我使用视图来清晰和抽象复杂的查询。

答案 2 :(得分:0)

您可以使用此查询

SELECT Particulars,Date,BillAmount,PaidAmount,BillAmount-PaidAmount as Balance 
FROM(
select Particulars,Date,BillAmount,0'PaidAmount' from tblBill
union
select Particulars,Date,0'BillAmount',PaidAmount from tblPayment
order by Date
)
ORDER BY Date;

答案 3 :(得分:0)

检查此查询

select * from 
(
select Particulars,Date,BillAmount,0'PaidAmount' , BillAmount as Balance from tblBill
union
select Particulars,Date,0'BillAmount',PaidAmount, BillAmount - PaidAmount as Balance
 from tblPayment p
inner join tblBill b where p.Particulars = p.Particulars
) a
order by Date