T-SQL query to insert missing values in a table based on 2 columns combination criteria
I have the following SQL Server 2016 table that holds all possible combination of 3 colors and 3 shapes:
Color Shape
---------------------
red square
red circle
red octagon
yellow square
yellow circle
yellow octagon
green square
green circle
green octagon
I have the following data table that contains "objects" as follows (there are no duplicates):
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
As you can see there are some "gaps" in terms of shape+color combination for each object.
What I would like to achieve is to insert any missing combination of shape+color for each [object] record. Desired output for object1 and object2 would be for example:
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......
Thank you
sql
add a comment |
I have the following SQL Server 2016 table that holds all possible combination of 3 colors and 3 shapes:
Color Shape
---------------------
red square
red circle
red octagon
yellow square
yellow circle
yellow octagon
green square
green circle
green octagon
I have the following data table that contains "objects" as follows (there are no duplicates):
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
As you can see there are some "gaps" in terms of shape+color combination for each object.
What I would like to achieve is to insert any missing combination of shape+color for each [object] record. Desired output for object1 and object2 would be for example:
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......
Thank you
sql
add a comment |
I have the following SQL Server 2016 table that holds all possible combination of 3 colors and 3 shapes:
Color Shape
---------------------
red square
red circle
red octagon
yellow square
yellow circle
yellow octagon
green square
green circle
green octagon
I have the following data table that contains "objects" as follows (there are no duplicates):
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
As you can see there are some "gaps" in terms of shape+color combination for each object.
What I would like to achieve is to insert any missing combination of shape+color for each [object] record. Desired output for object1 and object2 would be for example:
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......
Thank you
sql
I have the following SQL Server 2016 table that holds all possible combination of 3 colors and 3 shapes:
Color Shape
---------------------
red square
red circle
red octagon
yellow square
yellow circle
yellow octagon
green square
green circle
green octagon
I have the following data table that contains "objects" as follows (there are no duplicates):
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
As you can see there are some "gaps" in terms of shape+color combination for each object.
What I would like to achieve is to insert any missing combination of shape+color for each [object] record. Desired output for object1 and object2 would be for example:
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......
Thank you
sql
sql
edited Nov 10 at 7:27
marc_s
570k12811021250
570k12811021250
asked Nov 10 at 1:17
User 123
205
205
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
I think you can use this to insert your missing values in this way based on the output you have mentioned as well.
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' )
--Use this to make sure that you are inserting only unique combination for each object 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
Output
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.....
add a comment |
You can try to use CROSS JOIN and DISTINCT
SELECT t2.[Object ID],t1.*
FROM
(SELECT DISTINCT Color,Shape FROM T1) t1
CROSS JOIN
(SELECT DISTINCT [Object ID] FROM T2) t2
sqlfiddle
add a comment |
The following generates the rows to be inserted:
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
);
You can add an insert before this to insert these into the table.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235201%2ft-sql-query-to-insert-missing-values-in-a-table-based-on-2-columns-combination-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think you can use this to insert your missing values in this way based on the output you have mentioned as well.
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' )
--Use this to make sure that you are inserting only unique combination for each object 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
Output
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.....
add a comment |
I think you can use this to insert your missing values in this way based on the output you have mentioned as well.
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' )
--Use this to make sure that you are inserting only unique combination for each object 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
Output
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.....
add a comment |
I think you can use this to insert your missing values in this way based on the output you have mentioned as well.
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' )
--Use this to make sure that you are inserting only unique combination for each object 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
Output
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.....
I think you can use this to insert your missing values in this way based on the output you have mentioned as well.
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' )
--Use this to make sure that you are inserting only unique combination for each object 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
Output
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.....
answered Nov 10 at 2:07
Avi
384110
384110
add a comment |
add a comment |
You can try to use CROSS JOIN and DISTINCT
SELECT t2.[Object ID],t1.*
FROM
(SELECT DISTINCT Color,Shape FROM T1) t1
CROSS JOIN
(SELECT DISTINCT [Object ID] FROM T2) t2
sqlfiddle
add a comment |
You can try to use CROSS JOIN and DISTINCT
SELECT t2.[Object ID],t1.*
FROM
(SELECT DISTINCT Color,Shape FROM T1) t1
CROSS JOIN
(SELECT DISTINCT [Object ID] FROM T2) t2
sqlfiddle
add a comment |
You can try to use CROSS JOIN and DISTINCT
SELECT t2.[Object ID],t1.*
FROM
(SELECT DISTINCT Color,Shape FROM T1) t1
CROSS JOIN
(SELECT DISTINCT [Object ID] FROM T2) t2
sqlfiddle
You can try to use CROSS JOIN and DISTINCT
SELECT t2.[Object ID],t1.*
FROM
(SELECT DISTINCT Color,Shape FROM T1) t1
CROSS JOIN
(SELECT DISTINCT [Object ID] FROM T2) t2
sqlfiddle
answered Nov 10 at 1:56
D-Shih
25.4k61431
25.4k61431
add a comment |
add a comment |
The following generates the rows to be inserted:
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
);
You can add an insert before this to insert these into the table.
add a comment |
The following generates the rows to be inserted:
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
);
You can add an insert before this to insert these into the table.
add a comment |
The following generates the rows to be inserted:
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
);
You can add an insert before this to insert these into the table.
The following generates the rows to be inserted:
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
);
You can add an insert before this to insert these into the table.
answered Nov 10 at 12:48
Gordon Linoff
756k35291399
756k35291399
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235201%2ft-sql-query-to-insert-missing-values-in-a-table-based-on-2-columns-combination-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown