Solved

Sql query help regarding repeating rows

Posted on 2006-11-11
6
1,138 Views
Last Modified: 2008-01-09
Hi,

I need help in making the sql query for the following situation, for ex:

I have a table like following

******************
SEQUENCES
******************
SEQ_ID, SUBSEQ_ID, EXTRACOLS.....
1           0                 xy.......
2           0                 xys.....
2           1                 zubyy.....
2           2                 ztu....
3           0                 uoiu....
4           0                 iuzt.....
4           1                 ztre.....
.....
.....

I need to formulate the sql query in such a way that the subseq_id for each seq_id should be considered till 5. It means, the seq_id and other extra cols are repeated till subseq_id is 5. It is not necessary that the subseq_id is always available in the sequences table. If the information of subseq_id is available in the sequences table then the query should consider it otherwise it should make a duplicate row with the seq_id for non-existing subseq_ids.

If one has more questions regarding the above, please ask.

Please advice.
Thanks.
Aks.

0
Comment
Question by:aks143
6 Comments
 
LVL 18

Expert Comment

by:Jinesh Kamdar
Comment Utility
>> otherwise it should make a duplicate row with the seq_id for non-existing subseq_ids.

I didnt quite understand ur output requirement. Can you show a sample resultset that ur expecting?
0
 

Author Comment

by:aks143
Comment Utility
The subseq_id should be changed and the complete row should be duplicated if subseq_id not available in the table. The subseq_id should be increased to max of 5.

Following is the result table should look like

******************
RESULT SEQUENCES
******************
SEQ_ID, SUBSEQ_ID, EXTRACOLS.....
1           0                 xy.......
1           1                 xy....... << REPEATED with subseq_id 1
1           2                 xy....... << REPEATED with subseq_id 2
1           3                 xy....... << REPEATED with subseq_id 3
1           4                 xy....... << REPEATED with subseq_id 4
1           5                 xy....... << REPEATED with subseq_id 5
2           0                 xys.....
2           1                 zubyy.....
2           2                 ztu....
2           3                 xys..... << REPEATED with subseq_id 3
2           4                 zubyy... << REPEATED with subseq_id 4
2           5                 ztu.... << REPEATED with subseq_id 5
3           0                 uoiu....
3           1                 xy....... << REPEATED with subseq_id 1
3           2                 xy....... << REPEATED with subseq_id 2
3           3                 xy....... << REPEATED with subseq_id 3
3           4                 xy....... << REPEATED with subseq_id 4
3           5                 xy....... << REPEATED with subseq_id 5
.....
.....

Let me know if u need further clarification.
Thanks.
Aks
0
 
LVL 3

Expert Comment

by:cdemir
Comment Utility
hi,

are you looking for GROUP BY and HAVING clauses? i think, your question is not so clear.
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.

 

Author Comment

by:aks143
Comment Utility
>>i think, your question is not so clear.

The this is, i want to repeat the rows for non-existing subseq_ids for each seq_id. As shown in the example above. Please clarify, what is not understandable. Thanks.

regards,
Aks


0
 
LVL 28

Expert Comment

by:Naveen Kumar
Comment Utility
to some extent i can understand your question but not very clear.

1) Just give me the data in the table first as you have shown while asking this question ( take only 3 or 4 records ).
2) Explain what needs to be done in little more details ( do you need in a single SQL or pl/sql proc/function is fine ).
3) Now just give the output of the results which you need ( please provide the output only for those records which you have given in step 1).

This way we can come to conclusion that you need this for sure.

Otherwise i am getting confused because '3           0                 uoiu....' this is given while asking the question but you say that you need output for this as
'3           0                 uoiu....
3           1                 xy....... << REPEATED with subseq_id 1
3           2                 xy....... << REPEATED with subseq_id 2
3           3                 xy....... << REPEATED with subseq_id 3
3           4                 xy....... << REPEATED with subseq_id 4
3           5                 xy....... << REPEATED with subseq_id 5'  --> where did this xy come for seq_id 3 because it should be 'uoiu' as you have given first right.

As  of now i can understand only that for every seq id, you need 6 records from subseq_id from 0 to 5.

Thanks
0
 
LVL 4

Accepted Solution

by:
radja7 earned 200 total points
Comment Utility
try this:

SELECT sid AS seq_id, rn AS subseq_id
,(
  SELECT g.extracols
  FROM my_table g
  WHERE g.seq_id=sid
  AND g.subseq_id=DECODE(sign(msub-rn),-1,mod((rn+1),(msub+1)),rn)
) e
FROM
(
  SELECT * FROM
  (SELECT rownum-1 rn FROM dual CONNECT BY level<=6) d
  ,(SELECT seq_id sid, MAX(m.subseq_id) msub FROM my_table m GROUP BY seq_id) m
)
,(
  SELECT t.*
  FROM my_table t
) t
WHERE subseq_id(+)=rn
and seq_id(+)=sid
order by sid,rn
;
0

Featured Post

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.

Join & Write a Comment

Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

772 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

10 Experts available now in Live!

Get 1:1 Help Now