需要此场景的SQL查询吗

需要此场景的SQL查询吗,sql,sql-server,Sql,Sql Server,tbl_员工 empid empname openingbal 2 jhon 400 3 smith 500 tbl_横切面1 tid empid amount creditdebit date 1 2 100 1 2016-01-06 00:00:00.000 2 2 200 1 2016-01-08 00:00:00.000 3 2 100

tbl_员工

empid empname openingbal 
2     jhon    400    
3     smith   500   
tbl_横切面1

tid  empid  amount  creditdebit date
1    2      100     1           2016-01-06 00:00:00.000
2    2      200     1           2016-01-08 00:00:00.000
3    2      100     2           2016-01-11 00:00:00.000
4    2      700     1           2016-01-15 00:00:00.000
5    3      100     1           2016-02-03 00:00:00.000
6    3      200     2           2016-02-06 00:00:00.000
7    3      400     1           2016-02-07 00:00:00.000
tbl_横切面2

tid  empid  amount  creditdebit date
1    2      100     1           2016-01-07 00:00:00.000
2    2      200     1           2016-01-08 00:00:00.000
3    2      100     2           2016-01-09 00:00:00.000
4    2      700     1           2016-01-14 00:00:00.000
5    3      100     1           2016-02-04 00:00:00.000
6    3      200     2           2016-02-05 00:00:00.000
7    3      400     1           2016-02-08 00:00:00.000
这里1代表贷方,2代表借方

我想要像这样的输出

empid  empname   details            debitamount   creditamount   balance     Dr/Cr    date
2      jhon      opening Bal                                    400          Cr      
2      jhon      transection 1                    100           500          Cr      2016-01-06 00:00:00.000
2      jhon      transection 2                    100           600          Cr      2016-01-07 00:00:00.000
2      jhon      transection 1                    200           800          Cr      2016-01-08 00:00:00.000
2      jhon      transection 2                    200           1000         Cr      2016-01-08 00:00:00.000
2      jhon      transection 2        100                       900          Dr      2016-01-09 00:00:00.000
2      jhon      transection 1        100                       800          Dr      2016-01-11 00:00:00.000
2      jhon      transection 2                    700           1500         Cr      2016-01-14 00:00:00.000
2      jhon      transection 1                    700           2200         Cr      2016-01-15 00:00:00.000
3      smith     opening Bal                                    500          Cr      
3      smith     transection 1                    100           600          Cr      2016-02-03 00:00:00.000
3      smith     transection 2                    100           700          Cr      2016-02-04 00:00:00.000
3      smith     transection 2        200                       500          Dr      2016-02-05 00:00:00.000
3      smith     transection 1        200                       300          Dr      2016-02-06 00:00:00.000
3      smith     transection 1                    400           700          Cr      2016-02-07 00:00:00.000
3      smith     transection 2                    400           1100         Cr      2016-02-08 00:00:00.000

您可以通过以下方式完成:

select
  empid, sum(amount) over (partition by empid order by date) as balance, details
from (
  select
    empid, case creditdebit when 1 then amount else -amount end as amount, date, details
  from (
    select empid, openingbal as amount, 1 as creditdebit, '19000101' as date, 'opening Bal' as details
    from tbl_employee
    union all
    select empid, amount, creditdebit, date, 'transection 1'
    from tbl_transection1
    union all
    select empid, amount, creditdebit, date, 'transection 2'
    from tbl_transection2
  ) X
) Y
最内层的选择是从3个表中收集数据,下一个是计算+/-金额,最外层是计算余额


中的示例请花点时间重新阅读中的提问指南。这不是一个免费的代码编写服务或教程网站。你应该已经做了基础研究,介绍了你尝试过的代码,描述了它是如何不起作用的,以及预期的结果是什么。只是一个友好的提示,你可能想阅读这一页:因此你可以始终确保你的问题是容易回答的,并且尽可能清楚。请确保包括您为解决问题所做的任何努力,以及在尝试这些修复时发生的情况。另外,不要忘记显示代码和任何错误消息!