views:

54

answers:

2

I have table that contain name and date.

I need to count how many names I have in a one day, and make average to all days.

How to do it?

Thanks in advance

A: 

I don't have a sql processor to hand but something like

select dateadd(dd,0, datediff(dd,0,[date])), count(name)
from your_table
group by dateadd(dd,0, datediff(dd,0,[date])) 

This should count the names on a particular date. Note dateadd(dd,0, datediff(dd,0,[date])) is there to eliminate the time portion of the date, I am assuming that there may be a time component and that you wanted to count all things on the day.

This should average the names over the day

select dateadd(dd,0, datediff(dd,0,[date])), avg(name)
from your_table
group by dateadd(dd,0, datediff(dd,0,[date])) 
Preet Sangha
What is the purpose of the `dateadd(dd,0, datediff(dd,0,[date]))`
JoshD
The query seems unrelated to the question for me.
TomTom
The dateadd() calls are to remove any time portion.
nickd
+3  A: 

To get daily counts:

SELECT COUNT(name) AS c, date
FROM table
GROUP BY date

For the average:

SELECT AVERAGE(c)
FROM (SELECT COUNT(name) AS c, date
    FROM table
    GROUP BY date)
JoshD
Note sure this is what the user asks for logically, but it does answer the question how I read it, too.
TomTom
@TomTom: Yeah, it doesn't seem clear what is asked, by this is the only thing that seems to fit his words.
JoshD
@JoshD, shouldn't it be `COUNT(DISTINCT name)`? Unless the combination of name and date is unique in the table, the existing query will return the average number of records (with non-null names) per day, not the average number of names.
Mark Bannister
@Mark Bannister: I'm not sure. The way I read it, he wanted entries per day average. You may be right; "make average to all days" leaves a lot of wiggle room. Without more clarification, I'm not sure.
JoshD
the part with the average dont work, why ?
Gold