我有这些表:公司,SR_Service(保存票证信息)和联系人。
我想在任何给定的月份提取以下内容:公司名称,已打开的门票数除以每家公司的用户数量,月份
e.g。
等
我已经能够通过使用此查询获取此公司的信息(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
但是,我似乎无法以上面示例中列出的格式获取信息。我真的不想在250次左右放一个新ID!
提前致谢 格雷格
答案 0 :(得分:1)
将其设为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)