我正在为检查摘要报告提取一组信息,我遇到了一个问题,我需要显示检查的完整历史记录。特别是,比方说我发了一张支票。此检查可能是无效的。如果它在我们的程序中是空的,它只会将检查的状态更改为Void。但是,我需要能够创建一个新的行条目并相应地操作数据。
以下是我正在使用的查询。
SELECT DISTINCT
O.GFNo AS 'FileNumber'
,CASE
WHEN C.VoidDate IS NOT NULL
THEN C.VoidDate
WHEN C.VoidDate IS NULL
THEN C.CheckDate
ELSE NULL
END AS 'Date'
-- Check type descriptions.
,CASE
WHEN C.CheckStatus NOT IN (3,4) AND C.VoidDate IS NULL
THEN CASE
WHEN C.IsTransfer = 0 AND C.IsWire = 0 AND C.IsEFER = 0
THEN 'Check'
WHEN C.IsTransfer = 1
THEN 'Transfer'
WHEN C.IsWire = 1
THEN 'Wire'
WHEN C.IsEFER = 1
THEN 'EFER'
END
-- Stopped checks.
WHEN C.CheckStatus = 3 AND C.VoidDate IS NOT NULL
THEN CASE
WHEN C.IsTransfer = 0 AND C.IsWire = 0 AND C.IsEFER = 0
THEN 'Stopped Check'
WHEN C.IsTransfer = 1
THEN 'Stopped Transfer'
WHEN C.IsWire = 1
THEN 'Stopped Wire'
WHEN C.IsEFER = 1
THEN 'Stopped EFER'
END
-- Voided checks.
WHEN C.CheckStatus = 4 AND C.VoidDate IS NOT NULL
THEN CASE
WHEN C.IsTransfer = 0 AND C.IsWire = 0 AND C.IsEFER = 0
THEN 'Void Check'
WHEN C.IsTransfer = 1
THEN 'Void Transfer'
WHEN C.IsWire = 1
THEN 'Void Wire'
WHEN C.IsEFER = 1
THEN 'Void EFER'
END
END AS 'Description'
-- Check number.
,CASE
WHEN C.IsTransfer = 1
THEN 'XFer'
WHEN C.IsConsolidated = 1 AND C.ConsolidatedCheckID > 0
THEN 'Consolidated'
WHEN C.IsEFER = 1
THEN 'EFER-' + CONVERT(VARCHAR(MAX), C.WireNumber)
WHEN C.IsWire = 1
THEN 'W-' + CONVERT(VARCHAR(MAX), C.WireNumber)
WHEN C.IsCashiersCheck = 1
THEN 'C-' + CONVERT(VARCHAR(MAX), C.CheckNumber)
ELSE CONVERT(VARCHAR(MAX), C.CheckNumber)
END AS 'CheckNumber'
,CONVERT(VARCHAR(MAX), 0) AS 'Receipts'
,CASE
WHEN C.CheckStatus = 4 AND C.VoidDate IS NOT NULL
THEN ISNULL(C.Amount, 0)
ELSE CAST(0.0 - C.Amount AS MONEY)
END AS 'Disbursements'
FROM
Checks C
LEFT JOIN dbo.Orders O
ON C.OrdersID = O.OrdersID
LEFT JOIN CheckSessionDetail CSD
ON C.ChecksID = CSD.ChecksID
AND CSD.ActionType IN (6,9)
LEFT JOIN CheckSession CS
ON CSD.CheckSessionID = CS.CheckSessionID
WHERE
O.OrdersID = @OrdersID
AND C.CheckDate IS NOT NULL
这就是它的回归。如果检查为空,则只需将描述更改为“无效”。
| FileNumber | Date | Description | CheckNumber | Receipts | Disbursements |
| 1 | 8/8/2014 | Check | 1001 | 0 | $7500 |
| 1 | 8/8/2014 | Check | 1002 | 0 | $5000 |
| 1 | 8/8/2014 | Void | 1003 | 0 | $1000 |
这就是我需要它返回的方式:
| FileNumber | Date | Description | CheckNumber | Receipts | Disbursements |
| 1 | 8/8/2014 | Check | 1001 | 0 | $7500 |
| 1 | 8/8/2014 | Check | 1002 | 0 | $5000 |
| 1 | 8/8/2014 | Check | 1003 | 0 | $1000 |
| 1 | 8/8/2014 | Void | 1003 | $1000 | $0 |
请注意,如果检查无效,它会保留初始检查条目,但也会创建一个Void条目并将美元金额放入“收据”列。它也会保持相同的CheckNumber。
基本上,是否可以说出以下内容:如果Check的状态为Void,请创建一个新的行条目?我认为临时桌子可能会起作用,但我并没有对它们进行太多研究。我一整天都在试图弄明白这一点,所以你们所提供的任何帮助都会给你带来巨大的帮助。
答案 0 :(得分:1)
当然,而不是:
FROM Checks C
LEFT JOIN dbo.Orders O ...
尝试:
SELECT DISTINCT
O.GFNo AS 'FileNumber'
,c.Date
...
FROM ( select Date = CheckDate, typ = 'Check', * from Checks
UNION ALL
select Date = VoidDate, typ = 'Void', * from Checks WHERE VoidDate is not null ) c
LEFT JOIN dbo.Orders O ...
并在typ
,Description
等的案例陈述中使用Receipts
。