sql server stats查询

时间:2010-11-03 17:51:39

标签: sql-server-2008 aggregate

我有一个包含以下列的表

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是USA,那么

country    count(source_title)   % source_title
USA            10                    10/1000 * 100
抱歉,这里的每个人都是样本数据

source_title source_url此外还有国家/地区

Hadeland http://www.hadeland.net Hadeland挪威挪威

美国商业资讯http://www.businesswire.com美国商业资讯美国英语

阿德莱德现在http://www.adelaidenow.com.au阿德莱德现在澳大利亚英语

MSNBC Local http://www.msnbc.msn.com MSNBC Local美国英语

UDN.com http://forum.udn.com UDN.com TAIWAN Chinese

CBS3费城http://cbs3.com CBS3费城美国英语

104.7 Edge Radio http://www.1047edgeradio.com 104.7 Edge Radio美国英语

所以有四个来自美国,因此总百分比不应该是4/7 * 100

2 个答案:

答案 0 :(得分:1)

或许这样的事情:

;WITH T AS
(
SELECT  [Country]
    ,   Totals = COUNT(*)
FROM    [dbo].[NewsSourcesMatch]
GROUP BY [Country]
)
SELECT  [Country]
    ,   [source_title] 
    ,   [source_title_count] =  COUNT([source_title])
    ,   [source_title_pct]   =  COUNT([source_title])/t.Totals

FROM [dbo].[NewsSourcesMatch] A
    INNER JOIN
    T t
    ON A.country = t.Country

GROUP   BY A.[Country], [source_title]

同样适用于lang

答案 1 :(得分:1)

您可以使用OVER子句使用COUNT跨越整个数据集,以提供同一查询中的总行数。然后你有两个计数(每个国家和所有行)来生成%

应该是这样的:

SELECT  [Country]
    ,   [source_title_count] =  COUNT(*)
    ,   [source_total_count]  = COUNT(*) OVER ()
    ,   [source_percent]  = 100.0 * COUNT(*) / COUNT(*) OVER () 
FROM [dbo].[NewsSourcesMatch]
GROUP   BY [Country]

SELECT  [lang]
    ,   [source_title_count] =  COUNT(*)
    ,   [source_total_count]  = COUNT(*) OVER ()
    ,   [source_percent]  = 100.0 * COUNT(*) / COUNT(*) OVER () 
FROM [dbo].[NewsSourcesMatch]
GROUP   BY [lang]

如果没有,请添加样本数据和所需的输出。

还是这个?

SELECT  [Country]
    ,   COUNT(DISTINCT [source_title)) AS source_title_count
    ,   COUNT(*) source_country_count
    ,   100.0 * COUNT(*) / COUNT(DISTINCT [source_title)) source_country_count
FROM [dbo].[NewsSourcesMatch]
GROUP  BY [Country]

无法测试(此PC上没有SQL),但基于MSDN OVER clause

SELECT  [Country]
    ,   [source_title_count] =  COUNT(*)
     --attempt 1
    ,   [source_total_count]  = COUNT(*) OVER (Country)
    ,   [source_percent]  = 100.0 * COUNT(*) / COUNT(*) OVER (Country) 
     --attempt 2
    ,   [source_total_count]  = COUNT(*) OVER (PARTITION BY Country)
    ,   [source_percent]  = 100.0 * COUNT(*) / COUNT(*) OVER (PARTITION BY Country) 
FROM [dbo].[NewsSourcesMatch]
GROUP   BY [Country]