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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:14:20+00:00 2026-06-17T21:14:20+00:00

I have a query that returns results that look like the create table query

  • 0

I have a query that returns results that look like the create table query below

create table #testresults
(
  pat_id int,
  fill_date date,
  script_end_date date,
  drug_class char(3),
  distinctDrugs int
)

There are seven different categories of drugs that a pat_id can be given. The distinctDrugs column is the number of different drugs that a pat_id can be given within the time frame of fill_date and script_end_date. The results of running the query look like:
enter image description here

Each pat_id has many different fill_date and script_end_date time periods. These different time periods have a different drug_class and distinctDrugs per row. The right two columns on this example are indicative of what I need: I need on each row, for each fill_date and script_end_date the drug_class anddistinctDrugs for each drug_class. I used this query to add the two right-most columns to my base view

select distinct
 t.pat_id
,t.fill_date
,t.script_end_date
,t.drug_class
,t.distinctDrugs
,h3a.drug_class as h3aDrugClass
,h3a.distinctDrugs
from #temp as t
left join 
(
    select 
     pat_id
    ,fill_date
    ,script_end_date
    ,drug_class
    ,distinctDrugs
    from #temp 
    where drug_class='h3a'
) as h3a on h3a.pat_id=t.pat_id and h3a.fill_date between t.fill_date and t.script_end_date and t.drug_class !=h3a.drug_class
where h3a.drug_class is not null

It would be easy enough to do this for the rest of the drug_class columns, but this isn’t remotely efficient. Is there a way to do this much more simply using recursion (or any other manner for that matter)?

EDIT:
Here is the final product of what I was looking for:

select distinct 
 f.pat_id
,f.fill_date
,f.script_end_date
,case when h3a.drug_class is null then 'H3A' else 'H3A' end as H3A
,isnull(h3a.distinctDrugs,0) as h3aCounts
,case when h4b.drug_class is null then 'H4B' else 'H4B' end as H4B
,isnull(h4b.distinctDrugs,0) as h4bCounts
,case when h6h.drug_class is null then 'H6H' else 'H6H' end  as H6H
,isnull(h6h.distinctDrugs,0) as h6hCounts
,case when h2s.drug_class is null then 'H2S' else 'H2S' end as H2S 
,isnull(h2s.distinctDrugs,0) as h2sCounts
,case when h2e.drug_class is null then 'H2E' else 'H2E' end  as H2E
,isnull(h2e.distinctDrugs,0) as h2eCounts
,case when h2f.drug_class is null then 'H2F' else 'H2F' end as H2F
,isnull(h2f.distinctDrugs,0) as h2fCounts
,case when j7c.drug_class is null then 'J7C' else 'J7C' end  as J7C
,isnull(j7c.distinctDrugs,0) as j7cCounts
from familyStrata as f
left join 
(
    select
     pat_id
    ,drug_class
    ,distinctDrugs
    ,fill_date
    from familyStrata 
    where drug_class='h3a'
) as h3a on h3a.pat_id=f.pat_id and h3a.fill_date between f.fill_date and f.script_end_date
left join 
(
    select
     pat_id
    ,drug_class
    ,fill_date
    ,distinctDrugs
    from familyStrata
    where drug_class='h4b'
) as h4b on h4b.pat_id=f.pat_id and h4b.fill_date between f.fill_date and f.script_end_date
left join 
(
    select
     pat_id
    ,drug_class
    ,fill_date
    ,distinctDrugs
    from familyStrata
    where drug_class='h6h'
) as h6h on h6h.pat_id=f.pat_id and h6h.fill_date between f.fill_date and f.script_end_date
left join 
(
    select
     pat_id
    ,drug_class
    ,fill_date
    ,distinctDrugs
    from familyStrata
    where drug_class='h2f'
) as h2f on h2f.pat_id=f.pat_id and h2f.fill_date between f.fill_date and f.script_end_date
left join 
(
    select
     pat_id
    ,drug_class
    ,fill_date
    ,distinctDrugs
    from familyStrata
    where drug_class='h2s'
) as h2s on h2s.pat_id=f.pat_id and h2s.fill_date between f.fill_date and f.script_end_date
left join 
(
    select
     pat_id
    ,drug_class
    ,fill_date
    ,distinctDrugs
    from familyStrata
    where drug_class='h2e'
) as h2e on h2e.pat_id=f.pat_id and h2e.fill_date between f.fill_date and f.script_end_date
left join 
(
    select
     pat_id
    ,drug_class
    ,fill_date
    ,distinctDrugs
    from familyStrata
    where drug_class='j7c'
) as j7c on j7c.pat_id=f.pat_id and j7c.fill_date between f.fill_date and f.script_end_date

This is actually rather fast, but is in no way remotely elegeant/extensible. Here is what the result set should look like:
enter image description here

You can see the number the drug_class and the distinctDrugs for each different drug, for each time period. Now, is there a much more elegant solution to this problem than 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-17T21:14:22+00:00Added an answer on June 17, 2026 at 9:14 pm

    So, recursion isn’t the best method for doing this. I settled with

    select
     pat_id
    ,fill_date
    ,script_end_date
    ,'h3a' as h3a, coalesce(sum(case when drug_class='h3a' then distinctDrugs end),0) as h3aCounts
    ,'h4b' as h4b, coalesce(sum(case when drug_class='h4b' then distinctDrugs end),0) as h4bCounts
    ,'h6h' as h6h, coalesce(sum(case when drug_class='h6h' then distinctDrugs end),0) as h6hCounts
    ,'h2e' as h2e, coalesce(sum(case when drug_class='h2e' then distinctDrugs end),0) as h2eCounts
    ,'h3a' as h2f, coalesce(sum(case when drug_class='h2f' then distinctDrugs end),0) as h2fCounts
    ,'h3a' as h2s, coalesce(sum(case when drug_class='h2s' then distinctDrugs end),0) as h2sCounts
    ,'h3a' as j7c, coalesce(sum(case when drug_class='j7c' then distinctDrugs end),0) as j7cCounts
    ,row_number() over(order by pat_id) as rn
    from x
    group by pat_id,fill_date,script_end_date
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this query that returns the results by ordering it by focus.name ASC.
I'm running an MySQL query that returns results based on location. However I have
I have a method that basically returns the results of a mysql query. public
I have a query that returns a result set similar to the one below:
I have a query that returns data that looks like. PackageName SiteLocation Visible Package1
I have the following schema, and would like to do a query that returns
With T-SQL, is it possible to write a query that returns results that look
I have a sql query that returns names, appended with the current date-time, with
I am using MySQL 5.5. I have a query which returns results like this
I have a query that returns me around 6 million rows, which is too

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.