[英]T-SQL How do I rollup transactions based on if a field value exists?
I'm using SQL Server 2014. I have a list of financial transactions like in the below table:我正在使用 SQL Server 2014。我有一个如下表所示的金融交易列表:
PropertyID OccupancyNumber TransCode TransDesc VATCode Date Amount
1234 1111 GRNT Garage Rent GVAT 24/01/20 10.00
1234 1111 GVAT Garage VAT 24/01/20 2.00
1234 1111 RENT RENT 24/01/20 20.00
1234 1111 AMEN Amenity Charge AVAT 23/01/20 100.00
1234 1111 AVAT Amenity VAT 23/01/20 20.00
4567 2222 GRNT Garage Rent GVAT 24/01/20 15.00
4567 2222 GVAT Garage VAT 24/01/20 3.00
4567 2222 RENT RENT 24/01/20 150.00
4567 2222 AMEN Amenity Charge AVAT 23/01/20 200.00
4567 2222 AVAT Amenity VAT 23/01/20 40.00
However, I need to roll them up.但是,我需要将它们卷起来。 If there is a VATCode against a TransCode, I need to add it to the original TransCode and remove the VAT one.
如果有针对转码的 VATCode,我需要将其添加到原始转码并删除增值税。
Any VAT charges occur on the same day as the related Transaction.任何增值税费用与相关交易发生在同一天。 Also, this is per OccupancyNumber
此外,这是每个 OccupancyNumber
This is the desired outcome:这是想要的结果:
PropertyID OccupancyNumber TransCode TransDesc VATCode Date Amount
1234 1111 GRNT Garage Rent GVAT 24/01/20 12.00
1234 1111 RENT RENT 24/01/20 20.00
1234 1111 AMEN Amenity Charge AVAT 23/01/20 120.00
4567 2222 GRNT Garage Rent GVAT 24/01/20 18.00
4567 2222 RENT RENT 24/01/20 150.00
4567 2222 AMEN Amenity Charge AVAT 23/01/20 240.00
How would I achieve this?我将如何实现这一目标? Is a CTE the way to go?
CTE是要走的路吗?
Thanks.谢谢。
One idea would be to use a self referencing JOIN
and filter out the VAT transactions in the intial data set from Your Table.一种想法是使用自引用
JOIN
并从您的表中过滤掉初始数据集中的增值税交易。
This guesses the ON
clause, but should be enough to get you there if it's not quite right:这猜测
ON
子句,但如果它不太正确,应该足以让你到达那里:
WITH YourTable AS(
SELECT V.PropertyID,
V.OccupancyNumber,
V.TransCode,
V.TransDesc,
NULLIF(V.VATCode,'') AS VATCode,
TRY_CONVERT(date,V.[Date],3) AS [Date], --I hope [date] isn't a varchar, considering you display it in the format dd/MM/yy
V.Amount
FROM (VALUES(1234,1111,'GRNT','GarageRent','GVAT','24/01/20',10.00),
(1234,1111,'GVAT','GarageVAT','','24/01/20',2.00),
(1234,1111,'RENT','RENT','','24/01/20',20.00),
(1234,1111,'AMEN','AmenityCharge','AVAT','23/01/20',100.00),
(1234,1111,'AVAT','AmenityVAT','','23/01/20',20.00),
(4567,2222,'GRNT','GarageRent','GVAT','24/01/20',15.00),
(4567,2222,'GVAT','GarageVAT','','24/01/20',3.00),
(4567,2222,'RENT','RENT','','24/01/20',150.00),
(4567,2222,'AMEN','AmenityCharge','AVAT','23/01/20',200.00),
(4567,2222,'AVAT','AmenityVAT','','23/01/20',40.00))V(PropertyID,OccupancyNumber,TransCode,TransDesc,VATCode,[Date],Amount))
SELECT YT.PropertyID,
YT.OccupancyNumber,
YT.TransCode,
YT.TransDesc,
YT.VATCode,
YT.[Date],
YT.Amount + ISNULL(VAT.Amount,0) AS Amount
FROM YourTable YT
LEFT JOIN YourTable VAT ON YT.PropertyID = VAT.PropertyID
AND YT.VATCode = VAT.TransCode
WHERE YT.TransCode NOT LIKE '_VAT';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.