Solved

Sum total current month & same last year

Posted on 2007-03-22
2
687 Views
Last Modified: 2011-09-20
Hello,

Here is my sql:

SELECT
StoreID, YEAR(DatePickedUp) AS ROYear,
SUM(BilledHrsNonWarranty + BilledHrsWarranty + BilledHrsStock) as TotalHours,
SUM(PartsAmtNonWarranty + PartsAmtWarranty + PartsAmtStock) as TotalParts,
SUM(SubletAmtNonWarranty + SubletAmtWarranty + SubletAmtStock) as TotalSublet,
SUM(LaborAmtNonWarranty +  LaborAmtWarranty +  LaborAmtStock) as TotalLabor,
COUNT(RONumber) AS ROCount, SUM(Deposit1 + Deposit2 + Deposit3 + Deposit4 + PaymentAmt1 + PaymentAmt2 + PaymentAmt3) AS TotalROAmount

FROM
ROSummary

WHERE Year(DatePickedUp) BETWEEN 2006 And 2007
GROUP BY
StoreID, YEAR(DatePickedUP)

I'm trying to get the total labor for the current month and current month 1 year ago.
This is being used in SQL 2005 reporting services.

Thanks

Dan
0
Comment
Question by:DanPerlman
2 Comments
 
LVL 18

Expert Comment

by:chrismc
ID: 18773751
Group by month and in the where, just choose this month and the month 1 year ago.

Where (Year(GetDate()) = Year(DatePickedUp) Or Year(GetDate())-1 = Year(DatePickedUp)) And Month(GetDate()) = Month(DatePickedUp))

Cheers
Chris
0
 
LVL 2

Accepted Solution

by:
JimyLee earned 500 total points
ID: 19956678
If you are trying to get the previous year's total in the same result row as this year's data, something like this may do the trick for you.

SELECT
StoreID, YEAR(DatePickedUp) AS ROYear,
SUM(BilledHrsNonWarranty + BilledHrsWarranty + BilledHrsStock) as TotalHours,
SUM(PartsAmtNonWarranty + PartsAmtWarranty + PartsAmtStock) as TotalParts,
SUM(SubletAmtNonWarranty + SubletAmtWarranty + SubletAmtStock) as TotalSublet,
SUM(LaborAmtNonWarranty +  LaborAmtWarranty +  LaborAmtStock) as TotalLabor,
(select SUM(LaborAmtNonWarranty +  LaborAmtWarranty +  LaborAmtStock) from ROSummary where year(DatePickedUp) = 2006 and StoreID = ROS.StoreID) PrevYearTotalLabor
COUNT(RONumber) AS ROCount, SUM(Deposit1 + Deposit2 + Deposit3 + Deposit4 + PaymentAmt1 + PaymentAmt2 + PaymentAmt3) AS TotalROAmount
FROM ROSummary ROS
WHERE Year(DatePickedUp) = 2007
GROUP BY StoreID, YEAR(DatePickedUP)

Hope I didn't leave out anything in there.  Not at my normal computer today.  Hope it helps.

JimyLee
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

How to increase the row limit in Jasper Server.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

786 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