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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:46:22+00:00 2026-06-17T20:46:22+00:00

Hi and thanks for reading, I am trying to run the following SQL query

  • 0

Hi and thanks for reading,

I am trying to run the following SQL query with correlated subquery and it is bringing back no results. I am using the subquery to only bring back results that have the lowest date range. Both the query alone and the subquery alone work fine.

Any ideas? Am I linking the correlated subquery incorrectly?

Note: The correlated subquery is on the last line of the query

Thank you,
Willz06jw

SELECT aaa."effective_time", 
       aaa."event_type_c", 
       "clarity_dep"."department_name", 
       "patient"."birth_date", 
       "patient"."pat_mrn_id", 
       "zc_ped_delivr_meth"."name", 
       "zc_ped_delivr_meth"."ped_delivr_meth_c", 
       aaa."department_id", 
       "clarity_dep"."department_id", 
       aaa."alt_event_type_c", 
       aaa."in_event_type_c" 
FROM   (("CLARITY"."clarity_adt" aaa 
         inner join "CLARITY"."clarity_dep" "CLARITY_DEP" 
                 ON aaa."department_id" = "clarity_dep"."department_id") 
        inner join "CLARITY"."patient" "PATIENT" 
                ON aaa."pat_id" = "patient"."pat_id") 
       inner join "CLARITY"."zc_ped_delivr_meth" "ZC_PED_DELIVR_METH" 
               ON "patient"."ped_delivr_meth_c" = 
                  "zc_ped_delivr_meth"."ped_delivr_meth_c" 
