Max Indicator to display

Hi, I have a table like below:

Ship_ID	     Carton_ID	                  Ship_Date
111	         123	                4/1/2015
111	         123	                5/1/2015
222	         345	                4/1/2015
222	         345	                5/1/2015

Open in new window


I want to add a max indicator, to display something like this:

Ship_ID	     Carton_ID	                Ship_Date	Latest_Ind
111	        123	                  4/1/2015	
111	        123	                  5/1/2015	Y
222	        345	                  4/1/2015	
222	        345	                  5/1/2015	Y

Open in new window


Can someone help with a better logic to add this column?
 Below is the table definition.

DECLARE @TMP_TBL TABLE (SHIP_ID INT,CARTON_ID INT,SHIP_DT DATE)

INSERT INTO @TMP_TBL
SELECT 111 AS SHIP_ID,123 AS CARTON_ID,CAST('4/1/2015' AS DATE) AS SHIP_DT
UNION
SELECT 111	AS SHIP_ID,123 AS CARTON_ID,CAST('5/1/2015' AS DATE) AS SHIP_DT
UNION
SELECT 222	AS SHIP_ID,345 AS CARTON_ID,CAST('4/1/2015' AS DATE) AS SHIP_DT
UNION
SELECT 222 AS SHIP_ID,345 AS CARTON_ID,CAST('5/1/2015' AS DATE) AS SHIP_DT

Open in new window

pvsbandiAsked:
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.

Ryan ChongCommented:
if you are grouping records by SHIP_ID, try:

select a.SHIP_ID, a.CARTON_ID, a.SHIP_DT,
case when a.idx = 1 then 'Y' else '' end Latest_Ind
from
(
select a.SHIP_ID, a.CARTON_ID, a.SHIP_DT, row_number() over (partition by a.SHIP_ID order by a.SHIP_DT desc) idx
from @TMP_TBL a
) a
order by a.CARTON_ID, a.SHIP_DT

Open in new window


or if you are grouping records by SHIP_ID and CARTON_ID, try:

select a.SHIP_ID, a.CARTON_ID, a.SHIP_DT,
case when a.idx = 1 then 'Y' else '' end Latest_Ind
from
(
select a.SHIP_ID, a.CARTON_ID, a.SHIP_DT, row_number() over (partition by a.SHIP_ID, a.CARTON_ID order by a.SHIP_DT desc) idx
from @TMP_TBL a
) a
order by a.CARTON_ID, a.SHIP_DT

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
pvsbandiAuthor Commented:
Thanks!
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
Microsoft SQL Server

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.