Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9052809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:16:17+00:00 2026-06-16T13:16:17+00:00

I have temporary table ( @TempPackages ) which looks like this: EntryId (PK) PackageId

  • 0

I have temporary table (@TempPackages) which looks like this:

EntryId (PK)    PackageId    SubProductID    SubProductSequence
1               1111         17              3
2               1111         28              4
3               1111         33              1
4               1111         67              5
5               1111         122             2
6               2222         18              4
7               2222         29              5
8               2222         33              9
9               2222         103             7
10              2222         99              11
11              3333         256             5
12              3333         333             6
13              3333         789             3
14              3333         1023            2
15              3333         9845            1

I need a query which will give me the rows with the minimum/maximum SubProductSequence value for each unique PackageId. For the table above, the query would return this:

EntryId (PK)    PackageId    SubProductID    SubProductSequence
3               1111         33              1
4               1111         67              5
6               2222         18              4
10              2222         99              11
12              3333         333             6
15              3333         9845            1

The EntryId column was something that I added while trying to solve this, as it gives me a unique column to join the same table on to (to ensure I still only end up with 15 rows in my joined table).

I tried this – just to get the MIN():

SELECT
    *
FROM
    @TempPackages p1
INNER JOIN
    @TempPackages p2 ON p1.EntryId = p2.EntryId
    AND p1.SubProductSequence = (
        SELECT
            MIN(SubProductSequence)
        FROM
            @DeparturesToUpdate)

Obviously this is wrong, because the INNER JOIN is superfluous and the SELECT MIN() clause is wrong as it selects the rows with the minimum overall sequence numbers, not the minimum sequence numbers per package.

Any suggestions about the best way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T13:16:18+00:00Added an answer on June 16, 2026 at 1:16 pm

    One way is to use the ROW_NUMBER() function:

    SELECT
        EntryId 
      , PackageId 
      , SubProductID 
      , SubProductSequence
    FROM
      ( SELECT
            EntryId 
          , PackageId 
          , SubProductID 
          , SubProductSequence
          , ROW_NUMBER() OVER (PARTITION BY PackageId
                               ORDER BY SubProductSequence ASC)
              AS rna
          , ROW_NUMBER() OVER (PARTITION BY PackageId
                               ORDER BY SubProductSequence DESC)
              AS rnd
        FROM
            @TempPackages
      ) AS tmp 
    WHERE
          rna = 1
       OR rnd = 1 ;
    

    ROW_NUMBER() is a ranking function that is used with OVER clause. What it basically does in this case, is it groups the rows with same PackageId (that is done with the PARTITION BY PackageId), then orders them by SubProductSequence (ascending or descending) and assigns a row_number, starting from 1 for each packageId.

    So, the subquery would return this, if it was run alone:

    EntryId (PK)    PackageId    SubProductID    SubProductSequence  rna  rnd
    3               1111         33              1                    1    5
    5               1111         122             2                    2    4
    1               1111         17              3                    3    3
    2               1111         28              4                    4    2
    4               1111         67              5                    5    1
    
    6               2222         18              4                    1    5
    7               2222         29              5                    2    4
    9               2222         103             7                    3    3
    8               2222         33              9                    4    2
    10              2222         99              11                   5    1
    
    15              3333         9845            1                    1    5
    14              3333         1023            2                    2    4
    13              3333         789             3                    3    3
    11              3333         256             5                    4    2    
    12              3333         333             6                    5    1
    

    The WHERE condition added in the external query is obvious afterwards.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have optimized a complex Oracle statement using temporary table like this : original
I have a temporary table (or, say, a function which returns a table of
I have a string like following: CREATE GLOBAL TEMPORARY TABLE some_temp_table_name or CREATE GLOBAL
I have a sample file like the following: CREATE GLOBAL TEMPORARY TABLE tt_temp_user_11 (
I have a table called tester which I need to overlay with a temporary
I have an Oracle global temporary table which is ON COMMIT DELETE ROWS. I
I have a stored procedure inside which I create a temporary table that typically
I have written a Stored procedure which return some fields from a temporary table
I have created a temporary table using cakephp $this->Model->query() and inserted some records using
i have a temporary table in which i have the following data , i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.