将不确定数量的行转置为列?
我有两张有数据的表,我们称它们为表 T 和表 B。T 和 B 中的相关数据相互之间具有多对一关系,如下所示:
T.id | B.code |
---|---|
A | 1 |
A | 1.2 |
B | 1.2 |
B | 1.5 |
C | 1 |
C | 2 |
C | 3 |
C | 15 |
D | 296 |
等(这只是一个粗略的演示,与我使用的实际数据并不相似)。
我运行了一个 count(*) 查询,发现表 T 中的项目出现在表 B 中的次数最多是 8 次。我想要的是转置上面的表,其中 T 中有成千上万的条目,还有几个将 B 对应值的一百个可能条目放入 9 列表中,如下所示:
T.id | CODE 1 | CODE 2 | CODE 3 | CODE 4 | CODE 5 | CODE 6 | CODE 7 | CODE 8 |
---|---|---|---|---|---|---|---|---|
A | 1 | 1.2 | NULL | NULL | NULL | NULL | NULL | NULL |
B | 1.2 | 1.5 | NULL | NULL NULL | NULL | C | 1 | NULL NULL |
2 | 3 | 15 | NULL | NULL | NULL | NULL | D | 296 |
NULL | NULL | NULL | NULL | NULL | NULL | NULL | 等等 | 。 |
我
能找到的唯一搜索信息需要每次手动加入 B 并排除 B.code 的先前值(正如我目前正在做的那样),或使用 PIVOT 表(动态 SQL 或否则),它必须为 B.code 的每个可能值(如上所述,有数百个值)有一列,这两个选项都不是可扩展的选项。
作为参考,我的代码现在看起来像这样:
SELECT
T.id,
b1.code as 'CODE 1', b2.code as 'CODE 2', b3.code as 'CODE 3', b4.code as 'CODE 4' (etc)
FROM T
LEFT JOIN B b1 ON b1.code =
(SELECT TOP 1 code FROM B WHERE B.t_id = T.id)
LEFT JOIN B b2 ON b2.code =
(SELECT TOP 1 code FROM B WHERE B.t_id = T.id AND B.code NOT IN (b1.code))
LEFT JOIN B b3 ON b3.code =
(SELECT TOP 1 code FROM B WHERE B.t_id = T.id AND B.code NOT IN (b1.code, b2.code))
LEFT JOIN B b4 ON b4.code =
(SELECT TOP 1 code FROM B WHERE B.t_id = T.id AND B.code NOT IN (b1.code, b2.code, b3.code))
我的选择是什么?我注定了吗?一定有更好的方法,对吗?
I have two tables with data, let's call them table T and table B. Relevant data in T and B have a many-to-one relationship with each other, like this:
T.id | B.code |
---|---|
A | 1 |
A | 1.2 |
B | 1.2 |
B | 1.5 |
C | 1 |
C | 2 |
C | 3 |
C | 15 |
D | 296 |
etc. (This is just a rough demonstration and doesn't resemble the actual data I'm using).
I've run a count(*) query and found that the most number of times items from table T appear in table B is 8. What I want is to transpose the above table, which has thousands and thousands of entries in T and several hundred possible entries for B's corresponding value, into a 9-column table, like this:
T.id | CODE 1 | CODE 2 | CODE 3 | CODE 4 | CODE 5 | CODE 6 | CODE 7 | CODE 8 |
---|---|---|---|---|---|---|---|---|
A | 1 | 1.2 | NULL | NULL | NULL | NULL | NULL | NULL |
B | 1.2 | 1.5 | NULL | NULL | NULL | NULL | NULL | NULL |
C | 1 | 2 | 3 | 15 | NULL | NULL | NULL | NULL |
D | 296 | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
etc.
The only search info I can find requires either manually joining B each time and excluding previous values of B.code (as I'm currently doing it), or using a PIVOT table (dynamic SQL or otherwise), which would necessarily have one column for each and every possible value of B.code (which, as mentioned, is several hundred values), neither of which are scalable options.
For reference, my code right now looks something like this:
SELECT
T.id,
b1.code as 'CODE 1', b2.code as 'CODE 2', b3.code as 'CODE 3', b4.code as 'CODE 4' (etc)
FROM T
LEFT JOIN B b1 ON b1.code =
(SELECT TOP 1 code FROM B WHERE B.t_id = T.id)
LEFT JOIN B b2 ON b2.code =
(SELECT TOP 1 code FROM B WHERE B.t_id = T.id AND B.code NOT IN (b1.code))
LEFT JOIN B b3 ON b3.code =
(SELECT TOP 1 code FROM B WHERE B.t_id = T.id AND B.code NOT IN (b1.code, b2.code))
LEFT JOIN B b4 ON b4.code =
(SELECT TOP 1 code FROM B WHERE B.t_id = T.id AND B.code NOT IN (b1.code, b2.code, b3.code))
What are my options? Am I doomed? There has to be a better way, right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想要最终的查询是:
但是您需要动态 SQL 才能到达那里,而不必硬编码
...Code 8
或添加Code 9 当另一个代码添加到表中时:
输出(来自问题中的示例数据):
The query you want to end up with is:
But you need dynamic SQL to get there without having to hardcode
...Code 8
or addCode 9
when another code gets added to the table:Output (from sample data in the question):
未经测试,但这也许会有所帮助
Untested, but perhaps this will help