我有一个查询(qry_prob
),显示以下结果:
+------+-------------+--------------+
| PK_G | ID1_Prob_Ov | ID1_Prob_Sur |
+------+-------------+--------------+
| 1044 | 47.17% | 72.17% |
| 1045 | 46.93% | 53.79% |
| 1046 | 49.57% | 50.66% |
| 1047 | 59.89% | 66.54% |
+------+-------------+--------------+
由此,我想创建另一个查询,使用“ Prob”字段并使用参考表(tbl_bldpct
)为每个记录创建一个混合百分比,如下所示:
+----+------------+---------+
| ID | Bld_Input | Bld_Pct |
+----+------------+---------+
| 1 | % Prob Ov | 15.00% |
| 2 | % Prob Sur | 85.00% |
+----+------------+---------+
结果应如下所示:
+------+--------------+
| PK_G | ID1_prob_bld |
+------+--------------+
| 1044 | 68.42% |
| 1045 | 52.76% |
| 1046 | 50.50% |
| 1047 | 65.54% |
+------+--------------+
我使用了以下SQL:SELECT qry_prob.PK_G, [qry_prob].[ID1_Prob_Ov]*(SELECT Bld_Pct FROM tbl_bldpct WHERE ID = 1)+[qry_prob].[ID1_Prob_Sur]*(SELECT Bld_Pct FROM tbl_bldpct WHERE ID = 2) AS ID1_prob_bld
FROM qry_prob, tbl_bldpct;
但是,我得到的重复行如下:
+------+--------------+
| PK_G | ID1_prob_bld |
+------+--------------+
| 1044 | 68.42% |
| 1044 | 68.42% |
| 1045 | 52.76% |
| 1045 | 52.76% |
| 1046 | 50.50% |
| 1046 | 50.50% |
| 1047 | 65.54% |
| 1047 | 65.54% |
+------+--------------+
如果我在参考表中添加另一行,则会得到另一行重复项,因此我可以看到问题出在从该表中选择记录,但我不知道如何解决此问题... >
答案 0 :(得分:1)
尝试不同:
SELECT DISTINCT
qry_prob.PK_G,
[qry_prob].[ID1_Prob_Ov]*(SELECT Bld_Pct FROM tbl_bldpct WHERE ID = 1)+[qry_prob].[ID1_Prob_Sur]*(SELECT Bld_Pct FROM tbl_bldpct WHERE ID = 2) AS ID1_prob_bld
FROM
qry_prob,
tbl_bldpct;
或者只是删除表格:
SELECT
qry_prob.PK_G,
[qry_prob].[ID1_Prob_Ov]*(SELECT Bld_Pct FROM tbl_bldpct WHERE ID = 1) +
[qry_prob].[ID1_Prob_Sur]*(SELECT Bld_Pct FROM tbl_bldpct WHERE ID = 2) AS ID1_prob_bld
FROM
qry_prob;
答案 1 :(得分:1)
您可以两次加入tbl_bldpct
。不幸的是,MS Access的JOIN
语法并不十分灵活,因此您需要在WHERE
子句中使用交叉联接和过滤功能:
select (qp.ID1_Prob_Ov * bp1.Bld_Pct + qp.ID1_Prob_Sur * bp2.Bld_Pct) as ID1_prob_bld
from qry_prob as qp,
tbl_bldpct as bp1,
tbl_bldpct as bp2
where bp1.id = 1 and bp2.id = 2;
您还可以预聚合tbl_bldpct
:
select (qp.ID1_Prob_oc * bp.Bld_Pct_1 + qp.ID1_Prob * bp.Bld_Pct2) as ID1_prob_bld
from qry_prob as qp,
(select max(iif(id = 1, Bld_Pct, null)) as Bld_Pct_1,
max(iif(id = 1, Bld_Pct, null)) as Bld_Pct_2
from tbl_bldpct
) bp;
答案 2 :(得分:0)
这是因为您正在执行两个表CROSS JOIN
中的qry_prob, tbl_bldpct
。如果仅在FROM
子句中指定用逗号分隔的表而没有提及特定的JOIN
条件,则会自动发生这种情况:
FROM qry_prob, tbl_bldpct
由于在您的SELECT
查询中不需要第二张表,因此您可以安全地忽略它并摆脱CROSS JOIN
(这将返回第二张表的记录的笛卡尔积)。 / p>
SELECT qry_prob.PK_G,
[qry_prob].[ID1_Prob_Ov]*(SELECT Bld_Pct FROM tbl_bldpct WHERE ID = 1)
+[qry_prob].[ID1_Prob_Sur]*(SELECT Bld_Pct FROM tbl_bldpct WHERE ID = 2) AS ID1_prob_bld
FROM qry_prob
答案 3 :(得分:0)
如果我没记错的话
SELECT Table2.PK_G, [Bld_Input]*0.15+[ Bld_Pct]*0.85 AS ID1_prob_bld
FROM qry_prob;
基于 Bld_Pct
SELECT qry_prob.PK_G, qry_prob.Bld_Input* (SELECT Bld_Pct FROM tbl_bldpct WHERE ID =1) + qry_prob.Bld_Pct* (SELECT Bld_Pct FROM tbl_bldpct WHERE ID =2)
FROM qry_prob;