使用列中的不同值作为代码中的列名创建表

时间:2017-01-02 11:54:42

标签: c# sql-server vb.net

我有一张如下图所示的表,其中包含一些不同的值。我想为R列中的每个不同值创建一个包含列的新表,列名将以S开头。提前感谢您的帮助。 (在sql server或c#或vb.net中)

我的表:

Name Family R   C  D
---------------------
N1   F1     S1  1  A
N2   F2     S2  2  A
N3   F3     S1  1  B
N4   F4     S2  2  B
N5   F5     S3  3  A

新表:

    S1  S2  S3 .......
---------------------------------
A   N1  N2  N5 .......
    F1  F2  F5 .......
----------------------------------
B   N3  N4
    F3  F4

2 个答案:

答案 0 :(得分:0)

但是你想要这个要求的确切目的是什么

示例数据:

DECLARE @Table1  TABLE 
    (Name varchar(2), Family varchar(2), R varchar(2), C int, D varchar(1))
;

INSERT INTO @Table1
    (Name, Family, R, C, D)
VALUES
    ('N1', 'F1', 'S1', 1, 'A'),
    ('N2', 'F2', 'S2', 2, 'A'),
    ('N3', 'F3', 'S1', 1, 'B'),
    ('N4', 'F4', 'S2', 2, 'B'),
    ('N5', 'F5', 'S3', 3, 'A')
;

脚本

;with CTE as (
SELECT D,
[S1],
[S2],
[S3] 
        FROM (
select Name,
       Family, 
       R, 
       C, 
       D 
            FROM @Table1)t
                PIVOT (MAX(Name) For R in ([S1],[S2],[S3])
)pvt 
                GROUP BY  D,[S1],[S2],[S3] )
,
 CTE2 AS (

 SELECT D,
       [S1],
       [S2],
       [S3] 
         FROM (
select Name, 
       Family, 
       R, 
       C, 
       D FROM @Table1)t
            PIVOT (MAX(Family) For r in ([S1],[S2],[S3])
)pvt 
GROUP BY  D,[S1],[S2],[S3])

Select 
    CASE WHEN RN = 1 THEN D ELSE NULL END NAME ,S1,S2,S3 FROM (
Select *,
        Row_number()OVER(PARTITION BY D ORDER BY (SELECT NULL))RN from (
            SELECT  D,
            (SELECT S1 FROM cte where S1 IS NOT NULL AND C.D = D)S1,
            (SELECT S2 FROM cte where S2 IS NOT NULL AND C.D = D)S2,
            (SELECT S3 FROM cte where S3 IS NOT NULL AND C.D = D)S3  FROM  CTE C
            UNION 
            SELECT  D,
            (SELECT S1 FROM CTE2 where S1 IS NOT NULL AND C.D = D)S1,
            (SELECT S2 FROM CTE2 where S2 IS NOT NULL AND C.D = D)S2,
            (SELECT S3 FROM CTE2 where S3 IS NOT NULL AND C.D = D)S2  FROM cte2 C)T)TT

答案 1 :(得分:0)

尝试创建这样的数据透视表:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            DataTable dt = new DataTable();

            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Family", typeof(string));
            dt.Columns.Add("R", typeof(string));
            dt.Columns.Add("C", typeof(int));
            dt.Columns.Add("D", typeof(string));

            dt.Rows.Add(new object[] {"N1","F1","S1", 1, "A"});
            dt.Rows.Add(new object[] {"N2","F2","S2", 2, "A"});
            dt.Rows.Add(new object[] {"N3","F3","S1", 1, "B"});
            dt.Rows.Add(new object[] {"N4","F4","S2", 2, "B"});
            dt.Rows.Add(new object[] {"N5","F5","S3", 3, "A"});

            string[] uniqueRs = dt.AsEnumerable().Select(x => x.Field<string>("R")).Distinct().OrderBy(x => x).ToArray();

            DataTable pivot = new DataTable();
            pivot.Columns.Add("D", typeof(string));
            foreach (string uniqueR in uniqueRs)
            {
                pivot.Columns.Add(uniqueR, typeof(string));
            }

            var groups = dt.AsEnumerable().GroupBy(x => x.Field<string>("D"));

            foreach (var group in groups)
            {
                List<string> names = group.OrderBy(x => x.Field<int>("C")).Select(x => x.Field<string>("Name")).ToList();
                names.Insert(0, group.Key);
                pivot.Rows.Add(names.ToArray());

                List<string> families = group.OrderBy(x => x.Field<int>("C")).Select(x => x.Field<string>("Family")).ToList();
                families.Insert(0, group.Key);
                pivot.Rows.Add(families.ToArray());
            }

        }
    }
}