Solved

insert query

Posted on 2013-02-01
7
402 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
Comment Utility
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
Comment Utility
you want insert statement to be generated dynamically?
0
 
LVL 31

Expert Comment

by:awking00
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 31

Expert Comment

by:awking00
Comment Utility
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
Comment Utility
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 31

Accepted Solution

by:
awking00 earned 500 total points
Comment Utility
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
Comment Utility
okay thanks. I think thats what i need.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

728 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now