Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 将数据显示为分类账_Sql Server_Sql Server 2008 - Fatal编程技术网

Sql server 将数据显示为分类账

Sql server 将数据显示为分类账,sql-server,sql-server-2008,Sql Server,Sql Server 2008,我正在使用SQLServer2008。我有一张进阶表 当任何人获得贷款时,贷款金额存储在金额列中,日期存储在PaidDate中,人员代码存储在代码列中。当此人返还金额时,该金额存储在ReceiveAmount中,日期存储在ReceiveDate中 现在我想创建一个类似于特定代码分类账的报告 例如,代码102 ---------------------------------------------------------------------------- PaidDate / Receiv

我正在使用SQLServer2008。我有一张进阶表

当任何人获得贷款时,贷款金额存储在金额列中,日期存储在PaidDate中,人员代码存储在代码列中。当此人返还金额时,该金额存储在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
我该怎么做?请帮帮我。。谢谢

尝试以下未经测试的方法:

;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
尝试以下未经测试的方法:

;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

这里有一种方法:

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

看起来你的数据也有轻微的输入错误;为了得到你期望的结果,我已经对它稍加修改


还值得一提的是,如果您每天只收到一个代码的支付/接收操作,那么您可以在查询中不使用任何分组。这里有一种方法:

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

看起来你的数据也有轻微的输入错误;为了得到你期望的结果,我已经对它稍加修改


还值得一提的是,如果您每天只收到一个代码的“支付/接收”操作,那么您可以在查询中不使用任何GROUP BY。

这在TSQL代码中可能是可行的,但最好使用报告工具或自定义应用程序格式化数据以供显示。TSQL不是一种用于格式化或显示数据的好语言。这在TSQL代码中可能是可行的,但最好使用报表工具或自定义应用程序格式化数据以进行显示。TSQL不是格式化或显示数据的好语言。