sql server统计查询
我有一个包含以下列的表,
source_title, country, language, source_url
我需要生成一个查询,该查询将为我提供以下信息:
country, source_title count, percentage of sources
基本上
language, source_title count, percentage of sources
将国家/地区映射到所有源,并获取此映射的计数和百分比,
而不是行级数据,例如
SELECT [source_id]
,[source_title]
,[source_url]
,[moreover]
,[country]
,[lang]
FROM [NewsDatabase].[dbo].[NewsSourcesMatch]
order by country
例如,如果有 10记录国家/地区是美国,那么
country count(source_title) % source_title
USA 10 10/1000 * 100
对不起,这里是示例数据
source_title source_url 还有国家/地区语言
Hadeland http:// www.hadeland.net Hadeland NORWAY 挪威
Business Wire http://www.businesswire.com Business Wire美国
现在阿德莱德英语 http://www.adelaidenow.com.au 现在阿德莱德英语 澳大利亚英语
MSNBC 本地 < a href="http://www.msnbc.msn.com" rel="nofollow">http://www.msnbc.msn.com MSNBC 本地美国英语
UDN.com http://forum.udn.com UDN.com 台湾中文
CBS3 费城 http://cbs3.com CBS3 费城美国 英语
104.7 Edge Radio http://www .1047edgeradio.com 104.7 Edge Radio 美国英语,
因此有四个来自美国,所以总百分比不应该是 4/7* 100
I have a table with the following columns
source_title, country, language, source_url
I need to generate a query that will give me the following:
country, source_title count, percentage of sources
and
language, source_title count, percentage of sources
basically map the country to all sources and get the count and percentages of this mapping
not the row level data like
SELECT [source_id]
,[source_title]
,[source_url]
,[moreover]
,[country]
,[lang]
FROM [NewsDatabase].[dbo].[NewsSourcesMatch]
order by country
For example if there are 10 records where country is USA then
country count(source_title) % source_title
USA 10 10/1000 * 100
sorry everyone here is sample data
source_title source_url moreover country lang
Hadeland http://www.hadeland.net Hadeland NORWAY Norwegian
Business Wire http://www.businesswire.com Business Wire UNITED STATES English
Adelaide Now http://www.adelaidenow.com.au Adelaide Now AUSTRALIA English
MSNBC Local http://www.msnbc.msn.com MSNBC Local UNITED STATES English
UDN.com http://forum.udn.com UDN.com TAIWAN Chinese
CBS3 Philadelphia http://cbs3.com CBS3 Philadelphia UNITED STATES English
104.7 Edge Radio http://www.1047edgeradio.com 104.7 Edge Radio UNITED STATES English
so there are four from UNITED STATES so shouldnt the total percentage be 4/7* 100
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 OVER 子句通过 COUNT 跨越整个数据集,以给出同一查询中的总行数。然后,您有两个计数(每个国家和所有行)来生成 %
应该类似于:
如果没有,请添加示例数据和所需的输出。
还是这个?
无法对此进行测试(此 PC 上没有 SQL),但基于 MSDN OVER 条款
You can use the OVER clause to span the entire dataset with COUNT to give total number of rows in the same query. Then you have both counts (per country and all rows) to generate the %
Should be something like:
If not, please add sample data and required output.
Or this?
Can't test this (no SQL on this PC) but based on MSDN OVER clause
也许是这样的:
lang
也类似Something like this perhaps:
And similarly for
lang