Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

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

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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 …
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 information from SQL Server on Database, Connection and Server properties

722 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