SQL Server 数据透视表

发布于 2024-10-01 00:07:10 字数 426 浏览 1 评论 0 原文

我相当确定我可以使用 PIVOT 函数来解决我的问题,但我就是不明白。任何帮助将不胜感激。

所以我有一个看起来像这样的表

create table Answer (
id int,
question_id int,
user_id int,
answer varchar(1025))

,我想编写一个返回结果集的查询,如下所示:

user_id, question_1, question_2, question_3
1,       'answer1',  'answer2',  'answer3'
2,       'answer1',  'answer2',  'answer3'
n,       'answer1',  'answer2',  'answer3'

这可能吗?

TIA

im fairly sure i can use the PIVOT function to solve my issue, but i just can't figure it out. Any assistance would be greatly appreciated.

So i have a table that looks like

create table Answer (
id int,
question_id int,
user_id int,
answer varchar(1025))

and i would like to write a query that returns a result set in the following:

user_id, question_1, question_2, question_3
1,       'answer1',  'answer2',  'answer3'
2,       'answer1',  'answer2',  'answer3'
n,       'answer1',  'answer2',  'answer3'

is this even possible?

TIA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妄司 2024-10-08 00:07:10

假设表中存在此输入:

insert into answer SELECT 1, 1, 123, 'Answer ZZZZ'
insert into answer SELECT 2, 2, 123, 'Answer AAAA'
insert into answer SELECT 3, 3, 123, 'Answer BBBB'
insert into answer SELECT 4, 1, 345, 'Answer CCCC'
insert into answer SELECT 5, 2, 345, 'Answer DDDD'
insert into answer SELECT 6, 1, 678, 'Answer EEEE'
insert into answer SELECT 7, 2, 678, 'Answer FFFF'
insert into answer SELECT 8, 3, 678, 'Answer SSSS'
insert into answer SELECT 9, 3, 999, 'Answer RRRR'

您可以执行以下操作:

SELECT user_id, [1] as Answer1, [2] as Answer2, [3] as Answer3
    FROM (
    SELECT question_id, user_id, answer
    FROM answer) P
    PIVOT
    (
    MAX (answer)
    FOR Question_id IN ([1], [2], [3])
    ) AS pvt

您会得到以下结果:

user_id Answer1         Answer2         Answer3
123 Answer 1        Answer AAAA      Answer BBBB
345 Answer CCCC     Answer DDDD      NULL
678 Answer EEEE     Answer FFFF      Answer SSSS
999 NULL                NULL         Answer RRRR

Assumming this input in your table:

insert into answer SELECT 1, 1, 123, 'Answer ZZZZ'
insert into answer SELECT 2, 2, 123, 'Answer AAAA'
insert into answer SELECT 3, 3, 123, 'Answer BBBB'
insert into answer SELECT 4, 1, 345, 'Answer CCCC'
insert into answer SELECT 5, 2, 345, 'Answer DDDD'
insert into answer SELECT 6, 1, 678, 'Answer EEEE'
insert into answer SELECT 7, 2, 678, 'Answer FFFF'
insert into answer SELECT 8, 3, 678, 'Answer SSSS'
insert into answer SELECT 9, 3, 999, 'Answer RRRR'

You can do something like this:

SELECT user_id, [1] as Answer1, [2] as Answer2, [3] as Answer3
    FROM (
    SELECT question_id, user_id, answer
    FROM answer) P
    PIVOT
    (
    MAX (answer)
    FOR Question_id IN ([1], [2], [3])
    ) AS pvt

You get this result:

user_id Answer1         Answer2         Answer3
123 Answer 1        Answer AAAA      Answer BBBB
345 Answer CCCC     Answer DDDD      NULL
678 Answer EEEE     Answer FFFF      Answer SSSS
999 NULL                NULL         Answer RRRR
寄意 2024-10-08 00:07:10

可能的?是的。

理想吗?完全在 sql 中执行此操作很少会遇到很多性能问题。一旦数据透视表中的列数超过 8 左右,性能就完全无法实现。

这是否充满法律问题?不幸的是,是的。微软以其无限的智慧获得了专利“逻辑数据与物理数据之间映射的方法和系统”,2007 年 7 月发布

因此,请谨慎行事。

Possible? Yes.

Desirable? Rarely as you will run into lots of performance issues doing this entirely in sql. Once the number of columns in the pivot goes beyond around 8 or so, performance goes completely out the window.

Is this frought with legal issues? Unfortunately, yes. Microsoft in their infinite wisdom patented a "method and system for mapping between logical data and physical data", issued July 2007

So, proceed with caution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文