Solved

insert a group of records based on another group of records

Posted on 2014-03-20
8
134 Views
Last Modified: 2014-03-22
I have 2 tables:  1 is job table (jobId, jobName) and 1 is result table (rowId, jobId, resultCode).

There can be several resultCodes for each jobId.  I have created the resultCodes for 1 jobId, but I have 200 jobIds and I need to create the same resultCodes for each one.

how can I insert the resultCodes for each jobId that doesn't have one?
0
Comment
Question by:dhenderson12
8 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39942786
A sample data set would be helpful here.

>how can I insert the resultCodes for each jobId that doesn't have one?
Also, spell out in abundantly clear language which table you wish to insert, and what values, as it isn't clear in the question.
0
 

Author Comment

by:dhenderson12
ID: 39942943
ok.

table 1:
1  job1
2  job2
3  job3
4  job4
5  job5
6  job6

table 2:
1  job1  rc1
2  job1  rc2
3  job1  rc3
4  job2  rc1
5  job2  rc2
6  job2  rc3

now I have to create rc1, rc2, and rc3 for jobs 3, 4, 5, and 6.
0
 
LVL 12

Assisted Solution

by:Harish Varghese
Harish Varghese earned 250 total points
ID: 39942950
If you have a master table for ResultCodes and you want to insert all those codes for each of the jobs in Jobs table into Results table, you may use below query (I assume RowId is an identity column):
Insert into Results (jobId, resultCode)
Select J.JobId, RC.ResultCode
From Jobs J, ResultCodes RC
Where Not Exists (Select 1 from Results R
        Where R.JobId = J.JobId And R.ResultCode = RC.ResultCode)

Open in new window

If you do not have a separate master table for Result Codes and you want to setup the result codes for all jobs using the result codes you already setup for JobId 1, then you may use below query:
Insert into Results (jobId, resultCode)
Select J.JobId, RC.ResultCode
From Jobs J, Results RC
Where RC.JobId = 1 And Not Exists (Select 1 from Results R
        Where R.JobId = J.JobId And R.ResultCode = RC.ResultCode)

Open in new window

-Harish
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 40

Accepted Solution

by:
Sharath earned 250 total points
ID: 39943589
check this.
INSERT table2 
SELECT t1.jobId, 
       t2.resultCode 
  FROM table1 t1 
       CROSS JOIN (SELECT DISTINCT resultCode 
                     FROM table2) t2 
 WHERE NOT EXISTS (SELECT 1 
                     FROM table2 t3 
                    WHERE t1.jobId = t3.jobId 
                      AND t2.resultCode = t3.resultCode) 

Open in new window

0
 

Author Comment

by:dhenderson12
ID: 39945143
I don't understand the "select 1" statement.
0
 
LVL 40

Expert Comment

by:Sharath
ID: 39945390
Without INSERT statement at line 1, did you try running the SELECT statement and get what you are expecting?
0
 

Author Comment

by:dhenderson12
ID: 39947669
yes.
0
 

Author Comment

by:dhenderson12
ID: 39947689
oh, select 1 and select * are the same.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question