Solved

Sum total current month & same last year

Posted on 2007-03-22
2
682 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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Hello, In my precious Article  (http://www.experts-exchange.com/Database/Reporting/A_15280-Create-Project-in-Microstrategy-Part-I.html)we saw the Configuration part for Microstrategy which included Metadata Creation and DataSource Preparation as …
I recently went through setting up a JasperReports Server using the AWS EC2 instance, and this article will cover some basic administration tasks I had to perform.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet 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