点击图片查看表格结构和问题http://i.stack.imgur.com/odWKL.png
SELECT B.ENTITYID,
B.BALANCEDATE,
B.BALANCE,
MIN(DATEDIFF(DAY,B.BALANCEDATE,C.STATUSDATE)) RECENT
FROM BALANCES B JOIN STATUS C ON B.ENTITYID = C.ENTITYID
GROUP BY B.ENTITYID, B.BALANCEDATE,B.BALANCE
HAVING B.ENTITYID =1
我已经尝试了以下但不能更进一步,因为更多嵌套选择在访问类似属性方面存在问题:
答案 0 :(得分:4)
见@ljh's Answer if you are working on SQL Server。以下解决方案适用于MySQL。由于MySQL不支持CTE
和Window Function
。
SET @entity_name = 'ABCD';
SELECT b.*, d.Status
FROM Entity a
INNER JOIN Balances b
ON a.EntityID = b.EntityID
LEFT JOIN
(
SELECT a.EntityID,
a.StatusDate StartDate,
b.StatusDate + Interval -1 DAY EndDate,
a.Status
FROM
(
SELECT b.*, @r1 := @r1 + 1 AS Row_number
FROM `Entity` a
INNER JOIN Status b
ON a.EntityID = b.EntityID
CROSS JOIN (SELECT @r1 := 0) rowCount
WHERE a.EntityName = @entity_name
ORDER BY b.Status ASC
) a
LEFT JOIN
(
SELECT b.*, @r2 := @r2 + 1 AS Row_number
FROM `Entity` a
INNER JOIN Status b
ON a.EntityID = b.EntityID
CROSS JOIN (SELECT @r2 := 1) rowCount
WHERE a.EntityName = @entity_name
ORDER BY b.Status ASC
) b ON a.Row_number = b.Row_number
) d
ON b.BalanceDate BETWEEN d.StartDate AND d.EndDate
WHERE a.EntityName = @entity_name
由于MySQL不支持窗口函数,例如ROW_NUMBER()
,因此下面的查询使用User Variable
为每条记录提供类似于ROW_NUMBER()
的行号然后将用于加入另一个子查询。
SELECT b.*, @r1 := @r1 + 1 AS Row_number
FROM `Entity` a
INNER JOIN Status b
ON a.EntityID = b.EntityID
CROSS JOIN (SELECT @r1 := 0) rowCount
WHERE a.EntityName = @entity_name
ORDER BY b.Status ASC
输出
╔══════════╦═════════════════════════════════╦════════╦════════════╗
║ ENTITYID ║ STATUSDATE ║ STATUS ║ ROW_NUMBER ║
╠══════════╬═════════════════════════════════╬════════╬════════════╣
║ 1 ║ May, 29 2010 00:00:00+0000 ║ A ║ 1 ║
║ 1 ║ April, 16 2010 00:00:00+0000 ║ B ║ 2 ║
║ 1 ║ April, 02 2010 00:00:00+0000 ║ C ║ 3 ║
║ 1 ║ February, 26 2010 00:00:00+0000 ║ D ║ 4 ║
╚══════════╩═════════════════════════════════╩════════╩════════════╝
为记录提供行号的主要目的是将其用于加入另一个子查询,以便我们可以为每个StartDate
获取EndDate
和Status
。这在SQL Server 2012
上很容易,因为它有一个名为LAG()的窗口函数
╔══════════╦═════════════════════════════════╦══════════════════════════════╦════════╗
║ ENTITYID ║ STARTDATE ║ ENDDATE ║ STATUS ║
╠══════════╬═════════════════════════════════╬══════════════════════════════╬════════╣
║ 1 ║ May, 29 2010 00:00:00+0000 ║ (null) ║ A ║
║ 1 ║ April, 16 2010 00:00:00+0000 ║ May, 28 2010 00:00:00+0000 ║ B ║
║ 1 ║ April, 02 2010 00:00:00+0000 ║ April, 15 2010 00:00:00+0000 ║ C ║
║ 1 ║ February, 26 2010 00:00:00+0000 ║ April, 01 2010 00:00:00+0000 ║ D ║
╚══════════╩═════════════════════════════════╩══════════════════════════════╩════════╝
状态范围一旦组织完毕。它现在是每个Balances
的 LookUp 状态的基础。
最终结果
╔══════════╦═════════════════════════════════╦═════════╦════════╗
║ ENTITYID ║ BALANCEDATE ║ BALANCE ║ STATUS ║
╠══════════╬═════════════════════════════════╬═════════╬════════╣
║ 1 ║ May, 01 2010 00:00:00+0000 ║ 100 ║ B ║
║ 1 ║ April, 01 2010 00:00:00+0000 ║ 50 ║ D ║
║ 1 ║ March, 01 2010 00:00:00+0000 ║ 75 ║ D ║
║ 1 ║ February, 01 2010 00:00:00+0000 ║ 85 ║ (null) ║
╚══════════╩═════════════════════════════════╩═════════╩════════╝
MySQL
中演示的上述查询可以使用TSQL
和使用LAG() (introduce in SQL Server 2012 only)的Common Table Expression
轻松转换为Window Function
WITH lookupTable
AS
(
SELECT EntityID,
StatusDate StartDate,
DATEADD(DAY, -1, LAG(StatusDate) OVER(PARTITION BY EntityID ORDER BY Status)) EndDate,
Status
FROM Status
)
SELECT b.*, d.Status
FROM Entity a
INNER JOIN Balances b
ON a.EntityID = b.EntityID
LEFT JOIN lookupTable d
ON b.BalanceDate BETWEEN d.StartDate AND d.EndDate AND
d.EntityID = a.EntityID
WHERE a.EntityName = 'ABCD'
输出
╔══════════╦═════════════════════════════════╦═════════╦════════╗
║ ENTITYID ║ BALANCEDATE ║ BALANCE ║ STATUS ║
╠══════════╬═════════════════════════════════╬═════════╬════════╣
║ 1 ║ May, 01 2010 00:00:00+0000 ║ 100 ║ B ║
║ 1 ║ April, 01 2010 00:00:00+0000 ║ 50 ║ D ║
║ 1 ║ March, 01 2010 00:00:00+0000 ║ 75 ║ D ║
║ 1 ║ February, 01 2010 00:00:00+0000 ║ 85 ║ (null) ║
╚══════════╩═════════════════════════════════╩═════════╩════════╝
答案 1 :(得分:2)
以SQL Server 2012为RDBMS为例。这是您需要的第一个查询。
这个答案使用了SQL CTE(公用表表达式),它可能不适用于其他RDBMS系统。
的 SQL FIDDLE DEMO 强>
解释说明:
1.首先加入[余额]和[状态]表,使用BalanceDate> StatusDate过滤掉结果,返回两个表中的所有列,这是因为稍后需要所有列。
2.使用[Entity]表加入Step .1的输出,使用EntityName过滤掉结果,仍然保留3个表中的所有列,当然不需要重复EntityID。
3.使用CTE保存连接
4.使用CTE保存应用于连接输出的等级
5.使用排名编号按BalanceDate过滤掉结果和订单
;with CTE_AfterJoin
as
(
select E.EntityID, E.EnityName, C.BalanceDate, C.Balance, C.StatusDate, C.status
from Entity E
left join (
select B.EntityID, B.BalanceDate, B.Balance,S.StatusDate, S.[Status]
from Balances B
left join [Status] S
on B.EntityID = S.EntityID and B.BalanceDate > S.StatusDate
) C
on E.EntityID = C.EntityID
where E.EnityName = 'ABCD'
),
CTE_afterRank
as
(
select EnityName, BalanceDate, Balance,
rank() over (partition by BalanceDate order by StatusDate desc) as Rn, Status
from CTE_AfterJoin
)
select EnityName, BalanceDate, Balance, Status
from CTE_afterRank
where Rn = 1
order by BalanceDate desc
答案 2 :(得分:1)
同样在SQLServer2005 +中,您可以使用APPLY()运算符选项。
APPLY运算符允许您连接两个表表达式。对于左表表达式中的每一行,每次都处理右表表达式。最终结果集包含左表表达式中的所有选定列,后跟右表表达式的所有列。对于右表表达式中没有对应匹配的那些行,对于右表表达式,它包含NULL值。
SELECT e.EntityName, b.BalanceDate AS Date, b.Balance, o.Status
FROM Entity e JOIN Balances b ON e.EntityID = b.EntityID
OUTER APPLY (
SELECT TOP 1 s.Status AS Status
FROM Status s
WHERE b.EntityID = s.EntityID
AND s.StatusDate < b.BalanceDate
ORDER BY s.StatusDate DESC
) o
WHERE e.EntityName = 'ABCD'
为了提高性能(强制INDEX SEEK操作),请将此索引与INCLUDE子句一起使用.INCLUDE子句在最低/叶级别添加数据,而不是在索引树中。这使得索引更小,因为它不是树的一部分
CREATE INDEX x ON Status(StatusDate) INCLUDE(EntityID, Status)
CREATE INDEX x ON Entity(EntityName) INCLUDE(EntityID)
CREATE INDEX x ON Balances(EntityID, BalanceDate, Balance)
SQLFiddle上的演示