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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:06:36+00:00 2026-06-15T23:06:36+00:00

I have my table (cte) defintions and result set here The CTE may look

  • 0

I have my table (cte) defintions and result set here

The CTE may look strange but it has been tested and returns the correct results in the most efficient manner that I’ve found yet. The below query will find the number of person IDs (patid) who are taking two or more drugs at the same time. Currently, the query works insofar as it returns the patIDs of the people taking both drugs, but not both drugs at the same time. Taking both drugs is indicated by one fillDate of one drug falling before a scriptEndDate of another drug. So
enter image description here

You can see in this partial result set that on line 18 the scriptFillDate is 2009-07-19 which is between the fillDate and scriptEndDate of the same patID from row 2. What constraint do I need to add so I can filter these unneeded results?

--PatientDrugList is a CTE because eventually parameters might be passed to it
--to alter the selection population
;with PatientDrugList(patid, filldate, scriptEndDate,drugName,strength)
as
(
    select rx.patid,rx.fillDate,rx.scriptEndDate,rx.drugName,rx.strength
        from rx
),
--the row constructor here will eventually be parameters for a stored procedure
DrugList (drugName)
as
(
    select x.drugName
        from (values ('concerta'),('fentanyl'))
        as x(drugName)
        where x.drugName is not null
)


    --the row number here is so that I can find the largest date range
    --(the largest datediff means the person was on a given drug for a larger
    --amount of time.  obviously not a optimal solution
     --celko inspired relational division!
     select distinct row_number() over(partition by pd.patid, drugname order by datediff(day,pd.fillDate,pd.scriptEndDate)desc)  as rn
     ,pd.patid
    ,pd.drugname
    ,pd.fillDate
    ,pd.scriptEndDate
    from PatientDrugList as pd
    where not exists
    (select * from DrugList 
    where not exists
    (select * from PatientDrugList as pd2
    where(pd.patid=pd2.patid)
    and (pd2.drugName = DrugList.drugName)))
    and exists 
    (select * 
        from DrugList
        where DrugList.drugName=pd.drugName
    )
    group by pd.patid, pd.drugName,pd.filldate,pd.scriptEndDate
  • 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-15T23:06:38+00:00Added an answer on June 15, 2026 at 11:06 pm

    Wrap you original query into a CTE, or better yet, for performance, stability of query plan and result, store it into a temp table.

    The query below (assuming CTE option) will give you the overlapping times when both drugs are being taken.

    ;with tmp as (
       .. your query producing the columns shown ..
    )
    select *
      from tmp a
      join tmp b on a.patid = b.patid and a.drugname <> b.drugname
     where a.filldate < b.scriptenddate
       and b.filldate < a.scriptenddate;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view that returns 2 ints from a table using a CTE.
I have table in which I am inserting rows for employee but next time
I have table and this table contain result column with some entries. I just
I have a table which has hierarchy in it. Lets start with Id =
I have written a table-valued UDF that starts by a CTE to return a
I have a CTE which pares down a set of people based on their
I have been creating a date range, but in some cases a have a
Here is the problem I am trying to solve. I have a table where
Sorry if this has been answered before, but all the related questions didn't quite
I have the CTE below working well in re-cursing an employee table and building

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.