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

  • Home
  • SEARCH
  • 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 8559061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:57:08+00:00 2026-06-11T15:57:08+00:00

i have a table: CREATE TABLE [dbo].[TargettingReport]( [MLISNPI] [varchar](50) NULL, [CLIENT_ID1] [varchar](50) NULL, [Sum

  • 0

i have a table:

CREATE TABLE [dbo].[TargettingReport](
    [MLISNPI] [varchar](50) NULL,
    [CLIENT_ID1] [varchar](50) NULL,
    [Sum of count] [int] NULL
) ON [PRIMARY]

i am trying to select the 2nd and third highest SUM OF COUNT based on MLISNPI, CLIENT_ID1

this is how i get the max for every unique occurrence of MLISNPI, CLIENT_ID1:

  select [MLISNPI],[CLIENT_ID1],MAX([Sum of count])
  from [TargettingReport]
  group by 
  [MLISNPI],[CLIENT_ID1]

the question is how do i get the 2nd and 3rd highest?

here’s some example data:

+---------+------------+--------------+
| MLISNPI | CLIENT_ID1 | sum of count |
+---------+------------+--------------+
|  567890 |     214060 |           18 |
|  678901 |     214060 |           58 |
|  789012 |     214060 |           27 |
|  891012 |     214060 |            1 |
|  101112 |     214060 |           23 |
|  003001 |     101596 |            0 |
|  003001 |     101522 |          436 |
|  003001 |     101597 |            0 |
|  003002 |     102165 |           66 |
|  003002 |     100062 |            1 |
|  003002 |     211074 |         1229 |
|  003006 |     102235 |           21 |
|  003014 |     213926 |            5 |
|  003016 |     213143 |            3 |
|  003023 |     213801 |           55 |
|  003023 |     212876 |           44 |
|  003023 |     100218 |            0 |
|  003028 |     211144 |          133 |
|  003041 |     100236 |          346 |
|  003041 |     103164 |           65 |
|  003051 |     213402 |          157 |
|  003058 |     100572 |           28 |
|  003065 |     101632 |           29 |
|  003071 |     213632 |            6 |
|  003072 |     101506 |            4 |
|  003081 |     100087 |          398 |
|  003083 |     214311 |            7 |
|  003117 |     210178 |          203 |
|  003121 |     214008 |            9 |
|  003139 |     102179 |         1635 |
|  003147 |     216022 |           21 |
|  003149 |     211425 |            1 |
|  003186 |     215748 |            1 |
+---------+------------+--------------+
  • 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-11T15:57:10+00:00Added an answer on June 11, 2026 at 3:57 pm
    ; with numbered as
    (
      select *,
          -- Assign number to each record
          -- Numbering is separate for each pair of MLISNPI and CLIENT_ID1
          -- Highest [Sum of desc] will get number 1 and so forth
             row_number() over (partition by [MLISNPI], [CLIENT_ID1]
                                order by [Sum of count] desc) rn
        from [TargettingReport]
    )
    select [MLISNPI],
           [CLIENT_ID1], 
           [Sum of count]
      from numbered
    -- Now one can filter ranks
     where rn between 2 and 3
    

    UPDATE: pivoting [Sum of count] around mlisnpi

    ; with numbered as
    (
      select mlisnpi,
             [sum of count],
          -- Assign number to each record
          -- Numbering is separate for each MLISNPI
          -- Highest [Sum of desc] will get number 1 and so forth
             row_number() over (partition by [MLISNPI]
                                order by [Sum of count] desc) rn
        from [TargettingReport]
    )
    select mlisnpi,
           [1] Rank1,
           [2] Rank2,
           [3] Rank3
      from numbered
     pivot (max([Sum of count]) for rn in ([1], [2], [3])) p
    

    Sql Fiddle is here.

    In case you actually want Cliend_ID1 instead of [Sum of count], just replace all occurrences of [Sum of count] with Client_ID1. Don’t add Client_ID1 to cte because pivot will include it in results and show each combination of mlisnpi and Client_ID1 in its own row.

    UPDATE2: pivoting with title included. A bit more verbose, as you need to union all titles (client_id1) and manipulate row numbers to get correct mix of titles and sums. Also you need to convert all to the same data type (varchar here) to be able to use union all.

    ; with numbered as
    (
      select *,
             row_number() over (partition by [MLISNPI]
                                order by [Sum of count] desc) rn
        from [TargettingReport]
    ),
    unioned as
    (
      select mlisnpi,
             convert(varchar(20), [Sum of count]) value,
             rn * 2 rn
        from numbered
       union all
       select mlisnpi,
           convert(varchar(20), [Client_ID1]),
           rn * 2 - 1
        from numbered
    )
    select mlisnpi,
           [1] Client1,
           [2] Rank1,
           [3] Client2,
           [4] Rank2,
           [5] Client3,
           [6] Rank3
      from unioned
     pivot (max(value) for rn in ([1], [2], [3], [4], [5], [6])) p
    

    Sql Fiddle for this is here.

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

Sidebar

Related Questions

I have the following table: CREATE TABLE [dbo].[TableB]( [id] [int] NULL, [FileName] [varchar](20) NULL
I have a table like: CREATE TABLE [dbo].[MOVIES_HISTORY]( [DATE] [datetime] NOT NULL, [COUNT] [int]
I have this table CREATE TABLE [dbo].[friend_blocked_list]( [subdomain] [varchar](50) NOT NULL, [un] [nvarchar](50) NOT
I have Created a table like CREATE TABLE [dbo].[tab1]( [Id] [int] NOT NULL, [Name]
I have this sql statement: CREATE TABLE [dbo].[User]( [UserId] [int] IDENTITY(1,1) NOT NULL, [FirstName]
I have a table CREATE TABLE [dbo].[MergeAndDeleteNonprofitDetails]( ID int IDENTITY(1,1) PRIMARY KEY, [ToJKNonprofitID] [int]
I have the following table: CREATE TABLE [dbo].[Tree]( [AutoID] [int] IDENTITY(1,1) NOT NULL, [Category]
Imagine to have a table defined as CREATE TABLE [dbo].[Price]( [ID] [int] NOT NULL,
I have a table like so.. CREATE TABLE [dbo].[Locations_Hours]( [LocationID] [int] NOT NULL, [sun_open]
I have the following table CREATE TABLE [dbo].[LogFiles_Warehouse]( [id] [int] IDENTITY(1,1) NOT NULL, [timestamp]

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.