Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.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
Sql 从子记录生成父记录_Sql - Fatal编程技术网

Sql 从子记录生成父记录

Sql 从子记录生成父记录,sql,Sql,我有以下情况 我正在从外部系统获取表单中的数据 UserId Group Value 1 Abc ..... 1 Abc ..... 1 Bcd ..... 2 Abc ..... 我需要将这些数据按摩到我的模式中,即 Parent Table ID UserID Group Name 1 1 Abc User 1 - Abc data 2 1

我有以下情况

我正在从外部系统获取表单中的数据

UserId  Group  Value
1       Abc    .....
1       Abc    .....
1       Bcd    .....
2       Abc    .....
我需要将这些数据按摩到我的模式中,即

Parent Table
ID    UserID    Group    Name
1     1         Abc      User 1 - Abc data
2     1         Bcd      User 1 - Bcd data
3     2         Abc      User 2 - Abc data

Child Table
ParentID  Value
1         .....
1         .....
2         .....
3         .....
因此,子数据和来自外部系统的数据之间存在1:1的比例。 问题是,我从子数据开始,需要编写一个查询,该查询可以基于UserID和Group的不同组合创建父记录

需要编写一个查询,该查询可以基于UserID和Group的不同组合创建父记录

(其中具有自动递增的ID)

然后:


请务必明确说明所使用的语言/技术。我猜想您正在使用SQL Server,因此最好将此添加到标记列表中。或者在问题中提到它。
insert into parent(UserID, Group, Name) 
select distinct 
UserID,  
Group, UserID, 
'User ' || UserID || ' - ' || Group || '  data'
from raw_input_data;
insert parent (userId, group, name)
select distinct userId, group, concat("user ", userId, " - ", group, " data")
from input_data;
insert child (parentId, value)
select p.parentId, d.value
from parent p
inner join input_data d on p.user_id = d.user_id and p.group = d.group;