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 7949425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:59:41+00:00 2026-06-04T01:59:41+00:00

I have a table: +———+——————–+————————–+ | imd_id | Total TRx per Plan | plan

  • 0

I have a table:

+---------+--------------------+--------------------------+
| imd_id  | Total TRx per Plan |        plan name         |
+---------+--------------------+--------------------------+
| 1111005 | 397.1556           | Medicaid Illinois (Idpa) |
| 1111005 | 25.7691            | Self Pay                 |
| 1111005 | 24.4355            | Tricare North Region     |
| 1111005 | 15.0312            | 0                        |
| 1111005 | 8.8425             | 0                        |
| 1111005 | 8.3139             | 0                        |
| 1111005 | 7.0534             | 0                        |
| 1111005 | 6.2588             | 0                        |
| 1111005 | 6.0358             | Bravo Health             |
| 1111005 | 5.9872             | 0                        |
| 1111531 | 133.9664           | Medicaid Al              |
| 1111531 | 29.2318            | 0                        |
| 1111531 | 23.2499            | 0                        |
| 1111531 | 21.9774            | 0                        |
| 1111531 | 14.9269            | 0                        |
| 1111531 | 10.1903            | Self Pay                 |
| 1111531 | 5.4962             | 0                        |
| 1111531 | 5.3409             | Bcbs Federal             |
| 1111531 | 4.4801             | 0                        |
| 1111531 | 3.8003             | 0                        |
+---------+--------------------+--------------------------+

and trying to generate data that looks like this

+---------+--------------------------+----------+---------------+-----------+----------------------+----------+
| imd_id  |      TopFirstPlan       | TopFirst | TopSecondPlan | TopSecond |    TopThirdPlan     | TopThird |
+---------+--------------------------+----------+---------------+-----------+----------------------+----------+
| 1111005 | Medicaid Illinois (Idpa) | 0.78     | Self Pay      | 0.05      | Tricare North Region | 0.04     |
| 1111531 | MEDICAID ALABAMA (AL)    | 0.5      | Self Pay      | 0.04      | Bcbs Federal         | 0.02     |
+---------+--------------------------+----------+---------------+-----------+----------------------+----------+

please note that the way the TOPFIRST, TOP SECOND, TOP THIRD are created is the corresponding Total TRx per Plan divided by the sum of the plans for that specific IMD_ID.

so far I have this:

 select distinct a.imd_id,'topone'=
    (select top 1 totalrxperplan 
        from book1 b
      where b.imd_id =  a.imd_id)/
      (select SUM(totalrxperplan) 
        from book1 b
        where b.imd_id = a.imd_id)
      ,'topplan2'=
      (select top 1 xifinplanname 
        from book1 b
      where b.imd_id =  a.imd_id)
from book1 a
order by 1 asc

this query will return:

+---------+--------------------------+----------+
| imd_id  |      TopFirstPlan1       | TopFirst |
+---------+--------------------------+----------+
| 1111005 | Medicaid Illinois (Idpa) | 79%      |
| 1111531 | MEDICAID ALABAMA (AL)    | 53%      |
+---------+--------------------------+----------+

but I need to add on the other columns.

