Access中的反向交叉表查询

时间:2013-08-28 14:28:46

标签: sql ms-access

在Access 2013中,我的表格具有以下结构:

Name   |City   |Q113   |Q213
Peter  |London |20     |30
Sandra |Paris  |40     |50

我想“反转交叉表查询”,获取以下内容:

Name   |City   |Period   |Value
Peter  |London |Q113     |20
Peter  |London |Q213     |30
Sandra |Paris  |Q113     |40
Sandra |Paris  |Q213     |50

我使用SQL尝试了一个Union查询:

SELECT [Name], [City] ,'Q113' AS [Period], [Q113] AS [Value]
FROM [Database]
UNION ALL
ORDER BY [Name]
       , [City]
       , [Period]

然而,它不起作用,我不断收到错误:“预期无效的SQL语句;'删除','插入','过程','选择'或'更新'

谷歌搜索此错误没有多大帮助,所以我猜上面的代码有问题?请帮忙。

1 个答案:

答案 0 :(得分:1)

UNION ALL

后,您的查询丢失了
SELECT [Name], [City] ,'Q113' AS [Period], [Q113] AS [Value]
FROM [Database]
UNION ALL
SELECT [Name], [City] ,'Q213' AS [Period], [Q213] AS [Value]
FROM [Database]
ORDER BY [Name], [City], [Period]

您需要添加查询的第二部分,然后将ORDER BY放在最后。