Home » SQL & PL/SQL » SQL & PL/SQL » Same serial for a group (Oracle 11g)
Same serial for a group [message #639405] Mon, 06 July 2015 15:57 Go to next message
vspn
Messages: 10
Registered: July 2015
Junior Member
I have data as below in the table.

Facility Store Stop
620592 S0722 1
620578 S0308 2
620578 S0631 3
620590 S0678 4
620590 S0444 5
620590 S0237 6
I need Sequence number for these record as

Facility Stop Sequence
620592 1 1
620578 2 2
620578 3 2
620590 4 3
620590 5 3
620590 6 3
Here Sequence should be based on group by Facility and order by Stop. Please help in creating sql query for this.
Re: Same serial for a group [message #639406 is a reply to message #639405] Mon, 06 July 2015 15:58 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Welcome to this forum.

Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/ and read http://www.orafaq.com/forum/t/174502/
Re: Same serial for a group [message #639417 is a reply to message #639405] Tue, 07 July 2015 00:02 Go to previous messageGo to next message
Michel Cadot
Messages: 68648
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

ROW_NUMBER and COUNT analytic functions will do the trick.

Re: Same serial for a group [message #639489 is a reply to message #639417] Wed, 08 July 2015 10:16 Go to previous messageGo to next message
vspn
Messages: 10
Registered: July 2015
Junior Member
I tried ROW_NUMBER it is not giving expected results.

Here is the output i got when I used row_number and Dense_Rank
Row_number() OVER ( PARTITION BY FACILITY_NOTE.FACILITY_ID ORDER BY STOP.STOP_SEQ ) AS SEQ,

Facility Store Sequence
620592 S0722 1
620578 S0308 1
620578 S0631 2

Each group didn't have same number..Ideal output should be as below
Facility Store Sequence
620592 S0722 1
620578 S0308 2
620578 S0631 2

Re: Same serial for a group [message #639490 is a reply to message #639489] Wed, 08 July 2015 10:26 Go to previous messageGo to next message
Michel Cadot
Messages: 68648
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

You do not use the hints, ALL the hints appropriately.

Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.

With any SQL or PL/SQL question, please, Post a working Test case: create table (including all constraints) and insert statements along with the result you want with these data then we will be able work with your table and data. Explain with words and sentences the rules that lead to this result.

[Updated on: Wed, 08 July 2015 10:28]

Report message to a moderator

Re: Same serial for a group [message #639538 is a reply to message #639417] Thu, 09 July 2015 06:07 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Michel Cadot wrote on Tue, 07 July 2015 10:32

ROW_NUMBER and COUNT analytic functions will do the trick.


Only COUNT() analytic function would suffice.
Re: Same serial for a group [message #639539 is a reply to message #639489] Thu, 09 July 2015 06:10 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
vspn wrote on Wed, 08 July 2015 20:46
I tried ROW_NUMBER it is not giving expected results.


Use COUNT().

SQL> with data(Facility, Store, Stop ) as(
  2  select 620592, 'S0722', 1 from dual union all
  3  select 620578, 'S0308', 2 from dual union all
  4  select 620578, 'S0631', 3 from dual union all
  5  select 620590, 'S0678', 4 from dual union all
  6  select 620590, 'S0444', 5 from dual union all
  7  select 620590, 'S0237', 6  from dual
  8  )
  9  select t.*, count(facility) over(partition by facility) rn
 10  from data t
 11  order by stop;

  FACILITY STORE       STOP         RN
---------- ----- ---------- ----------
    620592 S0722          1          1
    620578 S0308          2          2
    620578 S0631          3          2
    620590 S0678          4          3
    620590 S0444          5          3
    620590 S0237          6          3

6 rows selected.

SQL>


And please use code tags when you post your code. Instead of copy pasting the data, you should provide the create and insert statements, or at least the data set using WITH clause just like I did. Without this, the entire work needs to be done by the one who wants to help you, which could be annoying. Please do this in future, welcome to the forum!


Regards,
Lalit
Re: Same serial for a group [message #639540 is a reply to message #639538] Thu, 09 July 2015 06:13 Go to previous messageGo to next message
Michel Cadot
Messages: 68648
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Lalit Kumar B wrote on Thu, 09 July 2015 13:07
Michel Cadot wrote on Tue, 07 July 2015 10:32

ROW_NUMBER and COUNT analytic functions will do the trick.


Only COUNT() analytic function would suffice.


Yes, I didn't see STOP is in the source table (and even if it was not, we can do it with 2 COUNT).

[Updated on: Thu, 09 July 2015 06:14]

Report message to a moderator

Re: Same serial for a group [message #639542 is a reply to message #639539] Thu, 09 July 2015 06:15 Go to previous messageGo to next message
Michel Cadot
Messages: 68648
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Lalit Kumar B wrote on Thu, 09 July 2015 13:10
vspn wrote on Wed, 08 July 2015 20:46
I tried ROW_NUMBER it is not giving expected results.


Use COUNT().
...


Why didn't you let OP try to do it by himself?

Re: Same serial for a group [message #639543 is a reply to message #639542] Thu, 09 July 2015 06:25 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
I didn't want to ruin his learning. I see he has shown what he tried(ROW_NUMBER), and I see you gave him *hints*. I thought he is getting confused, and now he might play around with the solution and understand why his query didn't work. Also, as he was not posting the test case, I thought of showing him how to build the data set which could be used by others using WITH clause. I just gave him one chance, if he continues not to follow the guidelines(which I don't expect him to do) then obviously I won't help him at all.
Re: Same serial for a group [message #639544 is a reply to message #639543] Thu, 09 July 2015 06:29 Go to previous messageGo to next message
Michel Cadot
Messages: 68648
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
I didn't want to ruin his learning. I see he has shown what he tried(ROW_NUMBER),


I mentioned COUNT and he did not try it as I answered him and you emphasized this by your own previous answer.
So, yes, you did ruin his learning and your post just seems to be a "see, I can do it".

Re: Same serial for a group [message #639548 is a reply to message #639544] Thu, 09 July 2015 08:19 Go to previous messageGo to next message
Michel Cadot
Messages: 68648
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Note: what about
SQL> with data(Facility, Store, Stop ) as(
  2  select 620592, 'S0722', 1 from dual union all
  3  select 620578, 'S0308', 2 from dual union all
  4  select 620578, 'S0631', 3 from dual union all
  5  select 620590, 'S0678', 4 from dual union all
  6  select 620590, 'S0444', 5 from dual union all
  7  select 620590, 'S0237', 6  from dual union all
  8  select null, 'S0238', 7  from dual union all
  9  select null, 'S0239', 8  from dual
 10  )
 11  select t.*, count(facility) over(partition by facility) rn
 12  from data t
 13  order by stop;
  FACILITY STORE       STOP         RN
---------- ----- ---------- ----------
    620592 S0722          1          1
    620578 S0308          2          2
    620578 S0631          3          2
    620590 S0678          4          3
    620590 S0444          5          3
    620590 S0237          6          3
           S0238          7          0
           S0239          8          0

Re: Same serial for a group [message #639553 is a reply to message #639544] Thu, 09 July 2015 12:03 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Michel Cadot wrote on Thu, 09 July 2015 16:59
your post just seems to be a "see, I can do it".


OK.
Re: Same serial for a group [message #639554 is a reply to message #639539] Thu, 09 July 2015 13:30 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
Lalit Kumar B wrote on Thu, 09 July 2015 07:10
Use COUNT()


And what has COUNT to do with this? Pure data magic:

with data(Facility, Store, Stop ) as(
select 620592, 'S0722', 1 from dual union all
select 620592, 'S0722', 2 from dual union all
select 620592, 'S0722', 3 from dual union all
select 620578, 'S0308', 4 from dual union all
select 620578, 'S0308', 5 from dual union all
select 620578, 'S0631', 6 from dual union all
select 620590, 'S0678', 7 from dual union all
select 620590, 'S0444', 8 from dual union all
select 620590, 'S0237', 9  from dual
)
select t.*, count(facility) over(partition by facility) rn
from data t
order by stop;

  FACILITY STORE       STOP         RN
---------- ----- ---------- ----------
    620592 S0722          1          3
    620592 S0722          2          3
    620592 S0722          3          3
    620578 S0308          4          3
    620578 S0308          5          3
    620578 S0631          6          3
    620590 S0678          7          3
    620590 S0444          8          3
    620590 S0237          9          3

9 rows selected.

SQL> 


My understanding OP wants "Here Sequence should be based on group by Facility and order by Stop":

with data(Facility, Store, Stop ) as(
select 620592, 'S0722', 1 from dual union all
select 620592, 'S0722', 2 from dual union all
select 620592, 'S0722', 3 from dual union all
select 620578, 'S0308', 4 from dual union all
select 620578, 'S0308', 5 from dual union all
select 620578, 'S0631', 6 from dual union all
select 620590, 'S0678', 7 from dual union all
select 620590, 'S0444', 8 from dual union all
select 620590, 'S0237', 9  from dual
),
t as (
      select  d.*,
              min(stop) over(partition by facility) min_stop
        from  data d
     )
select  facility,
        store,
        stop,
        dense_rank() over(order by min_stop) sequence
  from  t
  order by stop
/

  FACILITY STORE       STOP   SEQUENCE
---------- ----- ---------- ----------
    620592 S0722          1          1
    620592 S0722          2          1
    620592 S0722          3          1
    620578 S0308          4          2
    620578 S0308          5          2
    620578 S0631          6          2
    620590 S0678          7          3
    620590 S0444          8          3
    620590 S0237          9          3

9 rows selected.

SQL> -- and with original data
SQL> with data(Facility, Store, Stop ) as(
  2  select 620592, 'S0722', 1 from dual union all
  3  select 620578, 'S0308', 2 from dual union all
  4  select 620578, 'S0631', 3 from dual union all
  5  select 620590, 'S0678', 4 from dual union all
  6  select 620590, 'S0444', 5 from dual union all
  7  select 620590, 'S0237', 6 from dual
  8  ),
  9  t as (
 10        select  d.*,
 11                min(stop) over(partition by facility) min_stop
 12          from  data d
 13       )
 14  select  facility,
 15          store,
 16          stop,
 17          dense_rank() over(order by min_stop) sequence
 18    from  t
 19    order by stop
 20  /

  FACILITY STORE       STOP   SEQUENCE
---------- ----- ---------- ----------
    620592 S0722          1          1
    620578 S0308          2          2
    620578 S0631          3          2
    620590 S0678          4          3
    620590 S0444          5          3
    620590 S0237          6          3

6 rows selected.

SQL> 


SY.
Re: Same serial for a group [message #639555 is a reply to message #639554] Thu, 09 July 2015 14:05 Go to previous messageGo to next message
Michel Cadot
Messages: 68648
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
And what has COUNT to do with this?


Just that "dense_rank() over(order by [min(stop) over(partition by facility)])" is [almost] logically equivalent to "count(distinct Facility) over (order by Facility)" (syntax that Oracle does not support but you can do the same thing with a simple dense_rank).

I don't know if the "order by Stop" means "order by min(stop)".

[Updated on: Thu, 09 July 2015 14:29]

Report message to a moderator

Re: Same serial for a group [message #639556 is a reply to message #639555] Thu, 09 July 2015 14:16 Go to previous messageGo to next message
vspn
Messages: 10
Registered: July 2015
Junior Member
I didnt post the code as my query involves many other tables. Anyway I am posting here the query and the result..

SELECT  DISTINCT 
        FACILITY_NOTE.FACILITY_ID,
        STOP.FACILITY_ALIAS_ID AS STOP_DESTINATION,
        Row_number() OVER ( PARTITION BY FACILITY_NOTE.FACILITY_ID  ORDER BY STOP.STOP_SEQ ) AS SEQ,
        ROUND(SUM(CASE
                    WHEN COALESCE(LPN.WEIGHT,0) = 0
                      THEN COALESCE(LPN.ESTIMATED_WEIGHT,0)
                    ELSE COALESCE(LPN.WEIGHT,0)
                 END)) AS WEIGHT,
        STOP.DEPARTURE_START_DTTM AS DELV_DATE,
        '1' AS MASTER_BOL

FROM WMS.SHIPMENT
INNER JOIN WMS.STOP
    ON STOP.SHIPMENT_ID = SHIPMENT.SHIPMENT_ID
INNER JOIN WMS.FACILITY_NOTE
  ON FACILITY_NOTE.NOTE = STOP.FACILITY_ALIAS_ID
  AND FACILITY_NOTE.MARK_FOR_DELETION = 0
LEFT JOIN WMS.FACILITY_ALIAS
  ON FACILITY_ALIAS.FACILITY_ID = STOP.FACILITY_ID
LEFT JOIN WMS.FACILITY
  ON FACILITY.FACILITY_ID = STOP.FACILITY_ID
INNER JOIN WMS.STOP_ACTION
    ON STOP_ACTION.STOP_SEQ = STOP.STOP_SEQ
    AND STOP_ACTION.SHIPMENT_ID = STOP.SHIPMENT_ID
    AND STOP_ACTION.ACTION_TYPE IN ('DL','DA')
INNER JOIN WMS.LPN
    ON  LPN.SHIPMENT_ID = SHIPMENT.SHIPMENT_ID
	AND LPN.LPN_TYPE = 1
	AND LPN.INBOUND_OUTBOUND_INDICATOR ='O'
	AND LPN.LPN_FACILITY_STATUS <= 90
INNER JOIN WMS.ORDERS
    ON ORDERS.ORDER_ID = LPN.ORDER_ID
    AND ORDERS.IS_CANCELLED = 0
    AND ORDERS.D_FACILITY_ID = STOP.FACILITY_ID
LEFT JOIN WMS.COMPANY_PARAMETER
    ON COMPANY_PARAMETER.TC_COMPANY_ID = SHIPMENT.TC_COMPANY_ID
    AND COMPANY_PARAMETER.PARAM_DEF_ID ='base_weight_uom'
LEFT JOIN WMS.SIZE_UOM
    ON TRIM(CAST(SIZE_UOM.SIZE_UOM_ID AS CHAR(10))) = COMPANY_PARAMETER.PARAM_VALUE
    AND SIZE_UOM.TC_COMPANY_ID = COMPANY_PARAMETER.TC_COMPANY_ID
LEFT JOIN
    WMS.COMPANY_PARAMETER COMPANY_PARAM
    ON	COMPANY_PARAM.PARAM_DEF_ID = 'BOL_TYPE'
    AND	COMPANY_PARAM.PARAM_GROUP_ID = 'COM_TEPE'
    AND	COMPANY_PARAM.TC_COMPANY_ID = SHIPMENT.TC_COMPANY_ID
WHERE
    '1986' = CASE TRIM(CAST('0' AS CHAR(1)))
                        WHEN '0' THEN TRIM(CAST(SHIPMENT.SHIPMENT_ID AS CHAR(20)))
                        WHEN '1' THEN TRIM(CAST(STOP.SHIPMENT_ID AS CHAR(20)))
                     END
    AND
    '0' = CASE TRIM(CAST('0' AS CHAR(1)))
                     WHEN '0' THEN '0'
                     WHEN '1' THEN TRIM(CAST(STOP.STOP_SEQ AS CHAR(10)))
                   END
AND '0' = '0'
GROUP BY
      FACILITY_NOTE.FACILITY_ID,
      STOP.FACILITY_ALIAS_ID,
      STOP.STOP_SEQ,
      STOP.DEPARTURE_START_DTTM
Order by FACILITY_NOTE.FACILITY_ID;



Output:
-----------------
Facility_id Stop_Destination  Seq  Weight  Delv_Date  Master_BOL
620592	       S0722	       1    920	   08-MAY-15	1
620578	       S0308	       1    2443   12-MAY-15	1
620578	       S0631	       2    460	   12-MAY-15	1


Re: Same serial for a group [message #639557 is a reply to message #639556] Thu, 09 July 2015 14:20 Go to previous messageGo to next message
Michel Cadot
Messages: 68648
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Is this the correct result because it has nothing with the original question where same facility_id has the same sequence?

Re: Same serial for a group [message #639558 is a reply to message #639555] Thu, 09 July 2015 15:36 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
Michel Cadot wrote on Thu, 09 July 2015 15:05

Quote:
And what has COUNT to do with this?


Just that "dense_rank() over(order by [min(stop) over(partition by facility)])" is [almost] logically equivalent to "count(distinct Facility) over (order by Facility)" (syntax that Oracle does not support but you can do the same thing with a simple dense_rank).

I don't know if the "order by Stop" means "order by min(stop)".



I see no equivalence. And my example shows that. Count simply returns how many rows have same facility value and if multiple facilities have same number of rows they will be assigned same serial number - obviously not what OP asked for. We could assign serial number solely based on facility dense rank, but it will not fulfill "order by Stop" part w3hic I understand as facility with stop 1 is assigned serial number 1, facility with lowest stop not taken by facility with serial number 1 is assigned serial number 2, etc. In sample provided all facilities have consecutive stop values, but OP never stated that's the rule. Compare results:

SQL> with data(Facility, Store, Stop ) as(
  2  select 620592, 'S0722', 1 from dual union all
  3  select 620578, 'S0308', 6 from dual union all
  4  select 620578, 'S0631', 3 from dual union all
  5  select 620590, 'S0678', 4 from dual union all
  6  select 620590, 'S0444', 5 from dual union all
  7  select 620590, 'S0237', 2 from dual
  8  )
  9  select t.*, count(facility) over(partition by facility) rn
 10  from data t
 11  order by rn
 12  /

  FACILITY STORE       STOP         RN
---------- ----- ---------- ----------
    620592 S0722          1          1
    620578 S0308          6          2
    620578 S0631          3          2
    620590 S0444          5          3
    620590 S0678          4          3
    620590 S0237          2          3

6 rows selected.

SQL> with data(Facility, Store, Stop ) as(
  2  select 620592, 'S0722', 1 from dual union all
  3  select 620578, 'S0308', 6 from dual union all
  4  select 620578, 'S0631', 3 from dual union all
  5  select 620590, 'S0678', 4 from dual union all
  6  select 620590, 'S0444', 5 from dual union all
  7  select 620590, 'S0237', 2 from dual
  8  )
  9  select  facility,
 10          store,
 11          stop,
 12          dense_rank() over(order by Facility) sequence
 13    from  data
 14    order by sequence
 15  /

  FACILITY STORE       STOP   SEQUENCE
---------- ----- ---------- ----------
    620578 S0631          3          1
    620578 S0308          6          1
    620590 S0237          2          2
    620590 S0678          4          2
    620590 S0444          5          2
    620592 S0722          1          3

6 rows selected.

SQL> 
SQL> with data(Facility, Store, Stop ) as(
  2  select 620592, 'S0722', 1 from dual union all
  3  select 620578, 'S0308', 6 from dual union all
  4  select 620578, 'S0631', 3 from dual union all
  5  select 620590, 'S0678', 4 from dual union all
  6  select 620590, 'S0444', 5 from dual union all
  7  select 620590, 'S0237', 2 from dual
  8  )
  9  ,
 10  t as (
 11        select  d.*,
 12                min(stop) over(partition by facility) min_stop
 13          from  data d
 14       )
 15  select  facility,
 16          store,
 17          stop,
 18          dense_rank() over(order by min_stop) sequence
 19    from  t
 20    order by sequence
 21  /

  FACILITY STORE       STOP   SEQUENCE
---------- ----- ---------- ----------
    620592 S0722          1          1
    620590 S0237          2          2
    620590 S0444          5          2
    620590 S0678          4          2
    620578 S0631          3          3
    620578 S0308          6          3

6 rows selected.

SQL>  


SY.
Re: Same serial for a group [message #639563 is a reply to message #639558] Fri, 10 July 2015 00:33 Go to previous messageGo to next message
Michel Cadot
Messages: 68648
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
obviously not what OP asked for


But what he asked is obviously not clear.
You focused on order, I focused on "group number" because order is not clear (what does mean "order by stop" when there are several stops in each group, min? max? avg? median? sum? product? sum of squares of differences with the min?...).

And if OP's latest result is correct, we are both wrong. Smile

[Updated on: Fri, 10 July 2015 00:42]

Report message to a moderator

Re: Same serial for a group [message #639580 is a reply to message #639557] Fri, 10 July 2015 10:43 Go to previous messageGo to next message
vspn
Messages: 10
Registered: July 2015
Junior Member
Out put I posted was incorrect. I wanted to show that I am not getting expected results with Row_Number(). I should get same Seq number for a group ordered by Stop as I asked initially.
Re: Same serial for a group [message #639581 is a reply to message #639563] Fri, 10 July 2015 10:45 Go to previous messageGo to next message
vspn
Messages: 10
Registered: July 2015
Junior Member
I need the same Seq number for a group by facility_id and ordered by min(Stop)
Re: Same serial for a group [message #639582 is a reply to message #639580] Fri, 10 July 2015 10:47 Go to previous messageGo to next message
Michel Cadot
Messages: 68648
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
vspn wrote on Fri, 10 July 2015 17:43
Out put I posted was incorrect. I wanted to show that I am not getting expected results with Row_Number(). I should get same Seq number for a group ordered by Stop as I asked initially.


And as I said "order by stop" is not a clear specification, so tell us what you mean by this.

Re: Same serial for a group [message #639583 is a reply to message #639581] Fri, 10 July 2015 10:47 Go to previous messageGo to next message
Michel Cadot
Messages: 68648
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
vspn wrote on Fri, 10 July 2015 17:45
I need the same Seq number for a group by facility_id and ordered by min(Stop)


Ah OK, now it is deterministic, see Solomon's query.

Re: Same serial for a group [message #639722 is a reply to message #639583] Tue, 14 July 2015 11:36 Go to previous messageGo to next message
vspn
Messages: 10
Registered: July 2015
Junior Member
In Solomon's queries, all the outputs shown are not as expected. Stops are always in sequence as given in my first post. So I need final serial or Sequence grouped by facility and ordered by min of Stop.

Let me tell the scenario for which I am writing the query.
We have many retail routes defined in the system, let's say it is Nashville. In this Nashville route, there are some stores/stops defined. Now the truck leaves from distribution center to deliver the goods to stores. Before even going to store, they go to crossdock location and from there on it goes to individual stores in the order defined in the Nashville route.
Say there are 5 stores/stops in the Nashville route. Each of these store belong to crossdock and multiple stores may have same crossdock.
So in the example shared there are 3 crossdock locations (620592, 620578, 620590) and 6 stores(S0722,S0308,S0631,S0678,S0444,S0237). Order they follow is fixed(i.e Stop is always fixed and always in sequence)
Re: Same serial for a group [message #639723 is a reply to message #639722] Tue, 14 July 2015 11:46 Go to previous messageGo to next message
Michel Cadot
Messages: 68648
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
. So I need final serial or Sequence grouped by facility and ordered by min of Stop.


Show us the difference between what you expect Solomon's output.

Re: Same serial for a group [message #639727 is a reply to message #639723] Tue, 14 July 2015 14:34 Go to previous messageGo to next message
vspn
Messages: 10
Registered: July 2015
Junior Member
output 1:
[code]
FACILITY STORE STOP RN
---------- ----- ---------- ----------
620592 S0722 1 1
620578 S0308 6 2
620578 S0631 3 2
620590 S0444 5 3
620590 S0678 4 3
620590 S0237 2 3
[\code]

Here the Stop 2 was given RN as 3. It should be 2. Similarly for others. After stop 1, there should be stop 2 and not 6. They should always be in sequence as I gave in my first post.
Expected output is:
[code]
FACILITY STORE STOP RN
---------- ----- ---------- ----------
620592 S0722 1 1
620578 S0308 2 2
620578 S0631 3 2
620590 S0444 4 3
620590 S0678 5 3
620590 S0237 6 3
[\code]
Re: Same serial for a group [message #639728 is a reply to message #639727] Tue, 14 July 2015 14:39 Go to previous messageGo to next message
vspn
Messages: 10
Registered: July 2015
Junior Member
Also in Solomon's output, Facility 620578 cannot have stops as 6 and 3. They should always follow sequence like 2,3 or 3,4 or 4,5 or 5,6. Similarly for 620590, it has 5,4,2. But it should be like 4,5,6
Re: Same serial for a group [message #639729 is a reply to message #639727] Tue, 14 July 2015 14:39 Go to previous messageGo to next message
Michel Cadot
Messages: 68648
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Note: to end code tags it is [/code] NOT [\code]. Use the "Preview" button BEFORE clicking on "Post" one.

So you want to change the original STOP column values.
A simple ROW_NUMBER will do it.

Re: Same serial for a group [message #639730 is a reply to message #639728] Tue, 14 July 2015 14:40 Go to previous messageGo to next message
Michel Cadot
Messages: 68648
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
vspn wrote on Tue, 14 July 2015 21:39
Also in Solomon's output, Facility 620578 cannot have stops as 6 and 3. They should always follow sequence like 2,3 or 3,4 or 4,5 or 5,6. Similarly for 620590, it has 5,4,2. But it should be like 4,5,6


See my previous answer posted at the same than yours.
And I add, as several rows have the same SEQUENCE/RN, that is min(stop) you have to add another rule to order these rows.

[Updated on: Tue, 14 July 2015 14:42]

Report message to a moderator

Re: Same serial for a group [message #639731 is a reply to message #639729] Tue, 14 July 2015 14:58 Go to previous messageGo to next message
vspn
Messages: 10
Registered: July 2015
Junior Member
My table already has Stops in serial. My challenge here is to generate sequence number which is same for a group. Let me ask you in simple way. In the below data, column1 is already available in the database table and I need to generate Serial# as shown.

Expected Output:
column1   serial#
-------------------
a          1
a          1
b          2
c          3
c          3
c          3
c          3


If I use Row_number, I get output as
column1   serial#
-------------------
a          1
a          2
b          3
c          4
c          5
c          6
c          7


Please suggest on how to get same number for a group of column1
Re: Same serial for a group [message #639736 is a reply to message #639731] Tue, 14 July 2015 23:59 Go to previous message
Michel Cadot
Messages: 68648
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Try to understand what has been posted, all is there.

Previous Topic: Disabling unique constraints and data warehousing query
Next Topic: Merge using row order
Goto Forum:
  


Current Time: Wed May 08 01:29:57 CDT 2024