Solved

insert query

Posted on 2013-02-01
7
457 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
[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 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
Percona Monitoring and Management and Grafana

Proactive monitoring is vital to a highly-available environment. We have a quick start guide on Experts Exchange for Grafana users.

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

623 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