SQL使用AVG显示COUNT

时间:2016-08-18 15:32:13

标签: sql sql-server count sql-server-2012 average

我正在尝试合并包含COUNTGROUP BY语句的AVG。我正在使用MSSql。

是否可以在AVG结果旁边显示COUNT

COUNT SQL:

SELECT ActionId, COUNT(ActionId) as ActionCount
FROM AIFMESSAGELOG
WHERE DIRECTION = 1 AND CREATEDDATETIME >= '2016-08-15 23:59:59.000' AND CREATEDDATETIME <= '2016-08-16 23:59:59.000'
Group By ActionId
Order By ActionCount DESC

返回:

ActionId       ActionCount
Insert Action  672
Update Action  184

AVG SQL:

SELECT DATEDIFF(millisecond, b.CREATEDDATETIME, a.CREATEDDATETIME)*1.0 AS AvgTime 
FROM AIFMESSAGELOG a
INNER JOIN AIFMESSAGELOG b
ON a.REQUESTMESSAGEID = b.MESSAGEID
WHERE b.CREATEDDATETIME >= '2016-08-15 23:59:59.000' AND b.CREATEDDATETIME <= '2016-08-16 23:59:59.000' AND a.ACTIONID = 'Insert Action'
ORDER BY A.CREATEDDATETIME DESC

返回:

AvgTime
50

需要的结果:

ActionId       ActionCount  AvgTime
Insert Action  672          50
Update Action  184          25

我的代码:

SELECT a.ActionId, COUNT(a.ActionId) as ActionCount, b.FinalAvg
FROM AIFMESSAGELOG a
INNER JOIN
(
SELECT AVG(AvgTime) AS FinalAvg, ACTIONID
FROM
(
SELECT c.ActionID, DATEDIFF(millisecond, d.CREATEDDATETIME, c.CREATEDDATETIME)*1.0 AS AvgTime 
FROM AIFMESSAGELOG c
INNER JOIN AIFMESSAGELOG d
ON c.REQUESTMESSAGEID = d.MESSAGEID
)MyTable
) b
ON a.ACTIONID = b.ACTIONID
WHERE a.DIRECTION = 1 AND a.CREATEDDATETIME >= '2016-08-15 23:59:59.000' AND a.CREATEDDATETIME <= '2016-08-16 23:59:59.000'
Group By a.ActionId
Order By ActionCount DESC

当我尝试运行最后一个查询时出现以下错误:

  

列'MyTable.ActionID'在选择列表中无效,因为它是   不包含在聚合函数或GROUP BY子句中。

请注意,我必须执行DATEDIFF来获取同一表和列中的2个不同的行。 我的表:

AIFMESSAGELOG 
-------------
ACTIONID
REQUESTMESSAGEID 
MESSAGEID
DIRECTION
CREATEDDATETIME

2 个答案:

答案 0 :(得分:2)

考虑两个聚合派生表的连接,由ACTIONID加入:

SELECT t1.ActionId, t1.ActionCount, t2.AvgTime

FROM    
   (SELECT ActionId, COUNT(ActionId) as ActionCount
    FROM AIFMESSAGELOG
    WHERE DIRECTION = 1 
    AND CREATEDDATETIME >= '2016-08-15 23:59:59.000' 
    AND CREATEDDATETIME <= '2016-08-16 23:59:59.000'
    GROUP By ActionId) As t1

INNER JOIN    
   (SELECT a.ACTIONID, 
           AVG(DATEDIFF(millisecond, b.CREATEDDATETIME, a.CREATEDDATETIME)*1.0) AS AvgTime
    FROM AIFMESSAGELOG a
    INNER JOIN AIFMESSAGELOG b
            ON a.REQUESTMESSAGEID = b.MESSAGEID
    WHERE b.CREATEDDATETIME >= '2016-08-15 23:59:59.000' 
    AND b.CREATEDDATETIME <= '2016-08-16 23:59:59.000'
    GROUP BY a.ACTIONID) AS t2

ON t1.ActionId = t2.ACTIONID
ORDER BY t1.ActionCount DESC

答案 1 :(得分:1)

SELECT
    a.ActionId
    ,SUM(CASE
                    WHEN a.DIRECTION = 1
                       AND a.CREATEDDATETIME >= '2016-08-15 23:59:59.000'
                       AND a.CREATEDDATETIME <= '2016-08-16 23:59:59.000'
                    THEN 1 END) AS ActionCount
    ,AVG(DATEDIFF(millisecond, b.CREATEDDATETIME, a.CREATEDDATETIME)*1.0) AS AvgTime 
FROM
    AIFMESSAGELOG a
    LEFT JOIN AIFMESSAGELOG b
    ON a.REQUESTMESSAGEID = b.MESSAGEID
    AND b.CREATEDDATETIME >= '2016-08-15 23:59:59.000' AND b.CREATEDDATETIME <= '2016-08-16 23:59:59.000' 
    AND a.ACTIONID = 'Insert Action'
GROUP BY
    a.ActionId
ORDER BY A.CREATEDDATETIME DESC

这会让你到那里,但我猜它可以更简化,这取决于你实际想要如何过滤你的date_time文件。

它的基础是对计数使用条件聚合,对平均值使用LEFT JOIN。