(另一个)ACCESS

时间:2015-09-09 15:06:54

标签: ms-access

我想要来自我的数据'的摘要数据。所有公司的表格'公司'表包括数据中没有记录的空白行。

如果我在嵌套的SELECT子句中总结数据(或在存储的查询中,我从数据表中得不到任何内容。例如

这是子选择

SELECT    transco,
          sum(m1) AS Jan15,
FROM data
WHERE (QVmeasure = 'Vol') 
GROUP BY QVmeasure, transco

输出:

transco Jan15
0292    154373665
1419    134915098

如果我在子选择中使用它,如下

SELECT  c.SAP_Code, 
        Jan15
FROM Companies AS c
LEFT JOIN (
       SELECT transco,
              sum(m1) as Jan15
       FROM data
       WHERE (QVmeasure = 'Vol') 
       GROUP BY QVmeasure, transco)
AS d
ON c.SAP_Code = d.transco

我明白了:

SAP_Code    Jan15
0292    
1419

我可以通过临时表获得正确的结果:

SELECT sum(m1) as Jan15,
    transco
INTO Temp_Table
    FROM data
    WHERE (QVmeasure = 'Vol') 
    GROUP BY QVmeasure, transco

接着是

Select c.SAP_code, 
       jan15
FROM companies AS c
LEFT JOIN Temp_Table as i
ON (c.SAP_Code = i.transco) 

,并提供:

SAP_code    jan15
0292    154373665
1419    134915098
1423

但如果我使用临时表,我将不得不创建宏,我希望用户只能运行查询。

以下适用于这个简单的案例,但我不能在其他情况下应用它:

SELECT  c.SAP_Code,
        sum(m1) AS Jan15
FROM Companies AS c
LEFT JOIN data as d
ON c.SAP_Code = d.transco
WHERE (d.QVmeasure = 'Vol') OR (d.QVmeasure is null)
GROUP BY d.QVmeasure,c.sap_code

我的子选择语法是否有问题,或者是ACCESS(2013)

TIA

1 个答案:

答案 0 :(得分:0)

你可以像这样嵌套子查询:

SELECT  c.SAP_Code, 
    (SELECT sum(d.m1)
    FROM data AS d
    WHERE d.QVmeasure = 'Vol' AND c.SAP_Code = d.transco
    ) AS [Jan15]
FROM Companies AS c