简单的SQLServer PIVOT/Transpose查询,怎么写?

发布于 2024-10-19 04:19:44 字数 821 浏览 3 评论 0原文

我有一个 SELECT,它以以下形式返回数据...

ID             Question             Answer
1              Any Good?            Yes
1              Happy?               No
1              Good Staff?          Yes
1              Return?              N/A
2              Any Good?            No
2              Happy?               No
2              Good Staff?          Yes
2              Return               N/A
...

我需要以下形式...

ID       Any Good?   Happy?   Good Staff?   Return?
1        Yes         No       Yes           N/A
2        No          No       Yes           N/A
...

我的顶级查询中的“Answer”列是使用 CASE .. WHEN 计算的。我想也许 PIVOT 子句可以帮助我。但这似乎需要聚合。我不需要聚合,只需转置即可。

我不介意必须在解决方案中显式指定每个 Answer (我猜无论如何我都必须这样做)。

有人知道这样做的最佳方法吗?

I have a SELECT which is returning me data in the following form...

ID             Question             Answer
1              Any Good?            Yes
1              Happy?               No
1              Good Staff?          Yes
1              Return?              N/A
2              Any Good?            No
2              Happy?               No
2              Good Staff?          Yes
2              Return               N/A
...

I need this in the following form...

ID       Any Good?   Happy?   Good Staff?   Return?
1        Yes         No       Yes           N/A
2        No          No       Yes           N/A
...

The 'Answer' column in my top query is computed using a CASE .. WHEN. I was thinking maybe the PIVOT clause could help me. This seems to require aggregation though. I don't need to aggregate, just transpose.

I don't mind having to specify each Answer explicitly in the solution (I'm guessing I'll have to do that anyway).

Anyone know the best way of doing this?

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

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

发布评论

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

评论(2

两相知 2024-10-26 04:19:44

你有没有尝试过类似的东西

SELECT  *
FROM    (
            SELECT  ID,
                    Question,
                    Answer
            FROM    @Table1
        ) t
PIVOT (MAX(Answer) FOR Question IN ([Any Good?],[Happy?],[Good Staff?],[Return?])) p

Have you tried something like

SELECT  *
FROM    (
            SELECT  ID,
                    Question,
                    Answer
            FROM    @Table1
        ) t
PIVOT (MAX(Answer) FOR Question IN ([Any Good?],[Happy?],[Good Staff?],[Return?])) p
骄傲 2024-10-26 04:19:44

枢轴操作要求您使用某种形式的聚合,但是如果您只有一个值,Max()将为您获取最大(唯一)值,

这样的东西应该可以工作

Select *
from Table
Pivot
(
    Max(answer)
    For Question In ([Any Good?],[Happy?],[Good Staff?],[Return?])
)
AS P

The pivot operation requires you to use some form of aggregate, however if you will only have one value Max() will grab the max (only) value for you

Something Like this should work

Select *
from Table
Pivot
(
    Max(answer)
    For Question In ([Any Good?],[Happy?],[Good Staff?],[Return?])
)
AS P
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文