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.
Author |
Topic |
soorma
Yak Posting Veteran
52 Posts |
Posted - 2009-10-21 : 14:12:51
|
I am trying to get the report like thisWeekStartDate average min max median Totalrecords Attmrec1/1/2009 3 5 5 4 200 3001/7/2009 5 8 9 1 300 500This is how the data looks like in the temp tabletotalattempts leadid50 500012 4515 40007This is what i have done until nowALTER PROCEDURE [dbo].[test] -- Add the parameters for the stored procedure here@StartDate varchar(12),@EndDate varchar(12) ASBEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for procedure hereIF object_id('tempdb..#TmpData') IS NOT NULL Begin Drop Table #TmpData EndCREATE TABLE #TmpData (TotalAttempts int NULL,[LeadID] VARCHAR(100) NULL )INSERT INTO #TmpData (TotalAttempts, leadid)select count(I3_RowID) as total,I3_RowIDFrom test whereCallDate between @StartDate and @EndDate Group by i3_rowid order by totalSELECT [avg] = Avg(TotalAttempts), [min] = Min(TotalAttempts), [max] = Max(TotalAttempts), TotalRecords=count(leadid), AttemptedTotalRecords=sum(Totalattempts), startdate=@StartDate FROM #TmpDatawhen i run the above SPit looks likeWeekStartDate average min max median Totalrecords Attmrec1/1/2009 3 5 5 4 200 300 I how can i add the median and group by weeks in the above query |
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-10-21 : 15:32:36
|
Here's one way. You will need to add a column to #tmpData: [RowID] int identity(1,1)And be sure to INSERT to #tmpData ORDER BY [callDate]select [avg] ,[min] ,[max] ,avg(t.TotalAttempts) as median ,totalRecords ,AttemptedTotalRecords ,startDate ,datename(weekday, startDate)from ( SELECT [avg] = Avg(TotalAttempts) ,[min] = Min(TotalAttempts) ,[max] = Max(TotalAttempts) ,TotalRecords = count(leadid) ,AttemptedTotalRecords = sum(Totalattempts) ,startDate = dateadd(week, datediff(week, 0, callDate), 0) --helpers for Median ,min(rowid) + convert(int, ceiling(count(*) / 2.0)) - 1 medID ,1 - (count(*)%2) evenOdd FROM #TmpData group by dateadd(week, datediff(week, 0, callDate), 0) ) dinner join #tmpData t on t.rowid >= d.medid and t.rowid <= medid+evenOddgroup by [avg] ,[min] ,[max] ,totalRecords ,AttemptedTotalRecords ,startDate Be One with the OptimizerTG |
|
|
soorma
Yak Posting Veteran
52 Posts |
Posted - 2009-10-22 : 12:44:48
|
This is still no working. i changed a little bit of query. but i am still not able to calculate the median. i am not using a temp table anymore. i am just querying against the table now.here is the sql Select Week, avg(total) as average, min(total) as minimum, max(total) as maximum, SUM(Total) as AttemptedTotalRecords, sum(lead) as TotalRecordsFROM (select week=DATEADD(wk, DATEDIFF(wk, 7, calldate), 7), count(I3_RowID) as total, 1 As LeadFrom test where (CallDate between '10/1/09' and '10/22/09') and (reason<>'deleted' or reason<>'wrongparty' or reason<>'Success' or reason<>'Failure')Group by DATEADD(wk, DATEDIFF(wk, 7, calldate), 7), i3_rowid ) aGroup by Week order by week |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-10-22 : 13:01:05
|
Did you try the solution I provided? What specifically was the problem?Be One with the OptimizerTG |
|
|
soorma
Yak Posting Veteran
52 Posts |
Posted - 2009-10-22 : 18:00:51
|
if i run the query that inserts the data in the temp table the results is like thistotal calldate1 2009-10-01 07:00:29.0001 2009-10-01 07:00:29.0001 2009-10-05 17:48:12.0001 2009-10-05 17:48:12.000This is the queryselect count(I3_RowID) as total,calldateFrom test whereCallDate between '10/1/09' and '10/22/09' group by I3_RowID,calldateorder by calldateIf i run the diff query like this select count(I3_RowID) as total,I3_RowIDFrom CallHistory whereCallDate between '10/1/09' and '10/22/09' Group by i3_rowid order by totaltotal i3_rowid1 54 452 14In ur solution ur are using calldate which i am not inserting in the temp table. |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-10-23 : 09:53:45
|
Ok - if you still want help, please post the structure of [test] and include a little sample data. If i3_rowid is an int column with a contiguous series of sequential values then you're right, you don't need the temp table.Be One with the OptimizerTG |
|
|
|
|
|
|
|