<tfoot id='91IFx'></tfoot>
      <bdo id='91IFx'></bdo><ul id='91IFx'></ul>

        <small id='91IFx'></small><noframes id='91IFx'>

      1. <legend id='91IFx'><style id='91IFx'><dir id='91IFx'><q id='91IFx'></q></dir></style></legend>

        <i id='91IFx'><tr id='91IFx'><dt id='91IFx'><q id='91IFx'><span id='91IFx'><b id='91IFx'><form id='91IFx'><ins id='91IFx'></ins><ul id='91IFx'></ul><sub id='91IFx'></sub></form><legend id='91IFx'></legend><bdo id='91IFx'><pre id='91IFx'><center id='91IFx'></center></pre></bdo></b><th id='91IFx'></th></span></q></dt></tr></i><div id='91IFx'><tfoot id='91IFx'></tfoot><dl id='91IFx'><fieldset id='91IFx'></fieldset></dl></div>

        SQL Server 中的动态枢轴列

        时间:2023-07-17

          • <tfoot id='pb5XP'></tfoot>

            <small id='pb5XP'></small><noframes id='pb5XP'>

            <legend id='pb5XP'><style id='pb5XP'><dir id='pb5XP'><q id='pb5XP'></q></dir></style></legend>

            <i id='pb5XP'><tr id='pb5XP'><dt id='pb5XP'><q id='pb5XP'><span id='pb5XP'><b id='pb5XP'><form id='pb5XP'><ins id='pb5XP'></ins><ul id='pb5XP'></ul><sub id='pb5XP'></sub></form><legend id='pb5XP'></legend><bdo id='pb5XP'><pre id='pb5XP'><center id='pb5XP'></center></pre></bdo></b><th id='pb5XP'></th></span></q></dt></tr></i><div id='pb5XP'><tfoot id='pb5XP'></tfoot><dl id='pb5XP'><fieldset id='pb5XP'></fieldset></dl></div>
              <bdo id='pb5XP'></bdo><ul id='pb5XP'></ul>

                    <tbody id='pb5XP'></tbody>
                • 本文介绍了SQL Server 中的动态枢轴列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述


                  我在 SQL Server 中有一个名为 Property 的表,其中包含以下列:


                  I have a table named Property with following columns in SQL Server:

                  Id    Name
                  

                  这个表中有一些属性,其他表中的某个对象应该赋予它价值.

                  there are some property in this table that certain object in other table should give value to it.

                  Id    Object_Id    Property_Id    Value
                  

                  我想制作一个如下所示的数据透视表,其中我在第一个表中声明的每个属性都有一列:

                  I want to make a pivot table like below that has one column for each property I've declared in 1'st table:

                  Object_Id    Property1    Property2    Property3    ...
                  

                  我想知道如何从表中动态获取数据透视列.因为第一个表中的行会改变.

                  I want to know how can I get columns of pivot dynamically from table. Because the rows in 1'st table will change.

                  推荐答案

                  是这样的:

                  DECLARE @cols AS NVARCHAR(MAX);
                  DECLARE @query AS NVARCHAR(MAX);
                  
                  select @cols = STUFF((SELECT distinct ',' +
                                          QUOTENAME(Name)
                                        FROM property
                                        FOR XML PATH(''), TYPE
                                       ).value('.', 'NVARCHAR(MAX)') 
                                          , 1, 1, '');
                  
                  SELECT @query =
                  
                  'SELECT *
                  FROM
                  (
                    SELECT
                      o.object_id,
                      p.Name,
                      o.value
                    FROM propertyObjects AS o
                    INNER JOIN property AS p ON o.Property_Id = p.Id
                  ) AS t
                  PIVOT 
                  (
                    MAX(value) 
                    FOR Name IN( ' + @cols + ' )' +
                  ' ) AS p ; ';
                  
                   execute(@query);
                  

                  SQL Fiddle 演示.

                  这会给你这样的东西:

                  SQL Fiddle Demo.

                  This will give you something like this:

                  | OBJECT_ID | PROPERTY1 | PROPERTY2 | PROPERTY3 | PROPERTY4 |
                  -------------------------------------------------------------
                  |         1 |        ee |        fd |       fdf |      ewre |
                  |         2 |       dsd |       sss |      dfew |       dff |
                  

                  这篇关于SQL Server 中的动态枢轴列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:SQL 查询以逗号分隔符以及 SQL Server 中的按列分组获取聚合结果 下一篇:sql 2005 中的枢轴

                  相关文章

                    <bdo id='UFf2B'></bdo><ul id='UFf2B'></ul>

                      <tfoot id='UFf2B'></tfoot>
                    1. <i id='UFf2B'><tr id='UFf2B'><dt id='UFf2B'><q id='UFf2B'><span id='UFf2B'><b id='UFf2B'><form id='UFf2B'><ins id='UFf2B'></ins><ul id='UFf2B'></ul><sub id='UFf2B'></sub></form><legend id='UFf2B'></legend><bdo id='UFf2B'><pre id='UFf2B'><center id='UFf2B'></center></pre></bdo></b><th id='UFf2B'></th></span></q></dt></tr></i><div id='UFf2B'><tfoot id='UFf2B'></tfoot><dl id='UFf2B'><fieldset id='UFf2B'></fieldset></dl></div>

                      <legend id='UFf2B'><style id='UFf2B'><dir id='UFf2B'><q id='UFf2B'></q></dir></style></legend>

                      <small id='UFf2B'></small><noframes id='UFf2B'>