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

SQL:使用聚合函數(shù)獲取百分比的查詢有問題

SQL: Having trouble with query that gets percentages using aggregate functions(SQL:使用聚合函數(shù)獲取百分比的查詢有問題)
本文介紹了SQL:使用聚合函數(shù)獲取百分比的查詢有問題的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我無論如何都不是 SQL 專家,并且很難從查詢中獲取我需要的數(shù)據(jù).我正在處理一個表,Journal_Entry,它有很多列.一列是 Status_ID,它是 Status 表的外鍵,具有三個值Green"、Yellow"和Red".此外,日志條目是針對特定用戶 (User_ID) 記錄的.

I'm not an expert in SQL by any means, and am having a hard time getting the data I need from a query. I'm working with a single table, Journal_Entry, that has a number of columns. One column is Status_ID, which is a foreign key to a Status table with three values "Green", "Yellow", and "Red". Also, a journal entry is logged against a particular User (User_ID).

我正在嘗試獲取為每個狀態(tài)記錄的日記條目數(shù),作為特定用戶記錄的日記條目總數(shù)的百分比.到目前為止,我已經(jīng)獲得了以下狀態(tài)為 1 的狀態(tài),即綠色(我知道這不起作用):

I'm trying to get the number of journal entries logged for each Status, as a percentage of the total number of journal entries logged by a particular user. So far I've got the following for a Status of 1, which is green (and I know this doesn't work):

SELECT  CAST((SELECT COUNT(Journal_Entry_ID) 
        FROM Journal_Entry 
        WHERE Status_ID = 1 AND User_ID = 3 / 
        SELECT COUNT(Journal_Entry_ID) 
        FROM Journal_Entry AND User_ID = 3)) AS FLOAT * 100

我需要繼續(xù)查詢其他兩個狀態(tài) ID,2 和 3,理想情況下希望以選擇三列作為百分比結(jié)束,每個狀態(tài)一列:Green_Percent"、Yellow_Percent"和"Red_Percent".

I need to continue the query for the other two status ID's, 2 and 3, and ideally would like to end with the selection of three columns as percentages, one for each Status: "Green_Percent", "Yellow_Percent", and "Red_Percent".

這可能是我問過的最雜亂無章的問題,因此對于任何不夠明確的問題,我深表歉意.我很樂意在必要時進行澄清.另外,我使用的是 SQL Server 2005.

This is probably the most disjointed question I've ever asked, so I apologize for any lack of clarity. I'll be happy to clarify as necessary. Also, I'm using SQL Server 2005.

非常感謝.

推薦答案

使用:

  SELECT je.statusid,
         COUNT(*) AS num,
         (COUNT(*) / (SELECT COUNT(*)+.0
                       FROM JOURNAL_ENTRY) ) * 100
    FROM JOURNAL_ENTRY je
GROUP BY je.statusid

然后是格式化您想要的精度的問題:

Then it's a matter of formatting the precision you want:

CAST(((COUNT(*) / (SELECT COUNT(*)+.0 FROM BCCAMPUS.dbo.COURSES_RFIP)) * 100)
     AS DECIMAL(4,2))

...將給出兩位小數(shù).如果您不需要任何小數(shù)位,請將結(jié)果轉(zhuǎn)換為 INT.

...will give two decimal places. Cast the result to INT if you don't want any decimal places.

您可以使用 CTE 來減少重復:

You could use a CTE to minimize the duplication:

WITH cte AS (
   SELECT je.*
     FROM JOURNAL_ENTRY je
    WHERE je.user_id = 3)
  SELECT c.statusid,
         COUNT(*) AS num,
         (COUNT(*) / (SELECT COUNT(*)+.0
                       FROM cte) ) * 100
    FROM cte c
GROUP BY c.statusid

這篇關(guān)于SQL:使用聚合函數(shù)獲取百分比的查詢有問題的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Modify Existing decimal places info(修改現(xiàn)有小數(shù)位信息)
The correlation name #39;CONVERT#39; is specified multiple times(多次指定相關(guān)名稱“CONVERT)
T-SQL left join not returning null columns(T-SQL 左連接不返回空列)
remove duplicates from comma or pipeline operator string(從逗號或管道運算符字符串中刪除重復項)
Change an iterative query to a relational set-based query(將迭代查詢更改為基于關(guān)系集的查詢)
concatenate a zero onto sql server select value shows 4 digits still and not 5(將零連接到 sql server 選擇值仍然顯示 4 位而不是 5)
主站蜘蛛池模板: 国产乱码精品一区二区三区忘忧草 | 91久久综合亚洲鲁鲁五月天 | 日本成人在线观看网站 | 一区二区三区久久 | 久久精品视频免费观看 | 一区二区三区在线看 | 日韩精品久久 | 黄色一级电影在线观看 | 精品久久久久久久久久久下田 | 超碰电影 | 国产色黄 | 天天射网站 | 久久99精品视频 | 久久成人18免费网站 | 国产精品一区二区av | 日本特黄a级高清免费大片 特黄色一级毛片 | 久久久久久中文字幕 | 亚洲精品888 | 国产精品美女久久久免费 | www.嫩草| 亚洲欧美一区二区在线观看 | 毛片网在线观看 | 97久久久久久 | 精品视频在线免费观看 | 国产精品久久久久一区二区三区 | 久久精品免费看 | 国产一区二区三区欧美 | 综合久久av | 国产精品免费一区二区三区四区 | 欧美久久视频 | 天天拍天天草 | 成人黄色电影在线观看 | 99精品免费在线观看 | 欧美精品一区二区免费视频 | 五月天国产 | 在线免费激情视频 | 国产一区二区三区久久 | 97精品超碰一区二区三区 | www国产精品 | 中文字幕精品一区二区三区精品 | 日本福利一区 |