拼命需要一些幫助,我有三個表。用於員工姓名的卡片表,用於生成的銷售線索的查詢表以及用於作業的工作表。來自多個表的多重計數和計算行的MYSQL查詢
我試圖做一個查詢,涉及返回每個員工的潛在客戶數量和職位數量,並計算每個員工的轉化率。
卡表架構是這樣的: -
cards
+-----------+------------+-------------+
| PK_CardID | LastName | FirstName |
+-----------+------------+-------------+
| 1 | Andrews | John |
| 2 | Smith | Cynthia |
| 3 | Jones | Adam |
+-----------+------------+-------------+
查詢表架構是這樣的: -
+--------------+-----------+------------+
| PK_EnquiryID | FK_CardID | DateAdded |
+--------------+-----------+------------+
| 1 | 1 | 1995-01-21 |
| 2 | 3 | 1995-01-22 |
| 3 | 1 | 1995-01-23 |
| 4 | 2 | 1995-01-21 |
+--------------+-----------+------------+
工作表模式是這樣的: -
+----------+-----------+------------+
| PK_JobID | FK_CardID | DateAdded |
+----------+-----------+------------+
| 1 | 3 | 1995-01-25 |
| 2 | 2 | 1995-01-26 |
| 3 | 3 | 1995-01-24 |
| 4 | 1 | 1995-01-26 |
+----------+-----------+------------+
所以我想要返回如下內容
+-----------+------------+-------------+----------------+-----------+-----------------+
| PK_CardID | LastName | FirstName | countEnquiries | countJobs | ConversionRatio |
+-----------+------------+-------------+----------------+-----------+-----------------+
| 1 | Andrews | John | 2 | 1 | 50% |
| 2 | Smith | Cynthia | 1 | 1 | 100% |
| 3 | Jones | Adam | 1 | 2 | 200% |
+-----------+------------+-------------+----------------+-----------+-----------------+
到目前爲止,我的SQL看起來像這樣
SELECT PK_CardID, LastName, FirstName
(SELECT count(PK_EnquiryID) FROM enquiries WHERE PK_Cardid = FK_CardID) as countEnq,
(SELECT count(PK_JobID) FROM jobs WHERE PK_CardID = FK_CardID) as countJob
FROM enquiries";
然而,這顯然是不正確極,任何幫助,將不勝感激。