I need max date and max time

I need this SP to return the 102.8 and not the 106.2

IF EXISTS (SELECT * FROM SYSOBJECTS WHERE ID = OBJECT_ID(N'[dbo].[usp_Get_PreviousGasWeight]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE [dbo].[usp_Get_PreviousGasWeight];

CREATE PROC [dbo].[usp_Get_PreviousGasWeight] 
		@ContainerID int,
		@CompanyID int
	SELECT [GasWeight] 
	FROM [dbo].[WeighingEvents]
	WHERE [ContainerID] = @ContainerID AND [CompanyID] = @CompanyID AND [Active] = 1
	ORDER BY [DateStamp] DESC

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Scott PletcherSenior DBACommented:
Verify the contents of these columns in the table:

There must be something preventing the row you want and expect from being considered by the query.  Not active?!
PortletPaulEE Topic AdvisorCommented:
You have ORDER BY [DateStamp] DESC so 2017-03-08 11:40 (102.8) is later than 2017-03-08 10:00 (106.2)

You also seem to only want one value... but the query isn't limited to one row. Do you need MAX() perhaps?

      SELECT MAX( [GasWeight] )
      FROM [dbo].[WeighingEvents]
      WHERE [ContainerID] = @ContainerID AND [CompanyID] = @CompanyID AND [Active] = 1
      /* ORDER BY [DateStamp] DESC */
PortletPaulEE Topic AdvisorCommented:
While I'm not sure what you really want, perhaps this will help:
          , ROW_NUMBER() OVER (ORDER BY [DateStamp] DESC) AS rn
      FROM [dbo].[WeighingEvents]
      WHERE [ContainerID] = @ContainerID
      AND [CompanyID] = @CompanyID
      AND [Active] = 1) AS d
WHERE rn = 1

Open in new window


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
CAMPzxzxDeathzxzxAuthor Commented:
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.