T-SQL查询可根据2列组合条件在表中插入缺失值

时间:2018-11-10 01:17:30

标签: sql sql-server tsql

我有以下SQL Server 2016表,其中包含3种颜色和3种形状的所有可能组合:

Color       Shape
---------------------
red         square
red         circle
red         octagon
yellow      square
yellow      circle
yellow      octagon
green       square
green       circle
green       octagon    

我有以下包含“对象”的数据表,如下所示(没有重复项):

Object ID              Shape            Color
------------------------------------------------
object1                square           green
object1                square           red
object1                octagon          yellow
object1                circle           green
object2                circle           red
object2                square           yellow
object3                square           red
object3                circle           red
object3                square           yellow
object3                square           yellow
object3                octagon          green
object4                circle           red
etc......
etc......
object100           

如您所见,每个对象在形状和颜色组合方面存在一些“空白”。

我想要实现的是为每个[object]记录插入任何缺少的shape + color组合。所需的对象1和对象2的输出例如:

     Object ID            Color       Shape
     -------------------------------------------
     object1              red         square
     object1              red         circle
     object1              red         octagon
     object1              yellow      square
     object1              yellow      circle
     object1              yellow      octagon
     object1              green       square
     object1              green       circle
     object1              green       octagon 
     object2              red         square
     object2              red         circle
     object2              red         octagon
     object2              yellow      square
     object2              yellow      circle
     object2              yellow      octagon
     object2              green       square
     object2              green       circle
     object2              green       octagon
     etc......

谢谢

3 个答案:

答案 0 :(得分:1)

您可以尝试使用CROSS JOINDISTINCT

SELECT t2.[Object ID],t1.*
FROM 
    (SELECT DISTINCT Color,Shape FROM T1) t1
CROSS JOIN
    (SELECT DISTINCT [Object ID] FROM T2) t2

sqlfiddle

答案 1 :(得分:1)

我想您也可以根据您提到的输出,以此方式插入缺失值。

create table color (colors varchar(10), Shape varchar(10))
insert into color values  
('red'       ,  'square'  )
,('red'       ,  'circle'  )
,('red'       ,  'octagon' )
,('yellow'    ,  'square'  )
,('yellow'    ,  'circle'  )
,('yellow'    ,  'octagon' )
,('green'     ,  'square'  )
,('green'     ,  'circle'  )
,('green'     ,  'octagon' ) 


create table objectsnew (objectID varchar(20), Shape varchar(10),colors varchar(10))
insert into objectsnew values
 ('object1' ,  'square'  ,  'green'  )
,('object1' ,  'square'  ,  'red'    )
,('object1' ,  'octagon' ,  'yellow' )
,('object1' ,  'circle'  ,  'green'  )
,('object2' ,  'circle'  ,  'red'    )
,('object2' ,  'square'  ,  'yellow' )
,('object3' ,  'square'  ,  'red'    )
,('object3' ,  'circle'  ,  'red'    )
,('object3' ,  'square'  ,  'yellow' )
,('object3' ,  'square'  ,  'yellow' )
,('object3' ,  'octagon' ,  'green'  )
,('object4' ,  'circle'  ,  'red'    )

-使用此命令可确保您仅插入每个对象ID的唯一组合。

insert into objectsnew (objectID, Shape, colors) 

select o.objectID,  c.Shape, o.colors  from color c 
join objectsnew o on c.colors = o.colors 
except 
select objectID, Shape, colors   from objectsnew 
order by o.objectID  , o.colors

输出

 objectID   Shape   colors
 object1    octagon green
 object1    circle  green
 object1    square  green
 object1    square  red
 object1    octagon red
 object1    circle  red
 object1    circle  yellow
 object1    square  yellow
 object1    octagon yellow
 Etc.....

答案 2 :(得分:1)

以下将生成要插入的行:

select cs.color, cs.shape, oi.objectID
from colorshape cs cross join
     (select distinct o.objectID from objects o) oi
where not exists (select 1
                  from objects o
                  where o.color = cs.color and
                        o.shape = cs.shape and
                        o.objectId = oi.objectId
                 );

您可以在此之前添加insert,以将其插入表格中。