Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-09-09
2
Medium Priority
?
74 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 36

Accepted Solution

by:
ste5an earned 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
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…

916 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