Solved

SQL - Need to change this SP to sum all previous years

Posted on 2016-09-09
2
40 Views
Last Modified: 2016-09-10
I need help getting this to work.  I need to get a sum value for all previous years without double counting any cylinder gas weight.

Example:

Cyl1 has a weighing event in each of the previous 5 years and this year.
The only weighing event that should be counted towards the total sum is the most recent weighing event LAST year.

Cyl2 had a weighing event 5 years ago and 4 years ago.
The only weighing event that should be counted towards the total sum is the one 4 years ago with the max Date for that year.

Note: All cylinders may be weighed more than once a year so it has to be the Max Date for the year used.

I also need a variant of this SP that includes the most recent weighing event including current year.




IF EXISTS (SELECT * FROM SYSOBJECTS WHERE ID = OBJECT_ID(N'[dbo].[usp_SF6_GetCylinderBOY]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE [dbo].[usp_SF6_GetCylinderBOY]
GO
CREATE PROCEDURE [dbo].[usp_SF6_GetCylinderBOY] 
	@CompanyID int,
	@YearPartDate datetime
AS 
BEGIN

SELECT SUM(GasWeight) 
FROM ( 
   select we.ContainerID, we.GasWeight, 
      row_number() over (partition by we.ContainerID order by we.DateStamp desc) rn
   FROM [WeighingEvents] we
      JOIN [Container] c ON we.[ContainerID] = c.[ContainerID] 
   WHERE c.ContainerTypeID = 1 AND we.[CompanyID] = @CompanyID AND we.[Active] = 1 AND (YEAR(we.[DateStamp]) = YEAR(@YearPartDate))
              ) sq
WHERE rn = 1
	
END
GO

Open in new window

0
Comment
Question by:CAMPzxzxDeathzxzx
2 Comments
 
LVL 32

Accepted Solution

by:
Stefan Hoffmann earned 500 total points
Comment Utility
Sounds like you're looking for:

ALTER PROCEDURE dbo.usp_SF6_GetCylinderBOY
    @CompanyID INT ,
    @YearPartDate DATETIME
AS 
    SET NOCOUNT ON;

    WITH    Filtered
              AS ( SELECT   WE.ContainerID ,
                            WE.GasWeight ,
                            ROW_NUMBER() OVER ( PARTITION BY WE.ContainerID ORDER BY WE.DateStamp DESC ) AS RN
                   FROM     WeighingEvents WE
                            INNER JOIN Container C ON WE.ContainerID = C.ContainerID
                   WHERE    C.ContainerTypeID = 1
                            AND WE.CompanyID = @CompanyID
                            AND WE.Active = 1
                            AND WE.DateStamp < DATEADD(YEAR, DATEDIFF(YEAR, 0, @YearPartDate), 0)
                 )
        SELECT  SUM(F.GasWeight)
        FROM    Filtered F
        WHERE   F.RN = 1 
GO

Open in new window

0
 

Author Closing Comment

by:CAMPzxzxDeathzxzx
Comment Utility
You rock dude
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now