分区总和
我想念什么?这个查询一次又一次地返回重复的数据。对于一个完整的总数,计数是正确的,但我希望有一行,但我得到的值重复了大约40次。你有什么想法吗?
SELECT BrandIdSUM(ICount) OVER (PARTITION BY BrandId ) FROM Table WHERE DateId= 20130618我明白了吗?
BrandId ICount 42176222 421762222 4222121212121212121212172176262222222222222 我想念什么?
由于整个查询如下:
SELECT BrandIdSUM(ICount) OVER (PARTITION BY BrandId) TotalICount= SUM(ICount) OVER () SUM(ICount) OVER () / SUM(ICount) OVER (PARTITION BY BrandId)as PercentageFROM Table WHERE DateId= 20130618哪个返回:
BrandId (No column name) TotalICount Percentage2 421762 32239892 762 421762 32239892 762 421762 32239892 762 421762 32239892 762 421762 32239892 762 421762 32239892 76我希望这种输出不需要使用不同的东西:
BrandId (No column name) TotalICount Percentage 3222222222222222222222222223222322323232
解决方案:
您可以使用DISTINCT删除这些PARTITION BY部分并使用GROUP BY:
SELECT BrandIdSUM(ICount)TotalICount = SUM(ICount) OVER () Percentage = SUM(ICount) OVER ()*1.0 / SUM(ICount) FROM Table WHERE DateId= 20130618GROUP BY BrandID不知道为什么用总数除以每个BrandID如果计数是错误的,并且想占总数的百分比,则将上述位置逆转为:
SELECT BrandIdSUM(ICount)TotalICount = SUM(ICount) OVER () Percentage = SUM(ICount)*1.0 / SUM(ICount) OVER () FROM Table WHERE DateId= 20130618 GROUP BY BrandID
页:
[1]