今天的另一个问题。 MS Access。
给出表格:
FormsList
FormID LocationID FormTypeID FormDate
1 001 001 21.11.2012
2 002 002 20.11.2012
3 001 003 16.11.2012
4 001 001 13.12.2012
4 003 004 13.12.2012
...
LocationsList
LocationID LocationName
001 FirstLocation
002 SecondLocation
003 ThirdLocation
004 FourthLocation
...
FormTypes
FormTypeID FormType
001 "C"
002 "R"
003 "F"
004 "X"
...
我需要一个交叉表查询,或者需要一些表来获得:
LocationID
。Query / s应为上表显示以下结果:
LocationID "C" "R" "F" "X"
001 2 0 0 0
002 0 1 0 0
003 0 0 0 1
004 0 0 0 0
谢谢。
答案 0 :(得分:1)
您应该能够使用以下内容转换数据:
TRANSFORM Count(FormsList.FormTypeID)
select LocationsList.LocationId
from (LocationsList
inner join FormsList
on LocationsList.LocationId = FormsList.LocationId)
inner join FormTypes
on FormsList.FormTypeId = FormTypes.FormTypeId
GROUP BY LocationsList.LocationId
PIVOT FormTypes.FormType
编辑,如果您需要所有地点,则需要使用LEFT JOIN
:
TRANSFORM Count(FormsList.FormTypeID)
select LocationsList.LocationId
from (LocationsList
left join FormsList
on LocationsList.LocationId = FormsList.LocationId)
left join FormTypes
on FormsList.FormTypeId = FormTypes.FormTypeId
GROUP BY LocationsList.LocationId
PIVOT FormTypes.FormType