SQL 报表到 select 每个公司每个用户打开的工单数量

SQL statement to select number of tickets opened per user per company

我有这些表:公司、SR_Service(保存工单信息)和联系人。

我想提取任何给定月份的以下信息:公司名称、打开的票数除以每家公司的用户数量、月份

例如

等等

我已经能够通过使用此查询(company_recID 用作公司标识符)获得一家公司的此信息:

SELECT 
co.Company_Name,
(CAST((COUNT(SR_Service_RecID)) AS FLOAT)     /    
  (SELECT count(Contact_RecID)
   FROM Contact 
   WHERE Company_RecID = 2215)) AS 'Tickets Per User'
FROM SR_Service s
LEFT JOIN company as co 
  on s.company_recid = co.company_recid
LEFT JOIN Contact as con 
  on co.Company_RecID = con.Contact_RecID
WHERE s.date_entered BETWEEN '2014-12-26 13:36:00' and '2015-01-02 13:36:00'
AND  co.Company_RecID = 2215
GROUP BY co.Company_Name

但是,我似乎无法获得上述示例中所列格式的信息。我真的不想把新ID放250次左右!

提前致谢 格雷格

设为correlated subquery

SELECT co.Company_Name,
       ( Cast(( Count(SR_Service_RecID) ) AS FLOAT) / (SELECT Count(Contact_RecID)
                                                       FROM   Contact cc
                                                       WHERE  cc.Company_RecID = co.Company_RecID) ) AS 'Tickets Per User',
       Datename(mm, date_entered) [month]
FROM   SR_Service s
       LEFT JOIN company AS co
              ON s.company_recid = co.company_recid
       LEFT JOIN Contact AS con
              ON co.Company_RecID = con.Contact_RecID
WHERE  s.date_entered BETWEEN '2014-12-26 13:36:00' AND '2015-01-02 13:36:00'
GROUP  BY co.Company_Name,
          Datename(mm, date_entered)