sql server统计查询

发布于 2024-09-30 04:00:32 字数 1593 浏览 9 评论 0原文

我有一个包含以下列的表,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自由范儿 2024-10-07 04:00:32

您可以使用 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 条款

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]

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:

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]

If not, please add sample data and required output.

Or this?

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]

Can't test this (no SQL on this PC) but based on 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]
最偏执的依靠 2024-10-07 04:00:32

也许是这样的:

;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 也类似

Something like this perhaps:

;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]

And similarly for lang

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文