久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

SQL - 總行數(shù)的百分比

SQL - percentage of total rows(SQL - 總行數(shù)的百分比)
本文介紹了SQL - 總行數(shù)的百分比的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我有這個查詢:

SELECT 
Count(*) as Cnt, 
Category
FROM [MyDb].[dbo].[MyTable]
group by Category
order by Cnt

它為我提供了每個 Category 中的行數(shù).現(xiàn)在我想添加第三列,它會給我 Cnt/(此表中的總行數(shù)).

It gives me count of rows in each Category. Now I would like to add a third column that would give me Cnt / (total rows in this table).

我該怎么做?

推薦答案

你可以用子查詢來做到:

you could do it with a subquery:

SELECT Count(*) as Cnt, Category, 
  (Cast(Count(*) as real) / cast((SELECT Count(*) FROM [MyDb].[dbo].[MyTable]) as  real)) AS [Percentage]
FROM [MyDb].[dbo].[MyTable]
group by Category
order by Cnt

或使用變量:

declare @total real;
select @total = count(*) from [MyDb].[dbo].[MyTable];

SELECT Count(*) as Cnt, Category, (Cast(Count(*) as real) / @total) AS [Percentage]
FROM [MyDb].[dbo].[MyTable]
group by Category
order by Cnt

我在兩個示例中都將 count(*) 轉(zhuǎn)換為實數(shù)以避免整數(shù)除法類型問題.

I've cast count(*) as real in both examples to avoid integer-division type issues.

希望這有幫助約翰

這篇關(guān)于SQL - 總行數(shù)的百分比的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉(zhuǎn)換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創(chuàng)建視圖?)
Create calculated value based on calculated value inside previous row(根據(jù)前一行內(nèi)的計算值創(chuàng)建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發(fā)技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉(zhuǎn)換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 在线看av网址 | 狠狠干2020 | www操操 | 精品久久av | 久久久久久黄 | 亚洲一区二区三区免费视频 | 国产精品一区二区福利视频 | 国产99免费 | 久久久久久国产精品免费免费 | 亚洲成人999| 欧美精品久久久久久久久久 | 久久久国产一区二区三区 | 精品一区二区三区免费毛片 | 夜夜精品浪潮av一区二区三区 | 亚洲国产aⅴ成人精品无吗 国产精品永久在线观看 | 中文字幕人成人 | 精品国产乱码久久久久久牛牛 | 国产成人精品免费视频 | a级在线免费 | 久久久123 | 中文av电影 | av日韩一区 | 国产精品免费一区二区三区 | 亚洲一区视频在线 | 精品久久久999 | 91视视频在线观看入口直接观看 | 一级毛片在线视频 | 国产在线97 | 欧美一区免费在线观看 | 在线观看国产 | 偷拍第一页 | 99re热这里只有精品视频 | 国际精品久久 | 国产激情一区二区三区 | 久久久久久久久毛片 | 91成人| 成人在线视频免费播放 | 色综合区 | 亚洲福利一区 | 天天干天天玩天天操 | 国产精品不卡视频 |