Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何从数据透视表创建多个表_Mysql_Sql_Sql Server_Ssis_Pivot - Fatal编程技术网

Mysql 如何从数据透视表创建多个表

Mysql 如何从数据透视表创建多个表,mysql,sql,sql-server,ssis,pivot,Mysql,Sql,Sql Server,Ssis,Pivot,我有一个如下生成的透视表,Name包含列名,text包含它们的值,因此需要基于类似的值生成多个表 输入表 ID Name text 1 Name,DOB John,02/02/1980 2 FirstName,SSN,City Ray,987898789,Chicago 3 Name,DOB Mary,12/21/1990 4 FirstName,SSN,City Cary,987000789,Dallas 5 PersonID

我有一个如下生成的透视表,Name包含列名,text包含它们的值,因此需要基于类似的值生成多个表

输入表

ID  Name    text    
1   Name,DOB    John,02/02/1980 
2   FirstName,SSN,City  Ray,987898789,Chicago   
3   Name,DOB    Mary,12/21/1990 
4   FirstName,SSN,City  Cary,987000789,Dallas   
5   PersonID,Code,Zip,Gender,Maritial   1234,A456,23456,M,single    
6   PersonID,Code,Zip,Gender,Maritial   1235,A457,23233,M,single    
7   PersonID,Code,Zip,Gender,Maritial   1236,A458,67675,M,Married   
所以输出表应该是

产出表1

ID  Name    DOB 
1   john    02/02/1980  
3   Mary    02/02/1980  
产出表2

ID  FirstName   SSN City
2   Ray 987898789   Chicago
4   Cary    987000789   Dallas
产出表3

ID  PersonID    Zip Gender  Marital 
5   1234    A456    23456   M   Single  
6   1235    A457    23233   M   Single  
7   1236    A458    67675   M   Married 

有人能帮我解决这个问题吗。这可以在Sqlserver、MySQL或SSIS中实现吗?

我的建议是首先规范化您的
输入表。在SQL Server中,可以使用递归CTE将逗号分隔列表中的数据拆分为行

CTE将类似于以下内容:

;with cte (id, col, Name_list, value, text_list) as
(
  select id,
    cast(left(Name, charindex(',',Name+',')-1) as varchar(50)) col,
         stuff(Name, 1, charindex(',',Name+','), '') Name_list,
    cast(left(text, charindex(',',text+',')-1) as varchar(50)) value,
         stuff(text, 1, charindex(',',text+','), '') text_list
  from input
  union all
  select id,
    cast(left(Name_list, charindex(',',Name_list+',')-1) as varchar(50)) col,
    stuff(Name_list, 1, charindex(',',Name_list+','), '') Name_list,
    cast(left(text_list, charindex(',',text_list+',')-1) as varchar(50)) value,
         stuff(text_list, 1, charindex(',',text_list+','), '') text_list
  from cte
  where Name_list > ''
    or text_list > ''
) 
select id, col, value
from cte;
看。这将为您提供以下格式的数据:

| ID |       COL |      VALUE |
-------------------------------
|  1 |      Name |       John |
|  2 | FirstName |        Ray |
|  3 |      Name |       Mary |
|  4 | FirstName |       Cary |
|  5 |  PersonID |       1234 |
一旦数据采用该格式,就可以基于每个表中所需的列来透视数据

例如,如果您想要
表1
的数据,您将使用:

;with cte (id, col, Name_list, value, text_list) as
(
  select id,
    cast(left(Name, charindex(',',Name+',')-1) as varchar(50)) col,
         stuff(Name, 1, charindex(',',Name+','), '') Name_list,
    cast(left(text, charindex(',',text+',')-1) as varchar(50)) value,
         stuff(text, 1, charindex(',',text+','), '') text_list
  from input
  union all
  select id,
    cast(left(Name_list, charindex(',',Name_list+',')-1) as varchar(50)) col,
    stuff(Name_list, 1, charindex(',',Name_list+','), '') Name_list,
    cast(left(text_list, charindex(',',text_list+',')-1) as varchar(50)) value,
         stuff(text_list, 1, charindex(',',text_list+','), '') text_list
  from cte
  where Name_list > ''
    or text_list > ''
) 
select *
-- into table1
from
(
  select id, col, value
  from cte
  where col in ('Name', 'DOB')
) d
pivot
(
  max(value)
  for col in (Name, DOB)
) piv;


然后,您将用下一个表的值替换每个查询中的列名。

只需手动硬编码每个查询类型;对此没有自动支持,但这比困难更乏味。