Compare records expanded

Posted on 2012-08-27
Last Modified: 2012-08-27
I have this query:

SELECT     TOP (100) PERCENT p.UnosId AS [UNOS ID], ro.Name AS Hospital, dr.CrossClampedOn AS CrossClamp,
                      MAX(CASE WHEN evCriteria.parentValueId = '860f6e6d-26e5-4667-8cb2-774f4dc249ae' THEN evCriteria.Description ELSE '0' END) AS [Donor Criteria Type],
                      CONVERT(varchar(10), p.Age) + '/ ' + p.AgeUnits AS [Age/Units], p.Sex,
                      MAX(CASE WHEN ev.EnumerationTypeId = 'd9510b51-3677-4c39-b76a-eea698f50bc0' THEN ev.Description ELSE '0' END) AS Race, CONVERT(varchar(10), p.Height)
                      + '/' + p.HeightUnits AS [Height/Units], CONVERT(varchar(10), p.Weight) + '/' + p.WeightUnits AS [Weight/Units], MAX(CASE WHEN c.NA IS NOT NULL
                      THEN CONVERT(nvarchar(10), c.NA) END) + '/' +
                          (SELECT     CONVERT(nvarchar(10), NA) AS Sodium
                            FROM          dbo.ITX_vw_tbl_Chemistry AS s
                            WHERE      (DateTime = MAX(c.DateTime))) AS [Sodium Peak/Last]
FROM         dbo.ITX_vw_tbl_Referral AS r INNER JOIN
                      dbo.ITX_vw_tbl_DonorReferral AS dr ON r.Id = dr.ReferralId INNER JOIN
                      dbo.ITX_vw_tbl_Patient AS p ON r.PatientId = p.Id INNER JOIN
                      dbo.ITX_vw_tbl_Organization AS ro ON r.ReferringOrganizationId = ro.Id INNER JOIN
                      dbo.ITX_vw_tbl_Chemistry AS c ON p.PatientId = c.PatientId LEFT OUTER JOIN
                      dbo.ITX_vw_tbl_EnumerationValue AS ev ON ev.Value = CONVERT(nvarchar(50), p.Race) LEFT OUTER JOIN
                      dbo.ITX_vw_tbl_EnumerationValue AS evCriteria ON evCriteria.Id = dr.OrganOutcomeDetail
GROUP BY p.UnosId, ro.Name, dr.CrossClampedOn, p.Age, p.Sex, p.Height, p.HeightUnits, p.Weight, p.WeightUnits, p.AgeUnits
HAVING      (p.UnosId = 'VHO228')
ORDER BY Hospital

I need to make sure the MAXDateTime corresponds last entered c.NA. Sometimes there are greater dates with no entries.
Question by:LCNW
    LVL 75

    Accepted Solution

    dont you think your where condition should be like this

                                WHERE      (DateTime = (SELECT MAX(c.DateTime) FROM dbo.ITX_vw_tbl_Chemistry AS c1 WHERE s.PatientId = c1.PatientId ) )) AS [Sodium Peak/Last]
    LVL 1

    Author Comment

    Aneeshattingal, I get a subquery returns more than 1 value error.
    LVL 1

    Author Comment

    Maybe a reverse coalesce? Not sure how to code that.
    LVL 1

    Author Closing Comment

    This helped. Thanks.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
    In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    This video discusses moving either the default database or any database to a new volume.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now