简体   繁体   中英

First group of values by a ordered query

I have a table that represents a group of collections ordered by a column called sequence in the group:

| GroupId | Collection | Sequence |
|---------|------------|----------|
| 002     | A.2018     | 1        |
| 002     | A.2017     | 2        |
| 003     | P.2018     | 1        |
| 003     | L.2018     | 2        |
| 003     | R.2018     | 3        |
| 003     | M.2018     | 4        |

In another table I have the customer sales by collection and segment:

| Collection | Segment | Customer | Sales  |
|------------|---------|----------|--------|
| A.2018     | 002     | C001030  | 304.30 |
| A.2017     | 002     | C001030  | 493.10 |
| L.2018     | 002     | C001030  | 232.33 |
| L.2018     | 010     | C001030  | 343.12 |
| R.2018     | 002     | C001030  | 434.23 |
| M.2018     | 002     | C001030  | 121.12 |

I want to get by GroupID, the first collection (ordered by collection sequence) that the customer has sales in the segment.

| GroupID | Collection | Segment | Customer | Sales  |
|---------|------------|---------|----------|--------|
| 002     | A.2018     | 002     | C001030  | 304.30 |
| 003     | L.2018     | 002     | C001030  | 232.33 |
| 003     | L.2018     | 010     | C001030  | 343.12 |
SELECT * FROM (
with t1 as (
SELECT '002' as groupid,      'A.2018' as collection, 1 as sequence FROM DUAL UNION ALL
SELECT '002','A.2017',2 FROM DUAL UNION ALL
SELECT '003','P.2018',1 FROM DUAL UNION ALL
SELECT '003','L.2018',2 FROM DUAL UNION ALL
SELECT '003','R.2018',3 FROM DUAL UNION ALL
SELECT '003','M.2018',4 FROM DUAL
),
    t2 as (
SELECT 'A.2018' as collection,'002' as segment ,'C001030' as customer,304.30 as sales  FROM DUAL UNION ALL
SELECT 'A.2017','002','C001030',493.10  FROM DUAL UNION ALL
SELECT 'L.2018','002','C001030',232.33  FROM DUAL UNION ALL
SELECT 'L.2018','010','C001030',343.12  FROM DUAL UNION ALL
SELECT 'R.2018','002','C001030',434.23  FROM DUAL UNION ALL
SELECT 'M.2018','002','C001030',121.12  FROM DUAL
) 
SELECT groupid,
       t1.collection,
       segment,
       customer,
       sales,
       row_number() over (partition by groupid,segment,customer order by t1.collection ASC) rn
FROM   t2,
       t1
WHERE  t1.collection = t2.collection
)
WHERE rn = 1

Output:

GROUPID COLLECTION  SEGMENT CUSTOMER    SALES   RN
002 A.2017  002 C001030 493.1   1
003 L.2018  002 C001030 232.33  1
003 L.2018  010 C001030 343.12  1

BUT:

You expected output for group 002 is

002     | A.2018     | 002     | C001030  | 304.30 |

and you wrote:

"the first collection (ordered by collection sequence) "

so the first collection here is A.2017 , i think.

Kind of

select GroupId, Collection, Segment, Customer, Sales
from(
  select t1.GroupId, t1.Collection, t2.Segment, t2.Customer, t2.Sales,
     row_number() over(partition by t2.Segment, t2.Customer order by t1.Sequence) rn
   from t1
   join t2 on t1.Collection = t2.Collection
   ) t
where rn=1

If I understand correctly, you can use window functions:

select cs.*
from (select c.*, s.segment, s.customer, s.sales,
           row_number() over (partition by c.collection, s.segment, s.customer order by c.sequence) as seqnum
    from collections c join
         sales s
         on c.collection = s.collection
     ) cs
where seqnum = 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM