1

我的查询确实返回任何记录。根据我的编写方式,它不返回任何记录或所有记录,尽管我没有刚刚返回所有内容的代码。

我需要从两个具有实际记录的源中提取数据,第三个表具有项目范围的信息,而不是特定于任何记录。我需要过滤掉大于 Miles_Budgeted 变量的记录。

这不会返回任何记录,尽管如果我将 param.Miles_Budgeted 替换为数值(例如 1000),它会过滤到所需的记录。

SELECT
    a.sort_id,
    a.l1l2,
    a.rtot_pct_oftot_miles,
    b.sumofeq,
    b.c_per_mile,
    b.sumofo_total,
    a.cpminrmd,
    a.RunTotMiles,
    param.Miles_Budgeted

FROM
    (SELECT (p.Budget_Cost_Targ / p.Project_Cost_Per_Mi) AS Miles_Budgeted FROM Tbl_Project_Parameters as p) AS param,
    qry_par_l2_by_cpermi AS a 
    INNER JOIN
        qry_l2 AS b 
        ON a.l1l2 = b.l1l2 
WHERE
 ((a.RunTotMiles) <= 
        (Param.Miles_Budgeted
                   )
)

ORDER BY
    a.sort_id;

此查询变体未运行(FROM 子句中的语法错误)

SELECT     
           a.sort_id, 
           a.l1l2, 
           a.rtot_pct_oftot_miles, 
           b.sumofeq, 
           b.c_per_mile, 
           b.sumofo_total, 
           a.cpminrmd, 
           a.runtotmiles, 
           param.miles_budgeted 
FROM       ( 
           ( 
                  SELECT (p.budget_cost_targ / p.project_cost_per_mi) AS miles_budgeted 
                  FROM   tbl_project_parameters                       AS p ) AS param 
INNER JOIN qry_par_l2_by_cpermi AS a ) 
INNER JOIN qry_l2 AS b 
ON         a.l1l2 = b.l1l2 
AND        ( 
                      a.runtotmiles) <= ( param.miles_budgeted ) 
ORDER BY   a.sort_id;

这也不会返回任何记录:

SELECT
    a.sort_id,
    a.l1l2,
    a.rtot_pct_oftot_miles,
    b.sumofeq,
    b.c_per_mile,
    b.sumofo_total,
    a.RunTotMiles,
    a.cpminrmd 
FROM
    qry_par_l2_by_cmipermi AS a 
    INNER JOIN
        qry_l2 AS b 
        ON a.l1l2 = b.l1l2 
WHERE
    (
((a.RunTotMiles) <= 
        (
            SELECT
(p.Budget_Cost_Targ / p.Project_Cost_Per_Mi) AS Budgeted_Miles 
            FROM
                Tbl_Project_Parameters AS p 
        )
) 
    )
ORDER BY
    a.sort_id;

再次,如果

SELECT
(p.Budget_Cost_Targ / p.Project_Cost_Per_Mi) AS Budgeted_Miles 
            FROM
                Tbl_Project_Parameters AS p 

替换为数值,则查询返回正确的记录。我尝试用 val() 或 Format(,"Standard") 包围 subq 或字段,但这并不能解决问题;仅包含相关代码的单独查询将正确的 Budgeted_Miles 返回为 1000。

任何想法表示赞赏。

4

1 回答 1

0

您是否尝试过将该子查询限制为仅返回一条记录?SELECT我知道当您尝试将 a 的结果与单个值进行比较时,某些版本的 SQL 不喜欢。

我相信 MS Access 的语法会使用“TOP”:

SELECT TOP 1 
(p.Budget_Cost_Targ / p.Project_Cost_Per_Mi) AS Budgeted_Miles 
FROM
Tbl_Project_Parameters AS p 
于 2018-07-09T17:42:22.487 回答