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.
Author |
Topic |
JarWithLid
Starting Member
10 Posts |
Posted - 2007-12-20 : 18:11:20
|
The query is to get the average number of kbarticles written per month. It is accomplished by dividing the total number of articles written by the number of different months articles have been written in.SELECT CAST( (SELECT COUNT(1) FROM kbarticles) AS DECIMAL(2) ) / CAST( COUNT(1) AS DECIMAL(2)) AS perMonthAvgFROM ( SELECT count(1) as cnt FROM kbarticles GROUP BY (CAST(month(cdate) as varchar(2)) + cast(year(cdate) as varchar(4))) ) as total |
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2007-12-20 : 19:16:08
|
see if this is any faster:select count(1) / cast(count(distinct datediff(month, 0, cdate)) as decimal(2)) AS perMonthAvgfrom kbarticles Be One with the OptimizerTG |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-12-21 : 03:58:09
|
And you need to multiply the first count by 1.0 for accuracyselect count(1)*1.0 / cast(count(distinct datediff(month, 0, cdate)) as decimal(2)) AS perMonthAvgfrom kbarticlesMadhivananFailing to plan is Planning to fail |
 |
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2007-12-21 : 08:32:17
|
quote: Originally posted by madhivanan And you need to multiply the first count by 1.0 for accuracyselect count(1)*1.0 / cast(count(distinct datediff(month, 0, cdate)) as decimal(2)) AS perMonthAvgfrom kbarticles
I hate to contradict someone with a gajillion posts but...I beg to differ. You only need to set the one of the operators to a more precise datatype (I always use the denominator).Here is an exceprt from BOL topic "Data Type Precedence":quote: The data type with the lower precedence is converted to the data type with the higher precedence
compare the results of these 2 statememtns:select count(1)*1.0 / cast(count(distinct datediff(month, 0, refdate)) as decimal(2)) AS perMonthAvgfrom sysobjectsselect count(1) / cast(count(distinct datediff(month, 0, refdate)) as decimal(2)) AS perMonthAvgfrom sysobjects Be One with the OptimizerTG |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-12-21 : 10:15:35
|
quote: Originally posted by TG
quote: Originally posted by madhivanan And you need to multiply the first count by 1.0 for accuracyselect count(1)*1.0 / cast(count(distinct datediff(month, 0, cdate)) as decimal(2)) AS perMonthAvgfrom kbarticles
I hate to contradict someone with a gajillion posts but...I beg to differ. You only need to set the one of the operators to a more precise datatype (I always use the denominator).Here is an exceprt from BOL topic "Data Type Precedence":quote: The data type with the lower precedence is converted to the data type with the higher precedence
compare the results of these 2 statememtns:select count(1)*1.0 / cast(count(distinct datediff(month, 0, refdate)) as decimal(2)) AS perMonthAvgfrom sysobjectsselect count(1) / cast(count(distinct datediff(month, 0, refdate)) as decimal(2)) AS perMonthAvgfrom sysobjects Be One with the OptimizerTG Well. I thought you didnt cast it to decimal (I forgot to notice that) and FYI select 1/2*1.0select 1*1.0/2MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|