0

This is probably quite easy, but I'm stuck on in regardless and my searches didn't come up with anything useful. I have data that currently is in this format:

    ID        Code  
|1212050   |   LB   |
|1212050   |   LJ   |
|1212050   |   LK   |
|1212052   |   FB   |
|B12L076   |   CL   |
|B12L076   |   LK   |

There are many possible codes, but I need the query to display the first ten (even if the ID only has one) in this format:

    ID        Code1    Code2    Code3     
|1212050   |   LB   |   LJ    |   LK   |   
|1212052   |   FB   |         |        |     
|B12L076   |   CL   |   LK    |        |        

I can easily create a crosstab query that creates a view with the codes as column headings, but that creates to many columns. I need it to have columns with labels of "Code1" through "Code10", regardless of which codes are used in the IDs. Here's the kicker: It has to be in Access sql.

Any help would be greatly appreciated.

The test group I will be using to test the query is:

     ID   Code
 |1212044|  LJ|
 |1212044|  LJ|
 |1212044|  LJ|
 |1212050|  HT|
 |1212050|  HT|
 |1212050|  HT|
 |1212050|  HT|
 |1212050|  HT|
 |1212050|  HT|
 |1212050|  HT|
 |1212050|  HT|
 |1212050|  HT|
 |1212050|  LB|
 |1212050|  HT|
 |1212050|  HT|
 |1212050|  HT|
 |1212050|  HT|
 |1212050|  HT|
 |1212050|  HT|
 |1212050|  HT|
 |1212050|  CL|
 |1212050|  LK|
 |1212050|  LK|
 |1212050|  LK|
 |1212050|  CL|
 |1212050|  LK|
 |1212050|  CL|
 |1212050|  LK|
 |1212050|  CL|
 |1212050|  LK|
 |1212050|  LJ|
 |1212052|  FB|
 |1212052|  FB|
 |1212052|  LB|
 |1212052|  FB|
 |B12L076|  CL|
 |B12L076|  LK|
 |B12L076|  CL|
 |B12L076|  LK|
 |B12L076|  LK|
 |B12L076|  CL|
 |B12L076|  LK|
 |B12L076|  CL|
 |B12L076|  LK|
 |B12L076|  LK|
 |B12L076|  CL|
 |B12L076|  LK|
 |B12L076|  CL|
 |B12L076|  LK|
 |B12L076|  CL|
 |B12L076|  LK|
 |B12L076|  CL|
 |B12L076|  LK|
 |B12L076|  CL|
 |B12L076|  LK|
 |B12L076|  CL|
 |B12L076|  LK|
 |B12L076|  CL|
 |B12L076|  LK|
 |B12L076|  CL|
 |B12L076|  LK|
 |B12L076|  CL|
 |B12L076|  LK|
 |B12L103|  LB|
 |B13A072|  CL|
 |B13A072|  LK|
 |B13A072|  HT|
4

3 回答 3

0

我可以想办法用查询来完成这一切:

使用自动编号类型向表中添加索引字段

设置查询以查找每个 ID 的最小索引值

设置第二个查询以查找每个 ID / Code 组合的序列号,计算为 1+ 记录的索引减去先前查询的 ID 索引

然后,第三个查询是第二个查询的交叉表,ID 为行,序列号为列 - 您可以选择序列号 <=10 以限制列数。交叉表中的“值”字段必须计算为代码值的“第一个”,或其他一些文本函数。

进一步思考,我认为您可以通过执行第二个查询中描述的计算并将其用作第三个查询中描述的列标题来组合第二个和第三个查询。我倾向于将事情分解成更小的查询,以使每个查询都变得简单。

于 2013-05-07T17:12:33.917 回答
0

让我们一步一步来。

第一步是将表连接到自身,以便在每个 ID 下列出所有不同的代码对。

完成后,我们可以应用分组来获取每个代码及其在给定 ID 下的代码中的排名。

在代码中:

Select a.ID, a.Code AS EarlierCode, b.Code AS LaterCode
from [YourTable] AS a
INNER JOIN [YourTable] AS b
ON a.ID = b.ID
WHERE a.Code <= b.Code

将此查询另存为 YourPairs。

现在

Select ID, LaterCode AS Code, "Code" & Count(*) AS Label
from [YourPairs] 
HAVING Count(*) <= 10

保存第二个查询,然后使用标签字段作为列标签构建交叉表。

于 2013-05-07T17:18:37.237 回答
0

以下是使用 Microsoft Access 的有效解决方案。

表 1 - 包含列 ID 和代码

Query1 - 使用 Table1 来识别 ID 和 Code 的唯一组合

SELECT Table1.ID, Table1.Code
FROM Table1
GROUP BY Table1.ID, Table1.Code
ORDER BY Table1.ID, Table1.Code;

Query2 - 使用 Query1 添加唯一的代码序列

SELECT Query1.ID, Query1.Code, 'Code ' & Count([Query1_1].[Code]) AS [Code Seq]
FROM Query1 LEFT JOIN Query1 AS Query1_1 ON Query1.ID = Query1_1.ID
WHERE (((Query1_1.Code)<=[Query1].[Code]))
GROUP BY Query1.ID, Query1.Code
ORDER BY Query1.ID, Query1.Code;

Query3 - 使用 Query2 生成所需的交叉表,每个 ID 限制为 10 个代码

TRANSFORM Max(Query2.Code) AS MaxOfCode
SELECT Query2.ID
FROM Query2
GROUP BY Query2.ID
ORDER BY Query2.ID
PIVOT Query2.[Code Seq] In ("Code 1","Code 2","Code 3","Code 4","Code 5","Code 6","Code 7","Code 8","Code 9","Code 10");

结果 - 使用上述测试数据

ID 代码 1 代码 2 代码 3 代码 4 代码 5 代码 6 代码 7 代码 8 代码 9 代码 10
1212044 LJ                                   
1212050 CL HT LB LJ LK
1212052 FB LB
B12L076 CL LK
B12L103 磅
B13A072 CL HT LK
于 2019-08-03T18:38:56.987 回答