April 22, 2008 at 10:53 pm
Hi,
I would like to create a report or view that outlines data in a particular format.
Month FirstCount SecondCount ThirdCount
June x y z
July z w z
August......
I would like the user to input the month, and then return information for that month (and successive months) based on different criteria for each column (ie. FirstColumn).
For example, the first column may count the number of dates that fall within that month, and the second column may calculate the number of employees active in that month.
What would be the best way to do this?
Thanks
April 23, 2008 at 12:00 am
COUNT()
GROUP BY
--Jeff Moden
Change is inevitable... Change for the better is not.
April 23, 2008 at 12:26 am
Thanks, I can use Count and Group By on one column with one set of criteria,
but I would like to have more than one column showing different values for that month (ie. each column will have a different set of criteria and use a different field to count from).
Thanks
April 23, 2008 at 12:56 am
[font="Verdana"]Post some real life data with desired o/p.
Mahesh[/font]
MH-09-AM-8694
April 23, 2008 at 2:44 am
It sounds like you might want to do a few joins...
e.g. (making something random up with a cte, I'm not saying this is good ;)):
declare @t_sample table(
[id] int
,[mnth] nvarchar(50)
,[some_number] int
)
insert into @t_sample values(6, 'June', 1)
insert into @t_sample values(6, 'June', 2)
insert into @t_sample values(7, 'July', 4)
insert into @t_sample values(7, 'July', 8)
insert into @t_sample values(7, 'July', 16)
insert into @t_sample values(8, 'August', 9)
;with [e]( [id], [mnth], [some_number] )
as ( select [id], [mnth], [some_number] from @t_sample )
select
e1.[mnth] [month]
,e2.[Count] [count_for_month]
,e3.[Average] [average_for_month]
,e4.[SomeTotal] [total_so_far]
from (
select distinct id, mnth from e
) e1
left join (
select id, count(*) [Count]
from e
group by id
) e2 on e2.id = e1.id
left join (
select id, avg(some_number) [Average]
from e
group by id
) e3 on e3.id = e1.id
left join (
select ea.id, sum(eb.some_number) [SomeTotal]
from (select distinct id from e) ea, e eb
where eb.id <= ea.id
group by ea.id
) e4 on e4.id = e1.id
order by e1.id
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply