[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Insert into table from same table

Posted on 2004-03-22
7
Medium Priority
?
1,704 Views
Last Modified: 2012-06-21
I have the following values in the same table, "things". And I want to add to this table the fields where run_id = '2' so that they are in run_id = '3'

"things table" before
run_id      column2      column3
2      horse      ion
2      joke      war
2      dog      cat
3      pig      computer
3      lost      dead
3      dune      funk

"things" table after executing sql:
run_id      column2      column3
2      horse      ion
2      joke      war
2      dog      cat
3      pig      computer
3      lost      dead
3      dune      funk
3      horse      ion
3      joke      war
3      dog      cat

What is the SQL to do this?
0
Comment
Question by:chaoticz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 10650327
What database are you using, and how is the field run_id defined?

AW
0
 
LVL 1

Expert Comment

by:dazzled1
ID: 10650464
Something along the lines of

UPDATE table_name set run_id = 3 WHERE run_id = 2


Cheers :)
0
 
LVL 6

Expert Comment

by:musdu
ID: 10650470
Hi,

insert into things
  select run_id+1, column2, column3 where run_id=2;

regards.
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 6

Accepted Solution

by:
vanekl earned 900 total points
ID: 10650481
you could try this,

insert into things (run_id,column2,column3) select 3, column2, column3 from things where run_id = 2;
0
 
LVL 1

Author Comment

by:chaoticz
ID: 10650520
I am using MSSQL 2000

run_id = int(4) primary key
column2 = int(4) primary key

before:
run_id    column2    column3
2      1      horse
2      2      home
2      3      car
3      4      apple
3      5      peach
3      6      grape

after:
run_id    column2    column3
2      1      horse
2      2      home
2      3      car
3      1      horse
3      2      home
3      3      car
3      4      apple
3      5      peach
3      6      grape
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 10650888
so run_id is NOT set by a Sequence?

you have introduced quite a change with the column2 requirement that appears to require that the NEW rows have a LOWER Column2 value than the existing Column2 values for Run_id = 3, additionally requiring the the existing rows for run_id =3 have their values for column2 incremented, as well.  That GREATLY complicates the problem.

AW
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 10650898
insert into Table
 (run_id,Column2,column3)
 Select 3,A.Column2,A.column3
    from Table as A
   Where A.run_id = 2
       and not exists (select B.run_id
                                from Table as B
                               Where B.run_id = 3
                                    and b.column2 = a.column2
                                    and b.column3 = a.column3)

assuming that you don't want duplicates
and that order of column2, column3 values isn't
important

hth  
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Suggested Courses

656 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