Showing posts with label period. Show all posts
Showing posts with label period. Show all posts

Monday, March 26, 2012

Grouping Data into Periods for Reporting

Hi there.

I am working on a set of reports where I am summing/averaging data elements based on what period they are in. For example, the report output should look something like this:

Period Sum May '07 41 April '07 14 Q2 '07 55 March '07 36 February '07 28 January '07 22 Q1 '07 86 June '07 N/A YTD '07 141 December '06 33 November '06 27 October '06 42 Q4 '06 102 September '06 58 August '06 84 July '06 52 Q3 '06 194 June '06 40 May '06 41 April '06 14 Q2 '06 95 March '06 67 February '06 38 January '06 N/A Q1 '06 105 YTD '06 496

For each of the items I am summing, all I have is a datetime of when the event happened. This is a relational database (not a cube), so I am struggling with how to create the 'buckets' based on period. I think the best way is to dynamically create the buckets based on a given date. Is there a way in RS that it can do this bucketing for you?

Thanks, Mike

There are several ways to "create buckets". I recently posted something here http://spacefold.com/lisa/post/Partition-Magic.aspx having discovered some of the SQL 2005 syntax that I never knew existed, which may help you in your explorations of non-cube data -- and there is also a PIVOT clause which is really neat.

When you look at it, it looks as though you can't dynamically figure out how many buckets you have and go for it, but you actually can, if you write a bit of very easy dynamic SQL. I was actually planning on posting about that today, having helped a co-worker do it!

But in RS, you can do this using a matrix layout control, which basically does the thing for you, albeit with (from my POV) some frustrating and counter-intuitive ways of thinking.

Look into the matrix data region first, if you need to display the buckets across, and if you don't like it look into the PIVOT clause to do this in SQL Server (NB: if your data source isn't SQL Server you don't have this T-SQL syntax but there are ways of getting around that if you need to <g>.)

If you need to display the buckets down, I think you have even easier ways to do it. What you seem to be showing (as I read your example table) is a table that has grouping and you've suppressed the detail rows that might ordinarily appear with each line (the dates for each event). OK so far?

The very simplest way to get what you want is to write your query like this:

SELECT MONTH(eventdate) AS M, Datepart(quarter,eventdate) AS Q, YEAR(eventdate) AS Y, eventtype, eventdate from YourEventTable

... now you can summarize by having appropriate groups on the first three calculated values that you see in this query.

The wrinkle that most people have when doing this particular thing is that they actually want the fiscal month, the fiscal quarter, and the fiscal year rather than the base values that you see here. So you generally want to write a couple of UDFs that pass in your first month of fiscal year to handle this properly. These can be a PITA but once you've written them appropriately for your situation, you are usually okay forever. Give a shout if you find you need help with this part. (I may be offline for about a week, but if it is urgent I'm sure many other people can help you with this).

So that's how you do it if your "buckets" are rows, as they seem to be from your example layout. If they are really columns, again, look into matrix and pivot.

>L<

|||

Hi Lisa.

