Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

insert query

Posted on 2013-02-01
7
Medium Priority
?
461 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 27

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
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…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

688 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