访问查询 - 总结月份到目前为止

时间:2017-10-18 21:49:00

标签: sql ms-access

我目前设置了交叉表查询,以便在2017年按月返还金额。这会逐个显示每个位置的月份总数。我想总结年初至今(第1-7个月),并且只返回该位置的一个值。我出于某种原因无法做到这一点。

TRANSFORM Sum(Location.Amt) AS SumOfAmt
SELECT 
    Location.Div
  , Location.Store
  , Location.Year
  , Location.Month
FROM 
  [Location Name] 
  INNER JOIN Location 
    ON [Location Name].Line = Location.Line
WHERE 
  (
    Location.Store     <> 100 
    AND Location.Year   = 2017 
    AND Location.Month  < 8 
    AND Location.Line   < 11
GROUP BY 
  Location.Div
  , Location.Store
  , Location.Year
  , Location.Month
ORDER BY 
  Location.Div
  , Location.Store
  , [Location Name].LineDesc
PIVOT [Location Name].LineDesc
;

1 个答案:

答案 0 :(得分:1)

selectgroup by条款中,您列出了YearMonth,这意味着它将汇总这些列中的每个值。如果您不想要这种行为,则应从查询中删除这些列。

TRANSFORM Sum(Location.Amt) AS SumOfAmt
SELECT 
    Location.Div
  , Location.Store
FROM 
  [Location Name] 
  INNER JOIN Location 
    ON [Location Name].Line = Location.Line
WHERE 
  (
    Location.Store     <> 100 
    AND Location.Year   = 2017 
    AND Location.Month  < 8 
    AND Location.Line   < 11
GROUP BY 
  Location.Div
  , Location.Store
ORDER BY 
  Location.Div
  , Location.Store
  , [Location Name].LineDesc
PIVOT [Location Name].LineDesc
;