Please note that we will ignore the plan name where it is 0

  • 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-04T01:59:43+00:00Added an answer on June 4, 2026 at 1:59 am
    CREATE TABLE #x(imd_id INT, totalrxperplan FLOAT, xifinplanname NVARCHAR(64));
    
    INSERT #x VALUES
    (1111005,397.1556 ,'Medicaid Illinois (Idpa)'),
    (1111005,25.7691  ,'Self Pay                '),
    (1111005,24.4355  ,'Tricare North Region    '),
    (1111005,15.0312  ,'0                       '),
    (1111005,8.8425   ,'0                       '),
    (1111005,8.3139   ,'0                       '),
    (1111005,7.0534   ,'0                       '),
    (1111005,6.2588   ,'0                       '),
    (1111005,6.0358   ,'Bravo Health            '),
    (1111005,5.9872   ,'0                       '),
    (1111531,133.9664 ,'Medicaid Al             '),
    (1111531,29.2318  ,'0                       '),
    (1111531,23.2499  ,'0                       '),
    (1111531,21.9774  ,'0                       '),
    (1111531,14.9269  ,'0                       '),
    (1111531,10.1903  ,'Self Pay                '),
    (1111531,5.4962   ,'0                       '),
    (1111531,5.3409   ,'Bcbs Federal            '),
    (1111531,4.4801   ,'0                       '),
    (1111531,3.8003   ,'0                       ');
    

    Now the query:

    WITH cte1(id,pn,s) AS 
    (
        SELECT imd_id, xifinplanname, 
          ROUND(totalrxperplan/SUM(totalrxperplan) OVER (PARTITION BY imd_id),2,1)
        FROM #x WHERE xifinplanname <> '0'
    ),
    cte2(id,pn,s,rn) AS 
    (
        SELECT id,pn,s,ROW_NUMBER() OVER (PARTITION BY id ORDER BY s DESC)
        FROM cte1
    )
    SELECT 
      id, 
      TopFirstPlan  = MAX(CASE WHEN rn = 1 THEN pn END),
      TopFirst      = MAX(CASE WHEN rn = 1 THEN s  END),
      TopSecondPlan = MAX(CASE WHEN rn = 2 THEN pn END),
      TopSecond     = MAX(CASE WHEN rn = 2 THEN s  END),
      TopThirdPlan  = MAX(CASE WHEN rn = 3 THEN pn END),
      TopThird      = MAX(CASE WHEN rn = 3 THEN s  END)
    FROM cte2 
    WHERE rn <= 3
    GROUP BY id;
    

    If my assumption is right that the output numbers don’t match yours because you want to include even ‘0’ plans in the total, just not in the results, then you can just shift the where clause:

    WITH cte1(id,pn,s) AS 
    (
        SELECT imd_id, xifinplanname, 
          ROUND(totalrxperplan/SUM(totalrxperplan) OVER (PARTITION BY imd_id), 2, 1)
        FROM #x -- < -- removed where clause from here
    ),
    cte2(id,pn,s,rn) AS 
    (
        SELECT id,pn,CONVERT(DECIMAL(3,2), s),ROW_NUMBER() OVER 
          (PARTITION BY id ORDER BY s DESC)
        FROM cte1 WHERE pn <> '0' -- <-- moved where clause here
    )
    SELECT 
      id, 
      TopFirstPlan  = MAX(CASE WHEN rn = 1 THEN pn END),
      TopFirst      = MAX(CASE WHEN rn = 1 THEN s  END),
      TopSecondPlan = MAX(CASE WHEN rn = 2 THEN pn END),
      TopSecond     = MAX(CASE WHEN rn = 2 THEN s  END),
      TopThirdPlan  = MAX(CASE WHEN rn = 3 THEN pn END),
      TopThird      = MAX(CASE WHEN rn = 3 THEN s  END)
    FROM cte2 
    WHERE rn <= 3
    GROUP BY id;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table in data base name train delay, with columns train number(int), DelayTime(int),
I have table with 5 columns ------------------------------------------------------ |_id(mongo default)| link | name| street|zip |
I have table Players with columns: ID Name Points What is most efficient way
I have table in my database which has fields of ID,NAME,CONTEXT. I am showing
I have table with data about tasks like Id, Name, Date, WorkerId, VehicleId (and
I have table data like this id id1 name 1 1 test1 1 1
I have table documents (id, name, time) . Is there a special sql command
I have table product(table name) product_id product_name product_image product_price product_description category_id category(table name )
I have table like: +------+-----+ | name | nick| +------+-----+ | yosi | Y
I have table - custom id (like 1.10,1.20) and name (string where values can

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.