Results 1 to 9 of 9

Thread: [RESOLVED] Select Last Record according to max date?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Resolved [RESOLVED] Select Last Record according to max date?

    Hey all,

    I got this SP that I am using in SQL 2000.

    Code:
    SET QUOTED_IDENTIFIER ON 
    GO
    SET ANSI_NULLS ON 
    GO
    
    
    ALTER  PROCEDURE dbo.usp_Approved_Subs
    
    @p_Status NVARCHAR(25),
    @p_Year INT
    
    AS
    
    SELECT 
    	pv.VendorName as Subcontractor, 
    	pv.SentBy_Name as InvitedByName,
    	pv.SentTo_Name as ContactName,
    	al.ActionDate as DateApproved,
    	al.fk_Action as Status,	
    	al.ActionDate as ActionDate	
    FROM PreQual_Vendors pv
    LEFT OUTER JOIN ActionLog al ON  pv.pk_PreQual_Vendor = al.fk_PreQual_Vendor
    WHERE 
    	al.ExtraText = @p_Status AND DATEPART(yyyy, al.ActionDate) = @p_Year
    ORDER BY  pv.VendorName
    	
    
    GO
    SET QUOTED_IDENTIFIER OFF 
    GO
    SET ANSI_NULLS ON 
    GO
    This returns records but I need to make sure the record that is returning is the last record for that given year. Some contractors could recieve a new status after the returned record therefore what is returning isn't very accurate.

    How would I return the max date that meets the status and year condition?

    Thanks

  2. #2
    Hyperactive Member
    Join Date
    Jan 2008
    Location
    Merseyside
    Posts
    456

    Re: Select Last Record according to max date?

    Do you have some sort of pk field so that you could get the max value for that year, or could you use the actiondate field in some way.

    e.g.
    Code:
    SELECT 
    	pv.VendorName as Subcontractor, 
    	pv.SentBy_Name as InvitedByName,
    	pv.SentTo_Name as ContactName,
    	al.ActionDate as DateApproved,
    	al.fk_Action as Status,	
    	al.ActionDate as ActionDate	
    FROM PreQual_Vendors pv
    LEFT OUTER JOIN ActionLog al ON  pv.pk_PreQual_Vendor = al.fk_PreQual_Vendor
    WHERE 
    	al.ExtraText = @p_Status AND DATEPART(yyyy, al.ActionDate) = @p_Year
    	AND al.ActionDate = (
    				SELECT MAX(al2.ActionDate) 
    				FROM ActionLog al2
    				WHERE al2.fk_PreQual_Vendor = al.fk_PreQual_Vendor
    			)
    ORDER BY  pv.VendorName

  3. #3
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: Select Last Record according to max date?

    Do your ActionLog table has PK?also is ActionDate value is Incremental in your table?I mean for each new ros value of ActionDate will be always higher compare to previous row value?
    __________________
    Rate the posts that helped you

  4. #4
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: Select Last Record according to max date?

    i am too late ,but i think above query will return only one record right?
    __________________
    Rate the posts that helped you

  5. #5
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: Select Last Record according to max date?

    you can also try this if Post #3 condition holds true
    Code:
    SET QUOTED_IDENTIFIER ON 
    GO
    SET ANSI_NULLS ON 
    GO
    
    
    ALTER  PROCEDURE dbo.usp_Approved_Subs
    
    @p_Status NVARCHAR(25),
    @p_Year INT
    
    AS
    
    SELECT 
    	pv.VendorName as Subcontractor, 
    	pv.SentBy_Name as InvitedByName,
    	pv.SentTo_Name as ContactName,
    	al.ActionDate as DateApproved,
    	al.fk_Action as Status,	
    	al.ActionDate as ActionDate	
    FROM PreQual_Vendors pv
    LEFT OUTER JOIN ActionLog al ON  pv.pk_PreQual_Vendor = al.fk_PreQual_Vendor
    INNER JOIN 
    (SELECT MAX(Pk1) A_PK FROM ActionLog 
    
    WHERE 
    	 ExtraText = @p_Status AND YEAR(ActionDate) = @p_Year
    GROUP BY fk_PreQual_Vendor
    ) A2 ON A1.Pk1 = A2.A_PK
    ORDER BY  pv.VendorName
    	
    
    GO
    SET QUOTED_IDENTIFIER OFF 
    GO
    SET ANSI_NULLS ON 
    GO
    __________________
    Rate the posts that helped you

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: [RESOLVED] Select Last Record according to max date?

    Thanks alot both of you,

    I went with Rite's final solution.

  7. #7
    Hyperactive Member
    Join Date
    Jan 2008
    Location
    Merseyside
    Posts
    456

    Re: [RESOLVED] Select Last Record according to max date?

    no problem..

    Hey i've hit the 2 green cubes!

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: [RESOLVED] Select Last Record according to max date?

    Grats!

    I need to spread some more to hook up Rite. :P

  9. #9
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: [RESOLVED] Select Last Record according to max date?

    Quote Originally Posted by Besoup
    Grats!

    I need to spread some more to hook up Rite. :P
    you are making me fool since long time


    congrates kevchadders
    Last edited by riteshjain1982; Sep 2nd, 2008 at 10:05 AM.
    __________________
    Rate the posts that helped you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width