Solved

insert a group of records based on another group of records

Posted on 2014-03-20
8
135 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 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