Solved

insert query

Posted on 2013-02-01
7
445 Views
Last Modified: 2013-02-08
--I've an existing table with data as below.
CREATE TABLE dba.t_states(state_id int,ev_id int,state_name varchar(25))
INSERT INTO  dba.t_states Values(2,1674,'VIP')
INSERT INTO  dba.t_states Values(4,1674,'Junior')
INSERT INTO  dba.t_states Values(8,1674,'Senior')
INSERT INTO  dba.t_states Values(1,1680,'C1')
INSERT INTO  dba.t_states Values(5,1680,'C2')
INSERT INTO  dba.t_states Values(7,1680,'C3')
SELECT * FROM dba.t_states
--I've this another table below where, in qual_id column I need to insert 1 for 1 event, 2 for other event ans so on
--I need a query which should display data as shown below.
CREATE TABLE dba.t_qual (qual_id int,state_id int)
INSERT INTO dba.t_qual VALUES(1,2)-- so for event 1674, qual_id =1 and state_id =2
INSERT INTO dba.t_qual VALUES(1,4)-- so for event 1674, qual_id =1 and state_id =4
INSERT INTO dba.t_qual VALUES(1,6)-- so for event 1674, qual_id =1 and state_id =4
-------------------------------------------------------------------------------------------------------
INSERT INTO dba.t_qual VALUES(2,1)-- so for event 1680, qual_id =2 and state_id =1
INSERT INTO dba.t_qual VALUES(2,5)-- so for event 1680, qual_id =2 and state_id =5
INSERT INTO dba.t_qual VALUES(2,7)-- so for event 1680, qual_id =2 and state_id =7
0
Comment
Question by:cottage125
7 Comments
 
LVL 26

Expert Comment

by:wilcoxon
ID: 38845024
If I understand what you want then this should do it:

select s.ev_id, q.qual_id, q.state_id
from dba.t_states s, dba.t_qual q
where s.state_id = q.state_id

Open in new window


If this is not what you want, please explain what you are looking for more clearly.
0
 
LVL 10

Expert Comment

by:Monica P
ID: 38846491
you want insert statement to be generated dynamically?
0
 
LVL 32

Expert Comment

by:awking00
ID: 38846704
create table dba.t_qual(qual_id, state_id) as
select dense_rank() over (order by ev_id) as qual_id, state_id
from dba.t_states;
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 32

Expert Comment

by:awking00
ID: 38846705
create table dba.t_qual(qual_id, state_id) as
select dense_rank() over (order by ev_id) as qual_id, state_id
from dba.t_states;
0
 

Author Comment

by:cottage125
ID: 38850938
I want insert statement which has some sort of logic so that it will insert qual_id =1 for any specific event.
For other event qual_id should be different than 1. I dont mind adding one more column in t_qual table if needed.
0
 
LVL 32

Accepted Solution

by:
awking00 earned 500 total points
ID: 38851536
Perhaps I still don't understand what you need. This will insert a 1 for the first (numerically) ev_id into the qual_id, a 2 for the second ev_id into the qual_id, etc.
insert into dba.t_qual(qual_id, state_id)
select dense_rank() over (order by ev_id) as qual_id, state_id
from dba.t_states;

SQL> select * from t_states;
  STATE_ID      EV_ID STATED_NA
---------- ---------- ---------
         2       1674 VIP
         4       1674 Junior
         8       1674 Senior
         1       1680 C1
         5       1680 C2
         7       1680 C3

SQL> select dense_rank() over (order by ev_id) qual_id, state_id
  2  from t_states;
QUAL_ID   STATE_ID
----------    ----------
         1          2
         1          4
         1          8
         2          1
         2          5
         2          7
0
 

Author Comment

by:cottage125
ID: 38868502
okay thanks. I think thats what i need.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

830 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