SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


roll up the data as per period between start and enddate


roll up the data as per period between start and enddate

Author
Message
sdennis
sdennis
SSC Rookie
SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)

Group: General Forum Members
Points: 41 Visits: 264
Hi

I have a query that returns aggregated data for a day between start and end date . Now My manager says he would send the start and end date and wants to rollup or aggregate the data to either month ,quarter or year as per the dates on the same data as per the parameter period(month,day,year ,quarter) that is sent .

Suppose he sends startdate = 06/20/2012 and enddate = 06/20/2013 and period = month then he wants to aggregate the data for every month Until the end date .
if period = quarter then he wants to see the aggregate for every 3months data from start to end date

below is the query

;with Vol_Factdata as

(Select CONVERT(char(10),EventStartDate,126) as Eventstartdate,
Agg.ProductTypeID,
Agg.ProductTypeName,
Agg.LoanProgramTypeId,
MIN(MetricValue) AS MetricValall,
SUM(SampleSize) as SampleSizeall

FROM [Reporting].[AggregatedRatesforWidgets] Agg with (NOLOCK)
WHERE Agg.EventStartDate >= @ipstartdate
and Agg.EventStartDate <=@ipenddate

and Agg.LoanProgramTypeID= @ipProducttypeID
GROUP BY CONVERT(char(10),agg.EventStartDate,126),Agg.ProductTypeID,Agg.ProductTypeName,Agg.LoanProgramTypeId)

Select
vcy.EventStartDate as EventStartDate,
vcy.ProductTypeID as ProductTypeID,
vcy.ProductTypeName as ProductTypeName,
vcy.LoanProgramTypeID AS LoanProgramTypeId,
'ALL' as Loanstate,
vcy.MetricValall_1year as MetricValue,
vcy.SampleSizeall_1year as SampleSize

FROM Vol_Factdata vcy

Can some one suggest me a better way ?

Thanks&Regards
SC
Luis Cazares
Luis Cazares
SSC Guru
SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)

Group: General Forum Members
Points: 51947 Visits: 20016
Unfortunately, there's not much we can do without more details, DDL, sample data and expected results based on that sample data. These things will allow us to give you a better and faster answer. For more information, please read the article linked in my signature and come back again. ;-)


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
sdennis
sdennis
SSC Rookie
SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)

Group: General Forum Members
Points: 41 Visits: 264
I Understand.Anyway I could solve it. Thanks
Luis Cazares
Luis Cazares
SSC Guru
SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)

Group: General Forum Members
Points: 51947 Visits: 20016
I'm glad you could solved, I was working on a possible solution but I'm not sure that it will work for you. I'll share it anyway.


;with Vol_Factdata as
(
Select CASE WHEN @Period = 'Year' THEN DATEPART(YEAR, EventStartDate)
WHEN @Period = 'Quarter' THEN DATEPART( QUARTER, EventStartDate)
WHEN @Period = 'Month' THEN DATEPART( MONTH, EventStartDate)
END as Period,
Agg.ProductTypeID,
Agg.ProductTypeName,
Agg.LoanProgramTypeId,
MIN(MetricValue) AS MetricValall,
SUM(SampleSize) as SampleSizeall
FROM [Reporting].[AggregatedRatesforWidgets] Agg with (NOLOCK)
WHERE Agg.EventStartDate >= @ipstartdate
and Agg.EventStartDate <=@ipenddate
and Agg.LoanProgramTypeID= @ipProducttypeID
GROUP BY CASE WHEN @Period = 'Year' THEN DATEPART(YEAR, EventStartDate)
WHEN @Period = 'Quarter' THEN DATEPART( QUARTER, EventStartDate)
WHEN @Period = 'Month' THEN DATEPART( MONTH, EventStartDate)
END,
Agg.ProductTypeID,
Agg.ProductTypeName,
Agg.LoanProgramTypeId
)
Select
vcy.Period as Period,
vcy.ProductTypeID as ProductTypeID,
vcy.ProductTypeName as ProductTypeName,
vcy.LoanProgramTypeID AS LoanProgramTypeId,
'ALL' as Loanstate,
vcy.MetricValall_1year as MetricValue,
vcy.SampleSizeall_1year as SampleSize
FROM Vol_Factdata vcy



By the way, I noticed the NOLOCK hint you're using and you should be aware that the data returned by your query might be incorrect due to it.
Understanding the SQL Server NOLOCK hint
SQL Server NOLOCK Hint & other poor ideas.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
sdennis
sdennis
SSC Rookie
SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)

Group: General Forum Members
Points: 41 Visits: 264
This Looks very good.I did not use case statements.So,this is much simpler.Im going to try this one.ThankyouSmile
sdennis
sdennis
SSC Rookie
SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)

Group: General Forum Members
Points: 41 Visits: 264
Your code was close to what I needed except for the eventstartdate need to be in date format(Not just year or month or quarter number)

BEGIN


;with Vol_Aggregatedata as

(Select CASE WHEN @ipperiod = 'day' THEN CONVERT(char(10),EventStartDate,126)
WHEN @ipperiod = 'week' THEN dateadd(week, datediff(week, 0, Agg.EventStartDate), 0)
WHEN @ipPeriod = 'quarter' THEN dateadd(qq, datediff(qq, 0, Agg.EventStartDate), 0)
WHEN @ipPeriod = 'month' THEN DATEADD(MONTH, DATEDIFF(MONTH, 0, Agg.EventStartDate), 0)
END as Eventstartdate,
Agg.ProductTypeID,
Agg.ProductTypeName,
Agg.LoanProgramTypeId,
MIN(MetricValue) AS MetricValall ,
SUM(SampleSize) as SampleSizeall

FROM [Reporting].[AggregatedRatesforWidgets] Agg with (NOLOCK)
WHERE Agg.EventStartDate >= @ipstartdate
and Agg.EventStartDate <=@ipenddate
and Agg.LoanProgramTypeID= @ipProgramtypeID
GROUP BY CASE WHEN @ipperiod = 'day' THEN CONVERT(char(10),EventStartDate,126)
WHEN @ipperiod = 'week' THEN dateadd(week, datediff(week, 0, Agg.EventStartDate), 0)
WHEN @ipPeriod = 'quarter' THEN dateadd(qq, datediff(qq, 0, Agg.EventStartDate), 0)
WHEN @ipPeriod = 'month' THEN DATEADD(MONTH, DATEDIFF(MONTH, 0, Agg.EventStartDate), 0)
END
,Agg.ProductTypeID,Agg.ProductTypeName,Agg.LoanProgramTypeId
)

Select
vcy.EventStartDate as EventStartDate,
vcy.ProductTypeID as ProductTypeID,
vcy.ProductTypeName as ProductTypeName,
vcy.LoanProgramTypeID AS LoanProgramTypeId,
'ALL' as Loanstate,
vcy.MetricValall as MetricValue,
vcy.SampleSizeall as SampleSize

FROM Vol_Aggregatedata vcy
order by Eventstartdate ,loanstate

END
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search