/****** Script for SelectTopNRows command from SSMS ******/
SELECT DISTINCT
(
SELECT ForeignCurrencyBal
FROM [InsightWarehouse].[dbo].[v_Account] AS subA
WHERE BusinessDate=['2019-02-01','2019-01-31',etc..]
AND
subA.AccountNum = STMT.ACCOUNT_NUMBER
) AS Balance,
ACCOUNT_NUMBER,
CASE WHEN STMT.CRF_TYPE='CREDIT'
THEN STMT.AMOUNT_LCY
ELSE NULL
END as 'CreditAmount',
CASE WHEN STMT.CRF_TYPE='DEBIT'
THEN STMT.AMOUNT_LCY
ELSE NULL
END as 'DebitAmount',
STMT.BOOKING_DATE,
STMT.VALUE_DATE,
CRF_TYPE
FROM [InsightLanding].[dbo].[v_STMT_ENTRY] AS STMT
LEFT JOIN [InsightWarehouse].[dbo].[v_Account] AS A
ON a.AccountNum = STMT.ACCOUNT_NUMBER
WHERE STMT.MIS_DATE='2019-02-01'
AND
STMT.ACCOUNT_NUMBER='14498478'
在这一行
WHERE BusinessDate=['2019-02-01','2019-01-31',etc..]
,这个列表可以通过子查询得到。
我想把条件放在前一天,这意味着此列表中的第二项。像这样的
BusinessDate='2019-01-31'
是这样的:
UPD:请注意您的子查询必须返回一行,所以也许您应该使用
TOP (1)
或一些聚合函数来避免可能的错误。或者
你可以试试
WHERE BusinessDate = CAST(GETDATE() -1 AS date)
,这将需要前一天。例如
2019-01-31
it will take2019-01-30
和2019-03-01
it will take2019-02-28
等等。此外,如果您只想包含工作日(周一至周五),请添加另一个条件
AND DATEPART(dw, GETDATE()-1) NOT IN (7,1)