SQL查询产生次数和平均值

时间:2018-08-01 14:33:57

标签: sql sql-server average

我有两个查询。一个将得出特定过程类型的记录总数。另一个按物种产生该特定过程的记录数

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

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

/* 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%

2 个答案:

答案 0 :(得分:0)

您可以使用窗口功能:

class Child1
{
    void mustDefine() { ... }
};

class Child2
{
    void mustDefine() { ... }
};

class NotChild
{
};

template<typename T>
void usesMustDefine(T t)
{
    t.mustDefine();
}

int main()
{
    Child1 c1;
    Child2 c2;
    NotChild c3;

    usesMustDefine(c1); // Succeeds
    usesMustDefine(c2); // Succeeds
    // usesMustDefine(c3); // Fails

    c1.mustDefine(); // Succeeds
    c2.mustDefine(); // Succeeds
    // c3.mustDefine(); // Fails

    return 0;
}

答案 1 :(得分:0)

如果公用表表达式不是您的选择(因为它们不受支持,或者您只是不喜欢它们),则可以通过子查询来做到这一点:

/* 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>