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
 General SQL Server Forums
 New to SQL Server Programming
 Query Strategy

Author  Topic 

mukhan85
Starting Member

46 Posts

Posted - 2009-06-30 : 11:51:53
Hi folks,
I have the following table:

NAME AVERAGE MIN/MAX
Jane 3.4 4
Mike 2.3 3
.
.

I need to generate above report for one Day, one Week, and one Month.
I am using Java to get the results, but all computations are done with SQL query( average computation, min/max).
The question is, is there a way to get dayly, weekly, and monthly reports by querying database once only, or do I need to query database each time for day, week, and month by changing the date range?

Thanks a lot!

rohitkumar
Constraint Violating Yak Guru

472 Posts

Posted - 2009-06-30 : 11:58:50
you'll be using case statements here
Go to Top of Page

mukhan85
Starting Member

46 Posts

Posted - 2009-06-30 : 12:04:24
how would I use them?

Thanks.
quote:
Originally posted by rohitkumar

you'll be using case statements here

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-06-30 : 12:17:10
Not with the data sample you listed here

Read the hint link below in my sig and reopost


Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

mukhan85
Starting Member

46 Posts

Posted - 2009-06-30 : 12:35:48
1.
I need to generate above report for one Day, one Week, and one Month.
I am using Java to get the results, but all computations are done with SQL query( average computation, min/max).
The question is, is there a way to get dayly, weekly, and monthly reports by querying database once only, or do I need to query database each time for day, week, and month by changing the date range?

2.
create table mytable(name varchar(10), number int, date DATE)

3.
insert into mytable (name, number, date)
select 'Jane', 4, 06/2009 union all
select 'Mike', 4, 06/2009 union all
select 'Jane', 4, 06/2009 union all
select 'Mike', 4, 06/2009 union all
.
.
.

select name, avg(number), min(number), max(number)
from mytable
where date = today
group by name.

I need the same report for one week and one month.
so, is there a way to get all data in one database query, I am using java to get all processed data from Database.
Thanks.


quote:
Originally posted by X002548

Not with the data sample you listed here

Read the hint link below in my sig and reopost


Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam





Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-30 : 12:44:08
yup..you need to apply group by over year, month and day and apply AVG(),MIN(),MAX(),... functions. can you show your exact o/p format so that i can show you query
Go to Top of Page

mukhan85
Starting Member

46 Posts

Posted - 2009-06-30 : 15:15:21
So, I need to get the statistics for one day, then for 7 days before today, and one month
I don't really care how the output will look like, it can either have 7 day , and one month results as a new two columns
or as a new row which would repeat the "name" again with one column that would indicate the rage.

Ex:
NAME AVERAGE(1 day) MIN/MAX(1 day) AVERAGE(7 days) MIN/MAX(7 days)
----- -------- ------- ------ ----------------
Jane 3.4 4 2.2 5
Mike 2.3 3 1.3 4

or

NAME AVERAGE MIN/MAX range
Jane 3.4 4 one_day
Mike 2.3 3 one_day
Jane 1.4 4 seven_day
Mike 3.3 3 seven_day
.
.

quote:
Originally posted by visakh16

yup..you need to apply group by over year, month and day and apply AVG(),MIN(),MAX(),... functions. can you show your exact o/p format so that i can show you query

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-07-01 : 12:23:34
[code]
SELECT DISTINCT name,
AVG(number*1.0) OVER (PARTITION BY DATEADD(dd,DATEDIFF(dd,0,date),0)) AS DayAvg,
AVG(number*1.0) OVER (PARTITION BY DATEADD(wk,DATEDIFF(wk,0,date),0)) AS WeekAvg,
AVG(number*1.0) OVER (PARTITION BY DATEADD(mm,DATEDIFF(mm,0,date),0)) AS MonthAvg
FROM table
[/code]
Go to Top of Page
   

- Advertisement -