Thanks so much for the great response. I was working on doing something similar, but there are a few more wrinkles (aren't there always?). As you mentioned, the buckets are rows, so that is good, but in this case, my columns can vary, so I need to use a matrix control.

1. The data set I need is for the current quarter (based on getdate()) and the previous 5 full quarters. I think this can be easily handled in the where clause, so that should be ok.

2. The matrix control is strange in that when you add row groups, it actually adds them as columns on the report itself (I posted a question on this recently). My customer doesn't want it that way, so I have to get the dataset to match the control - meaning, I will need to have my dataset return the summed/averaged bucket rows and NOT have the control handle the buckets. So, I think I am stuck.

Can you see any way around that?

Thanks, Mike

|||

Hi again, Mike,

I can't actually think closely about what you're stuck on here, because (I think I said) I'm leaving on a trip and 'way late on preparing <s>. But, in fact, a PIVOT clause might be just the ticket here -- for this reason among others I did end up posting a blog entry about that. http://spacefold.com/lisa/post/Matrix-Rebuilt-More-non-standard-fun-with-T-SQL.aspx

It's discussing some aspects of the clause that you may or may not be interested it but it will give you some idea of the scope of what it can help you accomplish. In your case -- since you actually know how many buckets there are (6 quarters) -- it may be especially apt.

I'll be back in a week, if you haven't got what you need by then I'll do my best to help <s>. Look forward to reading whatever else has been posted on this thread by then!

>L<

|||

Thanks Lisa.

I'll check into it. Enjoy your time 'away'.

- Mike

Grouping data by time period

Hi everyone,

I'm now on a TSQL problem and hope somebody here can help me.
I have a log data for a website that logs IP addresses and date/time of the access, and I shall group IP values by time period, beginning from the 1st access, at each 30 minutes.

Does anyone here can help me with this issue? Any tips will be very appreciated.

TIA

MarceloHere is a generic code that you should get you started.
declare @.interval int
set @.interval=30
select dateadd(minute,floor(datediff(minute,0,OrderDate)/@.interval)*@.interval,0) [dt],
count(*) [cnt]
from Northwind..Orders
group by dateadd(minute,floor(datediff(minute,0,OrderDate)/@.interval)*@.interval,0)
order by 1|||Thanks a lot, worked like a charm. Only thing I need to figure out now is how to count 30 minutes since first access from an IP address, but your code will help me a lot.

Regards,

Friday, March 23, 2012

Grouping By Date Period

I'm using SQL Server 2000.

Example table:
PeopleID Date Status
1 2004-01-01 True
1 2005-01-01 True
1 2006-01-01 True
2 2004-01-01 True
2 2005-01-01 False
2 2006-01-01 True

I'm trying to find a way to query whether or not someone has had a specific status for 3 years in a row. As you can see from the table above, PeopleID 1 has had a "Status" of "True" for 3 years in a row, whereas PeopleID 2 hasn't--there was one year where they had "False".

I'm wondering I can query this, or if I'm going to have to scan the records manually. :(

I suppose I could write a stored procedure and do some looping too.

Appreciate any help, thanks!If each person only has one entry per year something like this may work...

SELECT PeopleID
FROM myTable
WHERE Date > DateAdd(yyyy, -3 getdate())
AND Status = 'True'
GROUP BY PeopleID
HAVING Count(PeopleID) = 3|||This should work if N always = 3:
select YT1.PeopleID
from [YourTable] YT1
inner join [YourTable] YT2
on YT1.PeopleID = YT2.PeopleID
and YT1.Status = YT2.Status
and year(YT1.Date) = year(YT2.Date) - 1
inner join [YourTable] YT3
on YT2.PeopleID = YT3.PeopleID
and YT2.Status = YT3.Status
and year(YT2.Date) = year(YT3.Date) - 1
where YT1.Status = 'True'

Grouping and Filters

Hi everyone

I am using SSRS2005 with an SSAS cube building in BI

I need to create a custom grouping. Here's what i mean:

I give my period parameter some default Values. Like :
Period = 200501,200502,200601,200602
Now when building the report, I filter 2 Tables on the 2 years respectively.
Grouped by Period

So one table list all the Measures for 2005 and the other for 2006.
Now I want to use a Chart to Display the two totals. I can only get the Chart to
display the by monthley periods. IE:

30 o
|--|-||
20 o o
|--|-||
10 o
|--|-||
200501 200502 200601 200602 (instead of 2005 and 2006 as I need)

I need to create a grouping by which i can tell the chart what data to use.
I can't group by period.year because the Period field is an Integer

Any help is greatly appreciated
If I am unclear about anything please point it out to me

Thanks in advance
Gerhard Davids

Ok

So I sorted this out and it seems I was being really retarded.

I used the following statments in the grouping of the Chart.

Series group : =iif(Left(CStr(Fields!Period.Value),4) = "2004", 2004, iif(Left(CStr(Fields!Period.Value),4) = "2005",2005,iif(Left(CStr(Fields!Period.Value),4) = "2006",2006,Nothing)))

Category group : =iif(Right(Cstr(Fields!Period.Value),2) = "01" ,01,iif(Right(Cstr(Fields!Period.Value),2) = "02",02,iif(Right(Cstr(Fields!Period.Value),2) = "03",03,iif(Right(Cstr(Fields!Period.Value),2) = "04",04,iif(Right(Cstr(Fields!Period.Value),2) = "05",05,iif(Right(Cstr(Fields!Period.Value),2) = "06",06,iif(Right(Cstr(Fields!Period.Value),2) = "07",07,iif(Right(Cstr(Fields!Period.Value),2) = "08",08,iif(Right(Cstr(Fields!Period.Value),2) = "09",09,iif(Right(Cstr(Fields!Period.Value),2) = "10",10,iif(Right(Cstr(Fields!Period.Value),2) = "11",11,iif(Right(Cstr(Fields!Period.Value),2) = "12",12,iif(Right(Cstr(Fields!Period.Value),2) = "13",13,Nothing)))))))))))))

This allowed it to group the periods together but seperate for each year
and in the series explanation it gave me the total for
each year respectiveley.

In the data section I then simply sumed my measure

G

Wednesday, March 7, 2012

Group by datetime

Hi,
How can i group the datetime field with different time period in same day
e.g 2006/1/23 07:00:00 12
2006/1/23 07:01:00 10
result : 2006/1/23 22
Thanks & Best Regards,
SexballYou would need to chop the time portion off...
SELECT CAST(CONVERT(char, YourDateCol, 112) AS datetime), SUM(YourInt)
FROM TableName
GROUP BY CAST(CONVERT(char, YourDateCol, 112) AS datetime)
HTH. Ryan
"sexball" <sexball@.sexball.com> wrote in message
news:Ohfnc6AIGHA.1192@.TK2MSFTNGP11.phx.gbl...
> Hi,
> How can i group the datetime field with different time period in same day
> e.g 2006/1/23 07:00:00 12
> 2006/1/23 07:01:00 10
> result : 2006/1/23 22
>
> Thanks & Best Regards,
> Sexball
>|||Hi
Just try this:
select <date-field>, count(*)
from <table>
group by convert(varchar(10), <date-field>, 101)
Please let me know if you have any questions
best Regards,
Chandra
http://chanduas.blogspot.com/
http://www.SQLResource.com/
---
"sexball" wrote:

> Hi,
> How can i group the datetime field with different time period in same day
> e.g 2006/1/23 07:00:00 12
> 2006/1/23 07:01:00 10
> result : 2006/1/23 22
>
> Thanks & Best Regards,
> Sexball
>
>|||The result will be the error .. "<date-field> is invalid in the SELECT list
as it's not part of the GROUP BY"...
You'll need to say :-
select convert(varchar(10), <date-field>, 101), count(*)
from <table>
group by convert(varchar(10), <date-field>, 101)
But this will return a count of the rows returned not the SUM value the
poster was after.
select convert(varchar(10), <date-field>, 101), SUM(intvalue)
from <table>
group by convert(varchar(10), <date-field>, 101)
HTH. Ryan
"Chandra" <chandra@.discussions.microsoft.com> wrote in message
news:5744C110-1B28-440B-A17A-F4B14B026F72@.microsoft.com...
> Hi
> Just try this:
> select <date-field>, count(*)
> from <table>
> group by convert(varchar(10), <date-field>, 101)
> Please let me know if you have any questions
> --
> best Regards,
> Chandra
> http://chanduas.blogspot.com/
> http://www.SQLResource.com/
> ---
>
> "sexball" wrote:
>|||Try this,
CREATE TABLE #TEST(id1 int, date1 datetime)
INSERT INTO #TEST VALUES (1,GETDATE())
WAITFOR DELAY '00:00:02'
INSERT INTO #TEST VALUES (2,GETDATE())
WAITFOR DELAY '00:00:02'
INSERT INTO #TEST VALUES (3,GETDATE())
select * from #TEST
SELECT CAST(FLOOR(CAST( date1 AS float)) AS DATETIME),SUM(id1) FROM #TEST
GROUP BY
CAST(FLOOR(CAST( date1 AS float)) AS DATETIME)
HAVING COUNT(date1) > 1
DROP TABLE #TEST
Thanks,
Sree
"sexball" wrote:

> Hi,
> How can i group the datetime field with different time period in same day
> e.g 2006/1/23 07:00:00 12
> 2006/1/23 07:01:00 10
> result : 2006/1/23 22
>
> Thanks & Best Regards,
> Sexball
>
>