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

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

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

問題描述

我無論如何都不是 SQL 專家,并且很難從查詢中獲取我需要的數據.我正在處理一個表,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).

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

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

我需要繼續查詢其他兩個狀態 ID,2 和 3,理想情況下希望以選擇三列作為百分比結束,每個狀態一列: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))

...將給出兩位小數.如果您不需要任何小數位,請將結果轉換為 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

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

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

相關文檔推薦

Modify Existing decimal places info(修改現有小數位信息)
The correlation name #39;CONVERT#39; is specified multiple times(多次指定相關名稱“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(將迭代查詢更改為基于關系集的查詢)
concatenate a zero onto sql server select value shows 4 digits still and not 5(將零連接到 sql server 選擇值仍然顯示 4 位而不是 5)
主站蜘蛛池模板: 日日骚视频 | 国产区视频在线观看 | 久久久国产一区二区三区四区小说 | 国产亚洲欧美在线 | 奇米在线 | 日韩免费视频 | 国产精品入口 | 久久69精品久久久久久久电影好 | 欧美精品久久久 | 免费视频一区二区三区在线观看 | 久久国产精品无码网站 | 成年女人免费v片 | 亚洲精品一区二区网址 | 亚洲免费视频一区 | 91麻豆精品一区二区三区 | 做a的各种视频 | 干狠狠 | 欧美成人一区二免费视频软件 | 色综合欧美 | 亚洲综合日韩精品欧美综合区 | 亚洲午夜精品一区二区三区他趣 | 日韩在线第一 | 日韩在线视频一区二区三区 | 欧美一级免费片 | 亚洲综合色视频在线观看 | 午夜精品久久久久久久星辰影院 | 成人欧美一区二区三区视频xxx | 亚洲欧美一区二区三区视频 | 天天草天天操 | 久产久精国产品 | www.成人免费视频 | 免费视频二区 | 国产黄色网址在线观看 | 久久6| 久久人人网 | 国产一级电影在线观看 | 久久国产精品视频 | 五月香婷婷 | 国产a级毛毛片 | 欧美在线一区二区三区 | 精品国产色 |