调换表格

发布于 2024-09-14 16:40:36 字数 535 浏览 7 评论 0原文

你好,我想旋转一个完整的桌子:

LANGUAGE litAccept litDelete litErrorMsg  ..............
-------------------------------------------------
SPANISH  Accept    Delete    Error has ocurred
ENGLISH  Aceptar   Borrar    Ha ocurrido un error
.....

变成这样的东西:

LANGUAGE     ENGLISH   SPANISH
---------------------------------------
litAccept    Accept    Aceptar    
litDelete    Aceptar   Borrar    
litErrorMsg  Error..   Ha ocurridO..
...

我看到用 PIVOT 你可以旋转桌子,但我不知道是谁做的!

请帮忙,谢谢

Hi I would like to rotate a full table:

LANGUAGE litAccept litDelete litErrorMsg  ..............
-------------------------------------------------
SPANISH  Accept    Delete    Error has ocurred
ENGLISH  Aceptar   Borrar    Ha ocurrido un error
.....

into something like this:

LANGUAGE     ENGLISH   SPANISH
---------------------------------------
litAccept    Accept    Aceptar    
litDelete    Aceptar   Borrar    
litErrorMsg  Error..   Ha ocurridO..
...

I see with PIVOT you can rotate a table, but I don't know who do this!

Please help, thanks

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

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

发布评论

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

评论(3

油焖大侠 2024-09-21 16:40:36

http://www.simple- talk.com/community/blogs/andras/archive/2007/09/14/37265.aspx

Pivot 无法执行“动态”操作,因此您必须使用此方法

http://www.simple-talk.com/community/blogs/andras/archive/2007/09/14/37265.aspx

Pivot can't do 'dynamic' so you'll have to use this method

玉环 2024-09-21 16:40:36

哇我已经做到了!!

现在我明白了 PIVOT 是如何工作的。我尝试使用动态 SQL 一次旋转整个表,但没有办法将其适应pivot 的语法。旋转必须逐列 (PIVOT) 或逐行 (UNPIVOT) 完成,将它们插入临时表中以在最后获得完整的转置。

我认为这就是我的方法..但也许它可以在单个查询中完成? (UNION ALL ins 无效,你知道;P)

第二天我会写下我找到的解决方案。

谢谢!

Wow I have done it!!

Now I understand how it works PIVOT. I tried to rotate the entire table at once with dynamic SQL and there is no way to fit it in the syntax of pivot. The rotation must be done column by column (PIVOT) or line by line (UNPIVOT) inserting them in a temporally table to get the full transposing at the end.

I think this is the way for me.. but maybe it can be done in single query?? (UNION ALL ins't valid, you know ;P)

Next day I will write the solution I found.

Thanks!

我也只是我 2024-09-21 16:40:36

这可以使用动态 SQL 结合 UNPIVOTPIVOT 来完成。

DECLARE @colsUnPivot AS NVARCHAR(MAX),
    @colsPivot AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

SET @colsUnPivot = stuff((select ','+quotename(C.name)
         from sys.columns as C
         where C.object_id = object_id('t') and
               C.name like 'lit%'
         for xml path('')), 1, 1, '')

SET @colsPivot = STUFF((SELECT distinct ',' + QUOTENAME(c.Language) 
            FROM t c
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 
            'SELECT * from 
            (
                select Language l1, type language, value
                from t
                unpivot
                (
                    value
                    for type in (' + @colsUnPivot + ')
                ) upvt
            ) x
            pivot 
            (
                min(value)
                for l1 in (' + @colsPivot + ')
            ) p '


execute(@query)

请参阅SQL Fiddle 演示

This can be completed using Dynamic SQL with both an UNPIVOT and then a PIVOT.

DECLARE @colsUnPivot AS NVARCHAR(MAX),
    @colsPivot AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

SET @colsUnPivot = stuff((select ','+quotename(C.name)
         from sys.columns as C
         where C.object_id = object_id('t') and
               C.name like 'lit%'
         for xml path('')), 1, 1, '')

SET @colsPivot = STUFF((SELECT distinct ',' + QUOTENAME(c.Language) 
            FROM t c
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 
            'SELECT * from 
            (
                select Language l1, type language, value
                from t
                unpivot
                (
                    value
                    for type in (' + @colsUnPivot + ')
                ) upvt
            ) x
            pivot 
            (
                min(value)
                for l1 in (' + @colsPivot + ')
            ) p '


execute(@query)

See SQL Fiddle with a Demo

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