How to get value as header field in pivot table?

时间:2018-09-18 20:25:16

标签: ms-access

I've been looking for this answer for hours without finding anything at all on this subject.

I'm trying to get a pivot table from the "Sells" table that displays : - as header fields, the name of each seller in my shop - as rows, each month of the year - as data the sum of their sells for each month

The only issue I have is that the headers are populated with ID instead of the name of the sellers.

For example, instead of showing this :

Month    | Steve | Joe
January  | 5000$ | 600$
February | 400$  | 400$

It keeps showing the ID related to each seller:

Month    |   1   |   2
January  | 5000$ | 600$
February | 400$  | 400$

Here is my query :

TRANSFORM Sum(Sells.Ca) AS [Monthly sells]
SELECT DISTINCTROW Format$(Sells.DateSold,'mm - mmmm') AS Month
FROM Sells
GROUP BY Sells.DateSold
PIVOT Sells.Seller

Thank you very much for your help and your time.

EDIT: As @WolfgangKais mentionned in the comments, I forgot to mention that the Seller field is a lookup field, that's why it only shows the first value of the lookup field, hence the ID and not the name.

1 个答案:

答案 0 :(得分:0)

如@ June7和@Wolfgang Kais所述,我必须在查询中包括卖方表,以便获得每个卖方的姓名而不是其ID,非常感谢。

这是通过:

  1. 构建一个临时表,其中包含由卖方名称,出售金额和出售日期填充的行:

    SELECT Sellers.Name AS Seller, Sells.Ca AS Amount, Sells.DateSold AS Month
    FROM Sells
    INNER JOIN Sellers ON Sells.Seller = Sellers.ID
    
  2. 将此临时表包含到我的第一个数据透视查询中(在FROM部分中):

    TRANSFORM SUM(TempTable.Amount) AS [Monthly Sells]
    SELECT Format$(TempTable.Month, 'mm - mmmm') AS Month
    FROM(
         SELECT Sellers.Name AS Seller, Sells.Ca AS Amount, Sells.DateSold AS Month
         FROM Sells
         INNER JOIN Sellers ON Sells.Seller = Sellers.ID
    ) AS TempTable
    GROUP BY Temptable.Month
    PIVOT Temptable.Seller