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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:03:04+00:00 2026-05-20T11:03:04+00:00

I have the following query: for each FacilityCode I am getting LastCompletedOn and LastEDAOn

  • 0

I have the following query: for each FacilityCode I am getting LastCompletedOn and LastEDAOn.

SELECT  Code ,
        MAX(DATEADD(HOUR, Seed_TimeZone.DifferenceFromUTC, CompletedOn)) 'LastCompletedOn' ,
        MAX(DATEADD(HOUR, Seed_TimeZone.DifferenceFromUTC,LastUpdateDemographicsDate)) 'LastEDAOn'
FROM    PatientCharts WITH ( NOLOCK )
        INNER JOIN Facilities WITH ( NOLOCK ) ON Facilities.FacilityId = PatientCharts.FacilityId
        INNER JOIN Seed_TimeZone WITH ( NOLOCK ) ON Seed_TimeZone.TimeZoneId =      Facilities.TimeZoneId

WHERE   Facilities.IsActive = 1
        AND PatientCharts.IsDeleted = 0
        AND PatientCharts.IsErroneous = 0
        --AND dbo.PatientCharts.ChartStatusID=1
GROUP BY Code

I want to put the commented condition “AND dbo.PatientCharts.ChartStatusID=1 ” on LastCompletedOn. I have tried putting the whole query as a subquery but the same value is repeated for each facilitycode. How to accomplish this.

ANSWER

Response suggested by srgerg works fine. However we can get the same result by using a extra join

SELECT  Code ,       
        MAX(DATEADD(HOUR, DifferenceFromUTC, compOn.CompletedOn)) 'LastCompletedOn' ,
        MAX(DATEADD(HOUR, DifferenceFromUTC,Prim.LastUpdateDemographicsDate)) 'LastEDAOn'
FROM    PatientCharts AS Prim WITH ( NOLOCK )
        INNER JOIN Facilities WITH ( NOLOCK ) ON Facilities.FacilityId = Prim.FacilityId
        INNER JOIN Seed_TimeZone LEO WITH ( NOLOCK ) ON LEO.TimeZoneId = 
  Facilities.TimeZoneId 
        LEFT OUTER JOIN PatientCharts compOn ON compOn.PatientChartId = prim.PatientChartId AND compOn.ChartStatusID=1      
WHERE   Facilities.IsActive = 1
        AND Prim.IsDeleted = 0
        AND Prim.IsErroneous = 0        
GROUP BY Code
  • 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-05-20T11:03:05+00:00Added an answer on May 20, 2026 at 11:03 am

    You could try something like this:

    SELECT  Code ,
            MAX(CASE PatientCharts.ChartStatusID
                WHEN 1 THEN DATEADD(HOUR, Seed_TimeZone.DifferenceFromUTC, CompletedOn)
                ELSE CONVERT(DATETIME, '1-Jan-1900') END) 'LastCompletedOn' ,
            MAX(DATEADD(HOUR, Seed_TimeZone.DifferenceFromUTC,LastUpdateDemographicsDate)) 'LastEDAOn'
    FROM    PatientCharts WITH ( NOLOCK )
            INNER JOIN Facilities WITH ( NOLOCK ) ON Facilities.FacilityId = PatientCharts.FacilityId
            INNER JOIN Seed_TimeZone WITH ( NOLOCK ) ON Seed_TimeZone.TimeZoneId = Facilities.TimeZoneId
    WHERE   Facilities.IsActive = 1
            AND PatientCharts.IsDeleted = 0
            AND PatientCharts.IsErroneous = 0
            --AND dbo.PatientCharts.ChartStatusID=1
    GROUP BY Code
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following query: select r.people_code_id [People Code ID], r.resident_commuter [Campus6], c1.udormcom [AG],
I have following query which groups and counts entries for each day : SELECT
I have the following query: SELECT Account, Unit, SUM(state_fee), Code FROM tblMta WHERE MTA.Id
I have the following query which returns NULL for each field if there are
I have following SQL query SELECT TOP 10000 AVG(DailyNodeAvailability.Availability) AS AVERAGE_of_Availability FROM Nodes INNER
I have the following query: SELECT * FROM my_users as U RIGHT JOIN subscribers
I have the following query SELECT s.s_id, s.t_id, c.c_id, c.desc, sm.user_id FROM s s
I have the following queries: Query 1: Select * from T10,T11,T12,T13,T14 where T10.C0 =
I have the following query: SELECT Base.ReportDate, Base.PropertyCode, Base.FirstName, Base.MiddleName, Base.LastName, Person.FirstName, Person.MiddleName, Person.LastName
I have the following query: select * from ACADEMIC a left join RESIDENCY r

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.