我有两个表BMReports_FPN_Curves和BMReports_BOA_Curves ,每个表都由名称、日期时间、期间和值组成,例如:
BM_UNIT_NAME RunDate Period FPN (or BOA)
T_DRAXX-1 2010-12-01 00:03:00 1 497
RunDate 字段增加一分钟(这 c.1440 记录每天)和周期为 1-48。在BMReports_FPN_Curves我有每个时间段的完整数据集,BMReports_BOA_Curves包含将替换这些基础值的值。
通常存在重复的 BOA 值,并且Access SQL 语句中的嵌套 IIF 语句包含一个规则,用于在任何时间点选择 FPN、最大 BOA 值或最小 BOA 值之一。该规则规定:
1) 如果没有 BOA 值,使用 FPN 值
2) 如果有 BOA 值并且小于 FPN,找到并使用 Min BOA 值
3) 如果有 BOA 值并且大于FPN,查找并使用最大 BOA 值
Access SQL 查询完美运行,如下所示:
SELECT
dbo_BMReports_FPN_Curves.BM_Unit_Name,
dbo_BMReports_FPN_Curves.RunDate,
dbo_BMReports_FPN_Curves.Period,
dbo_BMReports_FPN_Curves.PN_Level,
IIf(IIf(Min([dbo_BMReports_BOA_Curves]![PN_Level]) <[dbo_BMReports_FPN_Curves]![PN_Level],Min([dbo_BMReports_BOA_Curves]! [PN_Level]),Max([dbo_BMReports_BOA_Curves]![PN_Level])) Is Null, [dbo_BMReports_FPN_Curves]![PN_Level],
IIf(Min([dbo_BMReports_BOA_Curves]![PN_Level])<[dbo_BMReports_FPN_Curves]! [PN_Level],Min([dbo_BMReports_BOA_Curves]! [PN_Level]),Max([dbo_BMReports_BOA_Curves]![PN_Level]))) AS BOA
FROM dbo_BMReports_FPN_Curves LEFT JOIN dbo_BMReports_BOA_Curves ON (dbo_BMReports_FPN_Curves.RunDate = dbo_BMReports_BOA_Curves.RunDate) AND (dbo_BMReports_FPN_Curves.BM_Unit_Name = dbo_BMReports_BOA_Curves.BM_Unit_Name)
GROUP BY dbo_BMReports_FPN_Curves.BM_Unit_Name, dbo_BMReports_FPN_Curves.RunDate, dbo_BMReports_FPN_Curves.Period, dbo_BMReports_FPN_Curves.PN_Level
HAVING (((dbo_BMReports_FPN_Curves.BM_Unit_Name)='T_DRAXX-1'));
我已经在 T-SQL 中重写了大部分查询(查询相同的 SQL Server 数据源),并且 LEFT JOIN、GROUP BY 和 HAVING 元素都可以正常工作,但是我在 CASE WHEN 替换 IFF 时遇到了困难如果有些人有一点空闲时间,我会非常感激。
目前的 SQL 查询:
SELECT
BMReports_FPN_Curves.BM_Unit_Name,
BMReports_FPN_Curves.RunDate,
BMReports_FPN_Curves.Period,
AVG(BMReports_FPN_Curves.PN_Level) AS FPN,
CASE
WHEN BMReports_BOA_Curves.PN_Level IS NULL THEN AVG(BMReports_FPN_Curves.PN_Level)
WHEN MIN(BMReports_BOA_Curves.PN_Level) IS < AVG(BMReports_FPN_Curves.PN_Level) THEN MIN(BMReports_BOA_Curves.PN_Level)
ELSE MAX(BMReports_BOA_Curves.PN_Level)
END AS BOA
FROM BMReports_FPN_Curves
LEFT JOIN BMReports_BOA_Curves ON BMReports_FPN_Curves.BM_Unit_Name = BMReports_BOA_Curves.BM_Unit_Name
AND BMReports_FPN_Curves.RunDate = BMReports_BOA_Curves.RunDate
GROUP BY BMReports_FPN_Curves.BM_Unit_Name, BMReports_FPN_Curves.RunDate, BMReports_FPN_Curves.Period
HAVING BMReports_FPN_Curves.BM_Unit_Name = 'T_DRAXX-1'
ORDER BY BMReports_FPN_Curves.BM_Unit_Name, BMReports_FPN_Curves.RunDate, BMReports_FPN_Curves.Period