ms访问查询数据透视

时间:2018-07-14 13:58:28

标签: sql ms-access

我有两个桌子。

tblA
ID   Date      Price
--------------------
1    10/1/18   100
2    10/2/18   200
3    10/1/19    50

tblB
ID   Date      Price
------------------
1    10/5/18   100
2    10/6/18   100
3    10/7/18   100

我有以下查询来汇总每月价格。

TRANSFORM IIf(Sum([SumPrice]) Is Null,0,Sum([SumPrice])) AS PricePerMonth
SELECT Format([AllDate],"yyyy") AS [Year], t.Name
FROM (
    SELECT [Date] as AllDate, Nz(Price, 0) AS SumPrice, 'tblA' As Name
    FROM tblA
UNION All
    SELECT [Date] as AllDate, Nz(Price, 0) AS SumPrice, 'tblB' As Name
    FROM tblB
)  AS t
GROUP BY Format([AllDate],"yyyy"), t.Name
ORDER BY Format([AllDate],"yyyy") DESC , t.Name
PIVOT Month([AllDate]) In (1,2,3,4,5,6,7,8,9,10,11,12);

输出查询:

Year  Name     1    2    3    4    5    6    7    8    9    10    11    12
--------------------------------------------------------------------------
2019  tblA    50    0    0    0    0    0    0    0    0     0     0     0
2018  tblA   100  200    0    0    0    0    0    0    0     0     0     0
2018  tblB     0    0    0    0  100  100  100    0    0     0     0     0

有没有办法获得此输出:

Year  Name     1    2    3    4    5    6    7    8    9    10    11    12
--------------------------------------------------------------------------
2019  tblA    50    0    0    0    0    0    0    0    0     0     0     0
2019  tblB     0    0    0    0    0    0    0    0    0     0     0     0   <===Different from above.
2018  tblA   100  200    0    0    0    0    0    0    0     0     0     0
2018  tblB     0    0    0    0  100  100  100    0    0     0     0     0

如您所见,tblB没有2019年的数据。所以我需要找到一种添加方法。

谢谢。

2 个答案:

答案 0 :(得分:1)

在“全部并集”中添加了其他“选择”,这将使另一个表缺少日期。

    TRANSFORM IIf(Sum([SumPrice]) Is Null,0,Sum([SumPrice])) AS PricePerMonth
SELECT Format([AllDate],"yyyy") AS [Year], t.Name
FROM (
       SELECT [Date] as AllDate, Nz(Price, 0) AS SumPrice, 'tblA' As Name
    FROM tblA
UNION All
    SELECT [Date] as AllDate, Nz(Price, 0) AS SumPrice, 'tblB' As Name
    FROM tblB

Union All

SELECT  tblb.Date as dt, 0 ,  'tblA' As Name
FROM tblB Left  JOIN tblA ON  Format(tblA.[Date],'yyyy')  = Format(tblB.[Date],'yyyy') 
where not Format(tblA.Date,'yyyy') = Format(tblb.Date,'yyyy')

Union All

SELECT (tblA.Date) as dt, 0 ,  'tblB' As Name
FROM tblA Left  JOIN tblB ON  Format(tblA.[Date],'yyyy')  = Format(tblB.[Date],'yyyy') 
where not Format(tblA.Date,'yyyy') = Format(tblb.Date,'yyyy')

)  AS t
GROUP BY  t.Name,Format([AllDate],"yyyy")
ORDER BY Format([AllDate],"yyyy") DESC , t.Name
PIVOT Month([AllDate]) In (1,2,3,4,5,6,7,8,9,10,11,12)

enter image description here

使用@YvetteLee查询结果如下所示(添加了屏幕截图只是为了显示差异) enter image description here

答案 1 :(得分:1)

尝试一下:

TRANSFORM IIf(Sum([SumPrice]) Is Null,0,Sum([SumPrice])) AS PricePerMonth
SELECT Format([AllDate],"yyyy") AS [Year], t.Name
FROM (
   SELECT [Date] as AllDate, Nz(Price, 0) AS SumPrice, 'tblA' As Name
   FROM tblA
UNION All
   SELECT [Date] as AllDate, Nz(Price, 0) AS SumPrice, 'tblB' As Name
   FROM tblB
UNION ALL
   SELECT [Date] as AllDate, 0 AS SumPrice, 'tblA' As Name
   FROM tblB
UNION ALL
   SELECT [Date] as AllDate, 0 AS SumPrice, 'tblB' As Name
   FROM tblA
)  AS t
GROUP BY  t.Name,Format([AllDate],"yyyy")
ORDER BY Format([AllDate],"yyyy") DESC , t.Name
PIVOT Month([AllDate]) In (1,2,3,4,5,6,7,8,9,10,11,12)