简体   繁体   English

如何基于具有相同元素的列将SQL表拆分为多个?

[英]How can I split a SQL table into multiple based on columns which have the same elements?

At the moment I have one large SQL table. 目前,我有一个大的SQL表。 Let's say it looks like this: 假设它看起来像这样:

Table: allData
County Census Tract Population Name
001    xxxxxx       4328       County1
001    yyyyyy       4729       County1
002    zzzzzz       5629       County2
003    aaaaaa       3947       County3

What I want is an individual table for each county. 我想要的是每个县的单独表格。 So I would have: 所以我会有:

Table: County1
County Census Tract Population Name
001    xxxxxx       4328       County1
001    yyyyyy       4729       County1

Table: County2
County Census Tract Population Name
002    zzzzzz       5629       County2

Table: County3
County Census Tract Population Name
003    aaaaaa       3947       County3

Thanks. 谢谢。

If your use case requires a separate table for each County you can use the following SQL to generate create table statements 如果您的用例需要为每个县使用单独的表,则可以使用以下SQL生成create table语句

select 'create table '+Name+' as select * from allData where name = '''+Name+''';'
  from allData
 group by name

This SQL will generate insert statements 该SQL将生成insert语句

select 'insert into '+Name+' select * from allData where name = '''+Name+''';'
  from allData
 group by name

You can run each of these above and copy paste the result into your SQL client to create and populate the tables 您可以运行上述每种方法,然后将结果复制粘贴到SQL客户端中,以创建并填充表

If you need to specify schema names add it right before Name like 如果您需要指定架构名称,则将其添加在“ Name之前,例如

select 'create table <schema.>'+Name+' as select * from allData where name = '''+Name+''';'

I have used the following SQL to test the same 我已经使用以下SQL测试相同

with allData as (    
select '001' County,    'xxxxxx' Census_Tract,       4328 Population,      'County1' Name union all
select '001',    'yyyyyy',       4729,       'County1' union all
select '002',    'zzzzzz',       5629,       'County2' union all
select '003',    'aaaaaa',       3947,       'County3' )
select 'create table '+Name+' as select * from allData where name = '''+Name+''';'
group by name;

with allData as (    
select '001' County,    'xxxxxx' Census_Tract,       4328 Population,      'County1' Name union all
select '001',    'yyyyyy',       4729,       'County1' union all
select '002',    'zzzzzz',       5629,       'County2' union all
select '003',    'aaaaaa',       3947,       'County3' )
select 'insert into '+Name+' select * from allData where name = '''+Name+''';'
from allData
group by name;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何有效地提取仅包含SQL中具有重复元素的行的子表? - How can I efficiently extract a sub-table which only contains rows that have duplicated elements in SQL? 如何将返回多列的两个 SQL 查询组合到同一个表的不同列中? - How can I combine two SQL queries returning multiple columns into separate columns of the same table? 如何将SQL表拆分为多列 - How to split SQL table into multiple columns 我可以在同一张表中有2个唯一的列吗? - Can I have 2 unique columns in the same table? 如何在SQL中查找一个表中的所有X,而另一个表中的Y值集相同 - How can I find in SQL all X from one table which have same set of Y values from another table 如何从MySQL表中获取多列具有相同值的所有行? - How to get all the rows from MySQL table which have same values for multiple columns? 如何在sql中将多列拆分为多行(用于学校时间表) - how to split multiple columns into multiple rows in sql (for school time table) 如何在 sql 的两个表上加入多个列 - how can I join multiple columns on two table in sql 基于同一表中的多个列和记录创建SQL更新查询 - Creating a SQL update query based on multiple columns and records in same table 如何将SQL查询返回的值拆分为SQL SERVER中的多个列? - How can I split values returned by sql query into multiple columns in SQL SERVER?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM