【发布时间】:2016-09-23 12:41:45
【问题描述】:
我有一个程序的流动账户余额,它根据用户的交易进行更新。从这里我想加入一个表,其中包含每个用户的行和程序历史的每个日期,这样我就可以了解余额的分布如何随时间变化。
交易余额数据Ex.1(这是日终余额):
userId transDate userBalance
33782 2016-05-13 233
33783 2016-05-13 143
30070 2016-05-20 572
30071 2016-05-20 888
例如。 2的日历表
userID balanceDate
33782 2016-05-13
33783 2016-05-13
30070 2016-05-13
30071 2016-05-13
33782 2016-05-20
33783 2016-05-20
30070 2016-05-20
30071 2016-05-20
想要的结果
userId balanceDate userBalance
33782 2016-05-13 233
33783 2016-05-13 143
30070 2016-05-13 0
30071 2016-05-13 0
33782 2016-05-20 233
33783 2016-05-20 143
30070 2016-05-20 572
30071 2016-05-20 888
基本上,我需要以某种方式将事务表连接到 calendarBalance 表,并让 userBalance 字段返回最大记录记录,其中 transDate 小于或等于 balanceDate 否则为 0。 由于数据库有数百万笔交易,我所做的每一次尝试都会超时。 我正在使用 SQL Server 2012。
这是一个超时的尝试:
SELECT d.balanceDate ,b.userId ,b.userBalance
FROM #calendar d ,#userBalance b
WHERE d.balanceDate >= b.transDate
AND b.transDate >= ALL (
SELECT b1.transDate
FROM #userBalance b1
WHERE b.userId = b1.userId
AND d.transDate >= b1.transDate
)
ORDER BY d.balanceDate ,b.userId
【问题讨论】:
-
一个用户一天内有多笔交易怎么办?
-
交易表是日期的总和
-
这是一个很好的起点。 spaghettidba.com/2015/04/24/…
-
您能否发布一个超时的尝试,以便我们帮助调试它?
-
@TabAlleman SELECT d.balanceDate ,b.userId ,b.userBalance FROM #calendar d ,#userBalance b WHERE d.balanceDate >= b.transDate AND b.transDate >= ALL ( SELECT b1. transDate FROM #userBalance b1 WHERE b.userId = b1.userId AND d.transDate >= b1.transDate ) ORDER BY d.balanceDate ,b.userId
标签: sql-server