我是初学者,我只是自己学习,所以请原谅我是否能以程序员的方式表达我想说的话! 我正在尝试开发可以执行此操作的简单业务应用程序:
- 注册员工或客户。
- 可以存储员工在支付工资之前可以提前支付的费用或金钱(这可以多次,或者可以 零)
- 可以存储付款(给予员工的工资) - 也可以多次付款
- 它可以计算所有费用,计算所有付款,然后找出它们的差额,因此将支付剩余的余额!
醇>
{{1}}
附上我所期望的图片和我愿意的图片 Expected Result
答案 0 :(得分:0)
首先祝贺你尽可能地拥有。坏消息是t-sql不是一个报告工具 - 你需要寻找其他地方,如果你想要漂亮的输出(SSRS,Crystal报告,excel等等),你需要在t-sql中接近你所需的输出有点创意,例如: -
/*
drop table customer
drop table expense
drop table TblLoad
CREATE TABLE Customer
( CustomerID INT --IDENTITY (1000,1)
,FirstName VARCHAR(40)
,RegDate DATETIME --DEFAULT GETDATE()
,Phone VARCHAR(10)
);
INSERT INTO CUSTOMER
( CustomerID
,FirstName
,RegDate
,Phone )
VALUES
(1001,'Emp1',cast('2015/10/20 00:00:00.000' as datetime), null),
(1002,'Emp2',cast('2015/10/22 00:00:00.000' as datetime), null),
(1003,'Emp3',cast('2015/10/25 00:00:00.000' as datetime), null)
CREATE TABLE Expense
( ExpenseID INT --IDENTITY (1000,1)
,CustomerID INT NOT NULL
,ExpDate DATETIME DEFAULT GETDATE()
,Amount MONEY
,Comment VARCHAR (100)
);
truncate table expense
insert into expense
( ExpenseID
,CustomerID
,ExpDate
,Amount
--,Comment
)
Values
(1001,1001,cast('2015/10/21' as datetime),100),
(1002,1001,cast('2015/11/22' as datetime),20),
(1003,1001,cast('2015/12/25' as datetime),80),
(1004,1002,cast('2015/11/21' as datetime),100),
(1005,1002,cast('2015/12/25' as datetime),200),
(1006,1003,cast('2015/11/25' as datetime),300)
CREATE TABLE TblLoad
( LoadID INT --IDENTITY (1000,1) --PRIMARY KEY
,CustomerID INT NOT NULL
,DepDate DATETIME DEFAULT GETDATE()
,Amount MONEY
,FromTo VARCHAR (100)
);
truncate table tblload
insert into tblload
( loadID
,CustomerID
,depDate
,Amount
--,Comment
)
Values
(1001,1001,cast('2015/10/21' as datetime),1000),
(1002,1001,cast('2015/11/21' as datetime),200),
(1003,1001,cast('2015/12/24' as datetime),800),
(1004,1002,cast('2015/11/21' as datetime),2000),
(1005,1002,cast('2015/12/25' as datetime),500),
(1006,1003,cast('2015/11/24' as datetime),4000)
*/
select
--case
--when right(rtrim(cast(yyyymm as char(8))) ,2 ) = '13' then char(30)
--else cast(yyyymm as char(8))
--end
case
when right(rtrim(cast(yyyymm as char(8))) ,2 ) = '13' then char(30)
else cast(customerid as char(4))
end
,case
when right(rtrim(cast(yyyymm as char(8))) ,2 ) = '13' then 'Total'
else firstname
end
,sum(amountexp) amuntexp
,sum(amountload) amountload
,sum(amountload) - sum(amountexp) finalpay
from
(
select year(e.expdate) * 100 + month(e.expdate) yyyymm,
C.CustomerID, c.FirstName, sum(e.amount) as AmountExp , 0 as AmountLoad , 0 as FinalPay
FROM Customer c
INNER JOIN Expense E ON C.CustomerID = e.CustomerID
group by year(e.expdate) , month(e.expdate),
C.CustomerID, c.FirstName
union
select year(e.expdate) * 100 + 13 ,
C.CustomerID, c.FirstName, sum(e.amount) as AmountExp , 0 as AmountLoad , 0 as FinalPay
FROM Customer c
INNER JOIN Expense E ON C.CustomerID = e.CustomerID
group by year(e.expdate) * 100 + 13,
C.CustomerID, c.FirstName
union
select year(l.depdate) * 100 + month(l.depdate) yyyymm,
C.CustomerID, c.FirstName, 0 asAmountExp , sum(l.amount) as AmountLoad , 0 as FinalPay
FROM Customer c
INNER JOIN tblload l ON C.CustomerID = l.CustomerID
group by year(l.depdate) , month(l.depdate),
C.CustomerID, c.FirstName
union
select year(l.depdate) * 100 + 13 yyyymm,
C.CustomerID, c.FirstName, 0 asAmountExp , sum(l.amount) as AmountLoad , 0 as FinalPay
FROM Customer c
INNER JOIN tblload l ON C.CustomerID = l.CustomerID
group by year(l.depdate) * 100 + 13,
C.CustomerID, c.FirstName
) s
group by s.CustomerID,s.firstname,yyyymm
order by s.CustomerID,s.firstname,yyyymm