【问题标题】:SQL query yielding count and averageSQL 查询产生计数和平均值
【发布时间】:2018-08-01 14:33:57
【问题描述】:

我有两个问题。一个产生特定过程类型的记录总数。另一个按物种产生该特定程序的记录数

我试图弄清楚如何获取并获得每种特定程序类型的计数和平均数。

按过程类型查询产生总数:

/* The following query will show the total number of studies by imaging area. */
SELECT 
    PlacerFld2 AS "Type of Procedure", Count(*) AS "Count of Procedures"
FROM 
    [order] o
WHERE 
    lastmodifieddate BETWEEN '2017-01-01 00:00:00' AND '2017-12-31 23:59:59'
    AND SiteBridgeID = '1' /* Medical Imaging */
    AND OrderStatusID <> '4'
    AND placerfld2 IN ('CARD', 'CARM', 'CRFL', 'CT', 'I131', 'LACR', 'LAUS', 'NUCMED','OUTFM','PCCR','PETCT','PETR','PHMR','SACR','SAES','SAMR','SAUS','VRC','VRUS')
GROUP BY 
    PlacerFld2
ORDER BY 
    PlacerFld2 ASC

示例输出:

CT    2056
SAMR  1800
SACR  3600

查询产生的程序总数(按物种)

/* The following query will break down the number of studies by species */
SELECT PlacerFld2 AS "Type of Procedure", City AS Species, Count(*) AS "Count of Procedures"
FROM [order] o

LEFT JOIN Visit v
ON o.VisitID = v.VisitID

LEFT JOIN PatientInfo pif
ON v.PatientID = pif.PatientID

LEFT JOIN Patient p
ON pif.PatientID = p.PatientID

LEFT JOIN PersonalInfo perinfo
ON p.PersonalInfoID = perinfo.PersonalInfoID

WHERE o.lastmodifieddate between '2017-01-01 00:00:00' AND '2017-12-31 23:59:59'
AND SiteBridgeID = '1' /* Medical Imaging */
AND OrderStatusID <> '4'
AND placerfld2 IN ('CARD','CARM','CRFL','CT','I131','LACR','LAUS','NUCMED','OUTFM','PCCR','PETCT','PETR','PHMR','SACR','SAES','SAMR','SAUS','VRC','VRUS')

GROUP BY Placerfld2, CITY
ORDER BY placerfld2 ASC, CITY ASC

示例表输出:

CT    CANINE   1500
CT    FELINE   556
SAMR  CANINE   1000
SAMR  FELINE   600
SAMR  EQUINE   200

期望的结果:

CT    CANINE   1500   72.9%
CT    FELINE   556    27.1%
SAMR  CANINE   1000   55.5%
SAMR  FELINE   600    33.3%
SAMR  EQUINE   200    11.1%

【问题讨论】:

  • 用您正在使用的数据库标记您的问题。

标签: sql sql-server average


【解决方案1】:

你可以使用窗口函数:

with cte as (<your query here with no order by>)
select col1, col2, col3,
       col3 * 100.0 / sum(col3) over (partition by col1) as percentage
from cte;

【讨论】:

    【解决方案2】:

    如果您不能选择公用表表达式(因为它们不受支持,或者您只是不喜欢它们),您可以通过子查询来实现:

    /* The following query will break down the number of studies by species */
    SELECT PlacerFld2 AS "Type of Procedure", City AS Species, Count(*) AS "Count of Procedures", COUNT(*) / x.[Count of Procedures] AS "Percentage"
    FROM [order] o
    
    LEFT JOIN Visit v
    ON o.VisitID = v.VisitID
    
    LEFT JOIN PatientInfo pif
    ON v.PatientID = pif.PatientID
    
    LEFT JOIN Patient p
    ON pif.PatientID = p.PatientID
    
    LEFT JOIN PersonalInfo perinfo
    ON p.PersonalInfoID = perinfo.PersonalInfoID
    
    LEFT JOIN (
        /* The following query will show the total number of studies by imaging area. */
        SELECT 
            PlacerFld2 AS "Type of Procedure", Count(*) AS "Count of Procedures"
        FROM 
            [order] o
        WHERE 
            lastmodifieddate BETWEEN '2017-01-01 00:00:00' AND '2017-12-31 23:59:59'
            AND SiteBridgeID = '1' /* Medical Imaging */
            AND OrderStatusID <> '4'
            AND placerfld2 IN ('CARD', 'CARM', 'CRFL', 'CT', 'I131', 'LACR', 'LAUS', 'NUCMED','OUTFM','PCCR','PETCT','PETR','PHMR','SACR','SAES','SAMR','SAUS','VRC','VRUS')
        GROUP BY 
            PlacerFld2) x ON x.[Type of Procedure] = PlacerFld2
    --ORDER BY 
    --    PlacerFld2 ASC
    
    WHERE o.lastmodifieddate between '2017-01-01 00:00:00' AND '2017-12-31 23:59:59'
    AND SiteBridgeID = '1' /* Medical Imaging */
    AND OrderStatusID <> '4'
    AND placerfld2 IN ('CARD','CARM','CRFL','CT','I131','LACR','LAUS','NUCMED','OUTFM','PCCR','PETCT','PETR','PHMR','SACR','SAES','SAMR','SAUS','VRC','VRUS')
    
    GROUP BY Placerfld2, CITY, x.[Count of Procedures]
    ORDER BY placerfld2 ASC, CITY ASC
    

    这里的基本模式是:

    SELECT... <your detail query>
    FROM
        <your original detail tables>
        LEFT JOIN (<your summary query>) x ON x.<a field> = <original field>
    WHERE.... <the rest of your detail query>
    GROUP BY... <remember to add the new summary total to the GROUP BY clause>
    

    【讨论】:

    • 我将不得不玩这个。结果返回字段中的百分比为 0。
    • 尝试将x.[Count of Procedures] 更改为CONVERT(NUMERIC(19,2), x.[Count of Procedures])。我认为这只是一个整数除法问题。如果您将其中一个值转换为小数,那么它将返回一个小数结果。目前这两个值都是整数,所以它给出了一个整数结果。您的实际百分比可能是 0.47,但这将返回为 0。另一个技巧是将结果乘以 100.0,将其转换为百分比,并将结果设为小数。
    • 是的!这正是问题所在。非常感谢您对此的帮助。我在 SQL 方面不是很先进,所以非常感谢你!!!!!!
    猜你喜欢
    • 2012-03-29
    • 1970-01-01
    • 2021-05-26
    • 1970-01-01
    • 1970-01-01
    • 2013-11-02
    • 1970-01-01
    • 1970-01-01
    • 2017-04-19
    相关资源
    最近更新 更多