生成总和查询

时间:2014-08-26 09:55:54

标签: sql ms-access sum

我正在使用MS Access 2007.我尝试使用以下数据生成查询。

SP      Location     Manager
100     On-site      Jones
200     On-site      Driver
200     Off-site     Driver
300     On-site      Jones
150     Off-site     Jones
200     On-site      Jones
100     Off-site     Driver

我想在每个位置为每位经理汇总SP。所需的输出应为:

Jones is On-site    Jones is Off-site    Driver is On-site    Driver is Off-site
      600                 150                  200                   300

我该怎么做?

我可以为“琼斯现场”提供查询。但是一旦我添加了其他的(即#琼斯不在网站上等等),它就无法工作。

这是我用于'琼斯的现场代码'

SELECT Sum([Selling Price]) AS Expr1
FROM [Table]
WHERE ((([Table].[Location])='Off-Site') AND (([Table].[Manager])='Jones'));

2 个答案:

答案 0 :(得分:2)

这应该有效:

SELECT SUM(SP), Manager & " is " & Locaton
FROM table
GROUP BY Manager & " is " & Locaton

答案 1 :(得分:0)

如果您想在所需的输出中进行查询,请尝试以下方法:

SELECT 
    [Jones is On-site], 
    [Jones is Off-site], 
    [Driver is On-site], 
    [Driver is Off-site]
FROM
(
    SELECT SUM(SP) AS SP, Manager + ' is ' + Location as Col1
    FROM Test
    GROUP BY Manager + ' is ' + Location
) XXX
PIVOT
(
   SUM(SP) FOR Col1 IN (
        [Jones is On-site], 
        [Jones is Off-site], 
        [Driver is On-site], 
        [Driver is Off-site]
   )
) AS ResultTable

请参阅演示here