WHERE  ( "patient"."birth_date" >= To_date ('01-12-2012 00:00:00', 
                                            'DD-MM-YYYY HH24:MI:SS') 
         AND "patient"."birth_date" < To_date ('06-12-2012 00:00:00', 
                                      'DD-MM-YYYY HH24:MI:SS' 
                                      ) ) 
       AND ( aaa."department_id" = 236601 
              OR aaa."department_id" = 236703 
              OR aaa."department_id" = 236801 
              OR aaa."department_id" = 236901 
              OR aaa."department_id" = 237101 
              OR aaa."department_id" = 237201 ) 
       AND aaa."event_type_c" = 3 
       AND aaa."effective_time" = (SELECT Min(bbb."effective_time") 
                                   FROM   "clarity_adt" bbb 
                                   WHERE  aaa."pat_id" = bbb.pat_id) 
  • 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-17T20:46:23+00:00Added an answer on June 17, 2026 at 8:46 pm

    Without zifting through the entire statement, the easiest solution would be to just wrap the statement in a select using the ROW_NUMBER function to get the lowest dates.

    SQL Statement 1

    SELECT  *
    FROM    (
              SELECT ROW_NUMBER() OVER (PARTITION BY pat_id ORDER BY effective_time) AS rn,
                     aaa."effective_time", 
                     aaa."event_type_c", 
                     "clarity_dep"."department_name", 
                     "patient"."birth_date", 
                     "patient"."pat_mrn_id", 
                     "zc_ped_delivr_meth"."name", 
                     "zc_ped_delivr_meth"."ped_delivr_meth_c", 
                     aaa."department_id", 
                     "clarity_dep"."department_id", 
                     aaa."alt_event_type_c", 
                     aaa."in_event_type_c" 
              FROM   "CLARITY"."clarity_adt" aaa
                     inner join "CLARITY"."clarity_dep" "CLARITY_DEP" ON aaa."department_id" = "clarity_dep"."department_id"
                     inner join "CLARITY"."patient" "PATIENT" ON aaa."pat_id" = "patient"."pat_id"
                     inner join "CLARITY"."zc_ped_delivr_meth" "ZC_PED_DELIVR_METH" ON "patient"."ped_delivr_meth_c" = "zc_ped_delivr_meth"."ped_delivr_meth_c"
              WHERE  ( "patient"."birth_date" >= To_date ('01-12-2012 00:00:00', 'DD-MM-YYYY HH24:MI:SS') 
                     AND "patient"."birth_date" < To_date ('06-12-2012 00:00:00', 'DD-MM-YYYY HH24:MI:SS' ) ) 
                     AND ( aaa."department_id" = 236601 OR aaa."department_id" = 236703 OR aaa."department_id" = 236801 OR aaa."department_id" = 236901 OR aaa."department_id" = 237101 OR aaa."department_id" = 237201 ) 
                     AND aaa."event_type_c" = 3 
            ) q
    WHERE   q.rn = 1
    

    Your statement could be simplified (in reading it) by using an IN statement io. all the ORstatements like such

    SQL Statement 2

    SELECT  *
    FROM    (
              SELECT ROW_NUMBER() OVER (PARTITION BY pat_id ORDER BY effective_time) AS rn,
                     aaa."effective_time", 
                     aaa."event_type_c", 
                     "clarity_dep"."department_name", 
                     "patient"."birth_date", 
                     "patient"."pat_mrn_id", 
                     "zc_ped_delivr_meth"."name", 
                     "zc_ped_delivr_meth"."ped_delivr_meth_c", 
                     aaa."department_id", 
                     "clarity_dep"."department_id", 
                     aaa."alt_event_type_c", 
                     aaa."in_event_type_c" 
              FROM   "CLARITY"."clarity_adt" aaa
                     inner join "CLARITY"."clarity_dep" "CLARITY_DEP" ON aaa."department_id" = "clarity_dep"."department_id"
                     inner join "CLARITY"."patient" "PATIENT" ON aaa."pat_id" = "patient"."pat_id"
                     inner join "CLARITY"."zc_ped_delivr_meth" "ZC_PED_DELIVR_METH" ON "patient"."ped_delivr_meth_c" = "zc_ped_delivr_meth"."ped_delivr_meth_c"
              WHERE  ( "patient"."birth_date" >= To_date ('01-12-2012 00:00:00', 'DD-MM-YYYY HH24:MI:SS') 
                     AND "patient"."birth_date" < To_date ('06-12-2012 00:00:00', 'DD-MM-YYYY HH24:MI:SS' ) ) 
                     AND ( aaa."department_id" IN (236601,236703,236801,236901,237101,237201) )
                     AND aaa."event_type_c" = 3 
            ) q
    WHERE   q.rn = 1
    

    In the end, I would even simplify it further by aliassing all tables and use a WITH statement. The final result would look like this

    SQL Statement 3

    ;WITH q AS (
      SELECT ROW_NUMBER() OVER (PARTITION BY pat_id ORDER BY effective_time) AS rn,
             aaa."effective_time", 
             aaa."event_type_c", 
             cd."department_name", 
             p."birth_date", 
             p."pat_mrn_id", 
             pdm."name", 
             pdm."ped_delivr_meth_c", 
             aaa."department_id", 
             cd."department_id", 
             aaa."alt_event_type_c", 
             aaa."in_event_type_c" 
      FROM   "CLARITY"."clarity_adt" aaa
             inner join "CLARITY"."clarity_dep" cd ON aaa."department_id" = cd."department_id"
             inner join "CLARITY"."patient" p ON aaa."pat_id" = p."pat_id"
             inner join "CLARITY"."zc_ped_delivr_meth" pdm ON p."ped_delivr_meth_c" = pdm."ped_delivr_meth_c"
      WHERE  p."birth_date" >= To_date ('01-12-2012 00:00:00', 'DD-MM-YYYY HH24:MI:SS'
             AND p."birth_date" < To_date ('06-12-2012 00:00:00', 'DD-MM-YYYY HH24:MI:SS' ) ) 
             AND aaa."department_id" IN (236601,236703,236801,236901,237101,237201)
             AND aaa."event_type_c" = 3 
    )
    SELECT  *
    FROM    q
    WHERE   rn = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

thanks for your reading. I am trying to modify Jquery Nivo Zoom plugin to
I'm trying to run a Linq query on a datatable to produce a result,
I´m trying to run an old .NET application from an ASP.NET website. After reading
Thanks for reading this. I'm trying to do this lab from programarcadegames.com where you
Thanks for reading! I am currently trying to develop an application which will backup
When trying to perform the following cast DateTime issueDate = Convert.ToDateTime(binfile.ReadByte()); (reading .dat files
i was trying to run this query: ( (SELECT * FROM (SELECT * FROM
I am trying to pull data using an mysql query that requires that I
Thanks for reading this. I have no idea why this is throwing a NullReferenceException
Thanks for reading. I have done a search, read multiple posts (lost count) and

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.