Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Max Function

Posted on 2011-10-27
1
Medium Priority
?
216 Views
Last Modified: 2012-05-12
I am trying to build a Function that returns the PlacerUserKey that is associated with the most recent record in a table.  How do I go about returning the User record that has the most recent Created date?  The function below does not work.  Thank you.


CREATE FUNCTION dbo.GetCurrentPlacer
	(
	@OrderKey
	)
RETURNS INT
AS
	BEGIN
	DECLARE @CurrentPlacer VARCHAR

	

	SELECT @CurrentPlacer = (MAX(Created) AS CurrCreated, PlacerUserKey
FROM            Placer AS pl
WHERE        (OrderKey = @OrderKey)
GROUP BY Created, PlacerUserKey
ORDER BY Created DESC)
	

	RETURN @CurrentPlacer
	END

Open in new window

0
Comment
Question by:rcowen00
1 Comment
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 37041851
I think this article would help:
http://www.experts-exchange.com/A_3203.html


CREATE FUNCTION dbo.GetCurrentPlacer
        (
        @OrderKey
        )
RETURNS INT
AS
        BEGIN
        DECLARE @CurrentPlacer VARCHAR

        
        SELECT TOP 1 @CurrentPlacer = PlacerUserKey
          FROM Placer AS pl
         WHERE OrderKey = @OrderKey
      ORDER BY Created DESC
        
        RETURN @CurrentPlacer
        END

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

580 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