Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 month to date, year to date together...

Author  Topic 

squarefish
Starting Member

28 Posts

Posted - 2009-11-26 : 04:59:51
Hi there,

Is it possible to get 3 columns that show last month, month to date, year to date for sales for a users on one row?

I have data for each day against each sales exec that goes back to the beginning of the year showing how many items they sold.

Table(slData)
salesexec sales dateofsale
1 1 01/02/2009
1 0 02/02/2009
2 2 01/02/2009


then I have a table that shows info about the salesexec


table(slSalesmen)
salesexec Name location
1 Richard Location1
2 John Location2


I need to be able to show the following

Name Location Year to Date Lastmonth Month to date
Richard Location1 123 21 15

Is this possible in 1 view?
I could do it by having 1 view for lastmonth 1 view for ytd and 1 for mtd and one linking them all to the details, but there must be an easier way...?

Many Thanks

Richard

squarefish
Starting Member

28 Posts

Posted - 2009-11-26 : 05:37:43
Just had a thought, could it be done using an if?

if month(salesdate) = month(getdate()-1) then 1 else 0 - for lastmonth

if month(salesdate) = month(getdate()) then 1 else 0 - for thismonth

if year(salesdate) = year(getdate()) then 1 else 0 - for ytd

Can anyone help me with the syntax?

Thanks
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-11-26 : 05:49:33
Here comes the syntax:
select
month(dateadd(month,-1,getdate())) as lastmonth,
month(getdate()) as thismonth,
year(getdate()) as thisyear



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-11-26 : 06:04:56
Why stop there?
-- Prepare sample data. This is NOT part of the query!
DECLARE @slData TABLE
(
salesexec int,
sales int,
dateofsale datetime
)

SET DATEFORMAT MDY

INSERT @slData
SELECT 1, 1, '01/02/2009' UNION ALL
SELECT 1, 0, '02/02/2009' UNION ALL
SELECT 2, 2, '01/02/2009'

DECLARE @slSalesmen TABLE
(
salesexec int,
Name varchar(20),
location varchar(20)
)

INSERT @slSalesmen
SELECT 1, 'Richard', 'Location1' UNION ALL
SELECT 2, 'John', 'Location2'


-- Here is your view definition query
SELECT s.Name,
s.Location,
SUM(CASE WHEN d.DateOfSale >= s.ThisYear AND d.DateOfSale < s.Tomorrow THEN d.Sales ELSE 0 END) AS [Year to Date],
SUM(CASE WHEN d.DateOfSale >= s.LastMonth AND d.DateOfSale < s.ThisMonth THEN d.Sales ELSE 0 END) AS [Last month],
SUM(CASE WHEN d.DateOfSale >= s.ThisMonth AND d.DateOfSale < s.Tomorrow THEN d.Sales ELSE 0 END) AS [Month to Date]
FROM (
SELECT SalesExec,
Name,
Location,
DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 1) AS Tomorrow,
DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0) AS ThisYear,
DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) AS ThisMonth,
DATEADD(MONTH, DATEDIFF(MONTH, 31, GETDATE()), 0) AS LastMonth
FROM @slSalesMen
) AS s
LEFT JOIN @slData AS d ON d.SalesExec = s.SalesExec
GROUP BY s.SalesExec,
s.Name,
s.Location



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

squarefish
Starting Member

28 Posts

Posted - 2009-11-26 : 07:44:58
Brilliant as always...!

Thanks
Go to Top of Page
   

- Advertisement -