sql query - MS SQL Server

I have a table defined as:

CREATE TABLE [dbo].[PatientMeasurementCube](
	[pat_id] [int] NOT NULL,
	[DateValue] [date] NULL,
	[msr_id] [int] NULL,
	[CarriedForward] [bit] NULL
) ON [PRIMARY]

Open in new window


Pat_id is the patient identity, DateValue is the year and month of the entry, and CarriedForward indicates if this row is the same for the patients previous row. i.e. CarriedForward is 0 when its a new row.
The data can look like:

22, 2014-01-01,  76, 0
22, 2014-02-01, 89, 1
22, 2014-03,01,  102, 1
22, 2015-01-01, 676, 0
22, 2015-02-01, 762, 1
22, 2015-03,01,  981, 1
26, 2015-01-01, 872,  0
26, 2015-02-01, 987, 1
26, 2015-03-01, 999, 0

Open in new window


In reality the table contains data for 10000+ patients, and the datevalue ranges from 2011-11-01 to 2015-06-01

Can someone suggest a query that would return the row that has the latest datevalue for the year for each patient where the value of CarriedForward is 0.

So from the above data we would get:

22, 2014-01-01,  76, 0
22, 2015-01-01, 676, 0
26, 2015-03-01, 999, 0

Open in new window


Note that some years the patient will not have a record with a CarriedForward value of 0.
soozhAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TONY TAYLORCommented:
SELECT pat_id, DateValue, msr_id, CarriedForward 
FROM (
	SELECT pat_id, DateValue, msr_id, CarriedForward, ROW_NUMBER() OVER(PARTITION BY pat_id, YEAR(DateValue) ORDER BY DateValue DESC) AS ROW_NUM
	FROM dbo.PatientMeasurementCube 
	WHERE CarriedForward = 0
	) Q 
WHERE ROW_NUM = 1

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vitor Montalv√£oMSSQL Senior EngineerCommented:
WITH CTE_PatYear (pat_id, pat_year, pat_maxdate)
AS (
	SELECT pat_id, YEAR(datevalue), MAX(DateValue)
	FROM [PatientMeasurementCube]
	WHERE CarriedForward = 0
	GROUP BY pat_id, YEAR(datevalue)
	)
SELECT P.*
FROM [PatientMeasurementCube] P
	INNER JOIN CTE_PatYear C ON P.pat_id=C.pat_id AND P.DateValue=C.pat_maxdate

Open in new window

0
TONY TAYLORCommented:
I do not oppose the accepted/assisted solution, I just wish to make a comment about Victor's approach.  While there is nothing wrong with it inherently, it might produced undesired results.

If the patient ever has two records associated with the same date AND that is the latest date in the given year, then Victor's example would bring BOTH of those records forward.

Just something to keep in mind!  ;-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.