Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find the minimum number for a field then change all of the following numbers, if necessary 1

Status
Not open for further replies.

borisbe

Technical User
Joined
Aug 15, 2005
Messages
73
Location
US
Hey

I have data where there could be 1 pattern or multiple patterns.

set pattern
1 1
1 2
1 3
2 2
2 4
3 3

Is there a way in a case when, if or some other expression that I can write that would find the minimum pattern for a set and always make it number 1 then change the sequence if necessary so I would get:

set pattern
1 1
1 2
1 3
2 1
2 2
3 1

Thank you!
 
Sorry, Borisbe...I don't follow your specifications as they correspond to the resulting data set. Could you please clarify for us?

[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
“People may forget what you say, but they will never forget how you made them feel.
 
Sorry, I probably just didn't give enough information or didn't explain it very well. Our database has the field called set but I would say it is more of a unique identifier for a person and the pattern# corresponds to a meeting date. A person has a meeting date planned for certain date(s) but if the date(s) need to be rescheduled then the data entry person removes the pattern# row and inserts a new pattern# so that is why person #2 (set #2) has their very first pattern# as 2 instead of 1 and they must have had to change another date which had pattern#3 so that is why their next one is pattern# 4. Person #3 had to reschedule twice so that is why their initial date has pattern#3.

set# pattern# dates
1 1 1/12
1 2 2/13
1 3 2/20

2 2 2/28
2 4 3/12

3 3 2/27

Is there a way in a case when, if or some other expression in a select statement that I can write that would find the minimum pattern for a set and always make it number 1 then change the sequence if necessary so I would get:

set pattern# dates
1 1 1/12
1 2 2/13
1 3 2/20

2 1 2/28
2 2 3/12

3 1 2/27

Thanks for your help!!!
 
Much clearer. Thanks.

There are many ways to achieve your goal. Here is one of the syntactically simpler ways that I can think of:

Code:
First, to confirm the data set:

select * from sets;

      SET#   PATTERN# DATE
---------- ---------- ----
         1          1 1/12
         1          2 2/13
         1          3 2/20
         2          2 2/28
         2          4 3/12
         3          3 2/27

6 rows selected.

Second, to meet your need:

select set#
    ,row_number() over (partition by set# order by pattern#) pattern#
    ,dates
from sets;

      SET#   PATTERN# DATE
---------- ---------- ----
         1          1 1/12
         1          2 2/13
         1          3 2/20
         2          1 2/28
         2          2 3/12
         3          1 2/27

6 rows selected.

In the above solution, "row_number() over (partiion..." is one of the very handy Oracle Analytic functions.

Let us know if this resolves your need, or if you have follow-up questions.

[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
“People may forget what you say, but they will never forget how you made them feel.
 
Worked BEAUTIFULLY!!!

Thanks[2thumbsup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top