| Author |
Topic |
|
BenBen
Starting Member
10 Posts |
Posted - 2010-04-21 : 15:10:23
|
| Hello All,I'm looking for some help. I am trying to calculate the following:Year over Year ChangeMonth over Previous Year's MonthWeek over Previous Year's WeekDay over Previous Year's DayMonth over Previous Month Same YearWeek over Previous Week Same YearThe Years: 2008, 2009, 2010Change In: The amount of requests from a ticketing system.Date Field: DateOfRequestWhat would be my best approach for a result set with the above calculations? Thank you in advance. |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2010-04-21 : 15:11:37
|
| It would help if you can provide the table structure, sample data and expected output. |
 |
|
|
BenBen
Starting Member
10 Posts |
Posted - 2010-04-21 : 16:02:50
|
| I would like to use the results in sql reporting services when I create charts. Here is the data.Let me know if you need anything else.Query for each below was from variations of the following query:SELECT COUNT(DATEPART(yy, DateRequest)) AS "New Requests", DATEPART(yy, DateRequest) AS YearFROM [DB].[dbo].[TicketSystem]GROUP BY DATEPART(yy, DateRequest)ORDER BY Year ascNew Requests, Week, Month, Year14,49,12,200826,50,12,200848,51,12,200823,52,12,200821,53,12,20084,1,1,200953,2,1,200989,3,1,200974,4,1,200965,5,1,200977,6,2,200976,7,2,200959,8,2,200973,9,2,200999,10,3,200996,11,3,200993,12,3,2009100,13,3,200929,14,3,200960,14,4,200988,15,4,2009104,16,4,200996,17,4,200972,18,4,20098,18,5,200981,19,5,200989,20,5,200993,21,5,200986,22,5,2009101,23,6,2009109,24,6,200985,25,6,2009103,26,6,200937,27,6,200954,27,7,2009104,28,7,200987,29,7,2009110,30,7,2009102,31,7,2009100,32,8,2009101,33,8,2009123,34,8,200992,35,8,200912,36,8,200990,36,9,200998,37,9,2009117,38,9,2009129,39,9,200983,40,9,200953,40,10,2009133,41,10,2009121,42,10,2009114,43,10,2009119,44,10,2009124,45,11,2009110,46,11,2009125,47,11,200955,48,11,200932,49,11,2009109,49,12,200998,50,12,2009121,51,12,200969,52,12,200958,53,12,200999,2,1,2010117,3,1,2010108,4,1,2010129,5,1,2010126,6,2,2010109,7,2,2010102,8,2,2010124,9,2,2010140,10,3,2010144,11,3,2010142,12,3,201099,13,3,201068,14,3,201048,14,4,201085,15,4,2010103,16,4,201059,17,4,2010New Requests, Month, Year132,12,2008285,1,2009285,2,2009417,3,2009420,4,2009357,5,2009435,6,2009457,7,2009428,8,2009517,9,2009540,10,2009446,11,2009455,12,2009453,1,2010461,2,2010593,3,2010295,4,2010New Requests, Year132,20085042,20091802,2010 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-21 : 23:51:37
|
| is this how data is stored or is this your reqd o/p format?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
BenBen
Starting Member
10 Posts |
Posted - 2010-04-22 : 01:17:39
|
| They are just the results of three queries I ran to show you what data I have. I guess I don't know what else you need?..... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-22 : 04:55:36
|
| what we need is how you want output to appear out of data above------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2010-04-22 : 07:19:17
|
| Follow the HOW TO ASK link in my signature, and it will tell you exactly what we need to help you.http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspxHow to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
BenBen
Starting Member
10 Posts |
Posted - 2010-04-22 : 15:22:19
|
| Step 1. How do I calculate the following calculations from a table?Year over yearMonth over same month previous yearWeek over same week previous yearDay over same day previous yearMonth over previous monthWeek over previous weekDay over previous dayStep 2.SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[eForm]( [TaskID] [int] IDENTITY(20000,1) NOT NULL, [DateRequest] [smalldatetime] NULL CONSTRAINT [PK_eForm] PRIMARY KEY CLUSTERED ( [TaskID] ASC)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]) ON [PRIMARY]GOSET ANSI_PADDING OFFStep 3. INSERT INTO [LForm].[dbo].[eForm] ,[DateRequest]) VALUES ,01/01/2008INSERT INTO [LForm].[dbo].[eForm] ,[DateRequest]) VALUES ,01/01/2009INSERT INTO [LForm].[dbo].[eForm] ,[DateRequest]) VALUES ,02/01/2008INSERT INTO [LForm].[dbo].[eForm] ,[DateRequest]) VALUES ,02/01/2009INSERT INTO [LForm].[dbo].[eForm] ,[DateRequest]) VALUES ,01/01/2010INSERT INTO [LForm].[dbo].[eForm] ,[DateRequest]) VALUES ,02/01/2010Step 4.SELECT COUNT(DATEPART(yy, DateRequest)) AS "New Requests", DATEPART(ww, DateRequest) AS Week,DATEPART(mm, DateRequest) AS Month,DATEPART(yy, DateRequest) AS YearFROM [LForm].[dbo].[eForm]WHERE DateRequest IS NOT NULLGROUP BY DATEPART(yy, DateRequest), DATEPART(mm, DateRequest),DATEPART(ww, DateRequest) ORDER BY Year asc, Month asc, Week ascStep 5.The result can be in any form and even from separate queries comparing different parts of the date. Or it can be all in one if that is even possible.For example: For Year over Year2009 2008500 Requests 250 RequestsThe YOY would be 500-250=250 250/250=11*100=100 percentYOY for 2009-2008 was 100 percentThe similiar for month and day of previous year.And if possible the percentage difference from the same year. For example, this week there was a percentage change increase of 25 percent compared to the previous week of this year.Again if such an analysis needs to be done in different queries, that is ok. Please execuse my low knowledge of SQL. Thank you. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-23 : 02:07:45
|
| will output be conditional based on some parameter? or all analysis shown in parallel?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
BenBen
Starting Member
10 Posts |
Posted - 2010-04-23 : 02:54:46
|
| all analysis shown in parallel |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-23 : 04:18:05
|
i'll show for year. rest you can do similarlySELECT COUNT(DATEPART(yy, DateRequest)) AS "New Requests", DATEPART(ww, DateRequest) AS Week,DATEPART(mm, DateRequest) AS Month,DATEPART(yy, DateRequest) AS YearINTO #TempFROM [LForm].[dbo].[eForm]WHERE DateRequest IS NOT NULLGROUP BY DATEPART(yy, DateRequest), DATEPART(mm, DateRequest),DATEPART(ww, DateRequest) ORDER BY Year asc, Month asc, Week ascSELECT Year,CntYr,CntPrev,(CntYr-CntPrev)*100.0/CntPrevFROM(SELECT Year, SUM([New Requests]) AS CntYrFROM #TempGROUP BY Year)mCROSS APPLY (SELECT SUM([New Requests]) AS CntPrev FROM #Temp WHERE Year=m.Year-1 )nDROP TABLE #Temp ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
BenBen
Starting Member
10 Posts |
Posted - 2010-04-23 : 04:49:10
|
| Thank you for the above.Question:I think that I need to be able to place two parameters into the script. As it stands it provides for data for one full year, but I don't know which year it is for. The YOY works fine. Im talking about when I do the MOM and WOW comparisons. For the MOM and WOW, how would you do it with two paramters (years)?I think that would do it.You are great. |
 |
|
|
BenBen
Starting Member
10 Posts |
Posted - 2010-04-23 : 05:01:03
|
| Im all set sir with the parameters.How would I compare one month over the previous year's same month?The script does great for YOY. MOM for months in same year, but not from the previous year.Does that make sense? |
 |
|
|
BenBen
Starting Member
10 Posts |
Posted - 2010-04-23 : 06:04:25
|
| Also, does the use of the temp table required? What I like to do it is use this as a dataset in SR so I can create a chart. I need fields to do that. Currently, it runs fine but no fields are actually there to use in design mode. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-23 : 06:10:18
|
quote: Originally posted by BenBen Thank you for the above.Question:I think that I need to be able to place two parameters into the script. As it stands it provides for data for one full year, but I don't know which year it is for. The YOY works fine. Im talking about when I do the MOM and WOW comparisons. For the MOM and WOW, how would you do it with two paramters (years)?I think that would do it.You are great.
you need only singe parameter for MOM and WOW. you will always compare data for @Year with @Year-1------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
BenBen
Starting Member
10 Posts |
Posted - 2010-04-23 : 09:13:34
|
| Thanks for your expertise sir. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-23 : 09:19:51
|
no problem. you're welcome ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|