2017-01-02 12 views
-2

私はいくつかの異なる値を持つ下の図のような表を持っています。私は、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 
+0

はStackOverflowのへようこそ。 [ask]をお読みください –

+0

必要なテーブルを作成できない問題を指定できますか? – Fabio

答えて

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()); 
      } 

     } 
    } 
} 
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 
関連する問題