在MS-Access中,我有下表有重复字段(SpecialtyCodes),我想将它们显示为列。它的交叉表类型,但Access需要一个不同的值字段。 IDName,将是我的行标题字段,而SpecialtyCode将是我的列标题字段,但我不能为字段选择相同的字段。
很难解释我想要完成的事情,所以我正在展示一个例子。我知道这不是一个规范化的表,但我从报告中获取数据放入表中。
示例表:
IDName SpecialtyCode
A1 11
A1 23
A1 154
A2 44
A2 88
A3 05
示例输出:
IDName SpecialtyCode SpecialtyCode SpecialtyCode
A1 11 23 154
A2 44 88
A3 05
答案 0 :(得分:0)
据我所知,没有很好的方法可以做到这一点。这就是我要做的事情:
首先对您的参赛作品进行排名。
SELECT tb.IdName, tb.specialtyCode,
(SELECT COUNT(tbi.specialtyCode) FROM Table1 AS tbi WHERE tbi.IdName = tb.IdName
AND tbi.specialtyCode >= tb.specialtyCode) AS rank
FROM Table1 AS tb
接下来,您可以嵌套此查询,并使用IIF
将每个条目放在不同的列中。
SELECT tbo.IdName,
MAX(IIF(tbo.rank = 1, tbo.specialtyCode, "")) AS firstsc,
MAX(IIF(tbo.rank = 2, tbo.specialtyCode, "")) AS secondsc,
MAX(IIF(tbo.rank = 3, tbo.specialtyCode, "")) AS thirdsc
FROM
(SELECT tb.IdName, tb.specialtyCode,
(SELECT COUNT(tbi.specialtyCode) FROM Table1 AS tbi WHERE
tbi.IdName = tb.IdName AND tbi.specialtyCode >= tb.specialtyCode)
AS rank
FROM Table1 AS tb) AS tbo
GROUP BY tbo.IdName