| Author |
Topic |
|
bluemagic74
Starting Member
15 Posts |
Posted - 2011-12-06 : 19:55:59
|
| Hi,My table columns are – UserID, EntryVaule, EntryDateTimeOne user can enter more than 1 entries in a dayI am trying to write a SQL query to find these.Kind of new to this.. Please let me know. Thank you.AVG number of Total entries - total entries/total usersFrequency of Entries (count entries per user by week,month, year) |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-12-06 : 21:00:17
|
[code]select UserID, avg(EntryVaule)from Entriesgroup by UserIDselect UserID, dateadd(week, datediff(week, 0, EntryDateTime), 0), avg(EntryVaule)from Entriesgroup by UserID, dateadd(week, datediff(week, 0, EntryDateTime), 0)select UserID, dateadd(month, datediff(month, 0, EntryDateTime), 0), avg(EntryVaule)from Entriesgroup by UserID, dateadd(month, datediff(month, 0, EntryDateTime), 0)select UserID, dateadd(year, datediff(year, 0, EntryDateTime), 0), avg(EntryVaule)from Entriesgroup by UserID, dateadd(year, datediff(year, 0, EntryDateTime), 0)[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
bluemagic74
Starting Member
15 Posts |
Posted - 2011-12-06 : 23:20:12
|
| Thanks Khan!throwing below error.Msg 8117, Level 16, State 1, Line 2Operand data type varchar is invalid for avg operator.actually Entryvalue column has data like- 5, 1.4, 5000, 500/5000, 10/100, 10.5, 12 like this datatype is varchar(10). |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-12-06 : 23:30:38
|
quote: actually Entryvalue column has data like- 5, 1.4, 5000, 500/5000, 10/100, 10.5, 12 like this datatype is varchar(10).
then how do you calculate the average value for such data ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-12-06 : 23:38:11
|
oh sorry, i misunderstood your requirement.this will give you the number of entry by dayselect UserID, date = dateadd(week, datediff(week, 0, EntryDateTime), 0), cnt = count(*)from Entriesgroup by UserID, dateadd(week, datediff(week, 0, EntryDateTime), 0) to get avg by monthselect UserID, dateadd(month, datediff(month, 0, date), 0), avg(cnt)from( select UserID, date = dateadd(week, datediff(week, 0, EntryDateTime), 0), cnt = count(*) from Entries group by UserID, dateadd(week, datediff(week, 0, EntryDateTime), 0)) mgroup by select UserID, dateadd(month, datediff(month, 0, date), 0) KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|
|