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)
 t-sql question

Author  Topic 

sqlserverdeveloper
Posting Yak Master

243 Posts

Posted - 2009-10-14 : 13:39:02
I have the following data, I want TO calculate the MTD(MONTH specified) AND TTM(Trailing Tweleve months) discounts offered FOR a particular product FOR a given MONTH AND YEAR.

--DROP TABLE #Temp
CREATE TABLE #Temp
(ID INT IDENTITY(1,1),
MONTH VARCHAR(20),
YEAR VARCHAR(10),
ProductID VARCHAR(20),
discounts MONEY)

INSERT INTO #Temp(MONTH, YEAR, ProductID,discounts)
SELECT 'Jan','2008','Prod1',100.00
UNION
SELECT 'Feb','2008','Prod21',10.00
UNION
SELECT 'Mar','2008','Prod36',20.00
UNION
SELECT 'Apr','2008','Prod44',30.00
UNION
SELECT 'May','2008','Prod50',46.00
UNION
SELECT 'Jun','2008','Prod69',55.00
UNION
SELECT 'Jul','2008','Prod77',66.00
UNION
SELECT 'Aug','2008','Prod8',87.00
UNION
SELECT 'Sep','2008','Prod9',110.00
UNION
SELECT 'Oct','2008','Prod030',22.00
UNION
SELECT 'Nov','2008','Prod110',34.00
UNION
SELECT 'Dec','2008','Prod320',190.00
UNION
SELECT 'Jan','2009','Prod1',10.00
UNION
SELECT 'Feb','2009','Prod21',50.00
UNION
SELECT 'Mar','2009','Prod36',20.00
UNION
SELECT 'Apr','2009','Prod44',33.00
UNION
SELECT 'May','2009','Prod50',46.00
UNION
SELECT 'Jun','2009','Prod69',55.00
UNION
SELECT 'Jul','2009','Prod77',69.00
UNION
SELECT 'Aug','2009','Prod8',87.00
UNION
SELECT 'Sep','2009','Prod9',119.00
UNION
SELECT 'Oct','2009','Prod030',22.00
UNION
SELECT 'Nov','2009','Prod110',34.00

SELECT * FROM #Temp
   

- Advertisement -