Solved

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

Posted on 2016-09-09
2
58 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 34

Accepted Solution

by:
ste5an earned 500 total points
ID: 41792316
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
ID: 41792689
You rock dude
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

739 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