我想计算系统与多少客户进行了通信。在该期间,在议题中,在小组中,在方案中,在渠道中以百分比形式计算。
公式很简单:
F = X / Y
位置:
X - number of clients communicated by system.
和
Y - number of clients in analizing sample.
我有数据库表INTERACTIONS。
表示例:
datetime | Issue | Group | ProgramID | Channel | CustomerID | Outcome ---------------+-------+-------+-----------+---------+------------+-------- 20181126000001 | Sale | Loans | P-1 | SMS | 5 | Accept 20181126000005 | Sale | Loans | P-1 | PUSH | 7 | Ignore 20181126000010 | Sale | Loans | P-2 | SMS | 8 | Ignore 20181126000015 | Sale | Loans | P-3 | PUSH | 10 | Accept
所以X的选择是:
SELECT
ISSUE, GROUP, PROGRAMID, CHANNEL,
COUNT(DISTINCT CUSTOMERID) AS Customers
FROM INTERACTIONS
GROUP BY ISSUE, GROUP, PROGRAMID, CHANNEL
ORDER BY 1 ASC, 2 ASC, 3 ASC, 4 ASC;
Y的选择是:
SELECT COUNT(DISTINCT CUSTOMERID) AS Customers
FROM INTERACTIONS;
如何结合这两个SQL选择并获得F = X / Y的报告?
我想得到类似这样的结果
答案 0 :(得分:0)
SELECT
ISSUE, GROUP, PROGRAMID, CHANNEL,
COUNT(DISTINCT CUSTOMERID)/
(SELECT COUNT(DISTINCT CUSTOMERID) FROM
INTERACTIONS) AS Customers
FROM INTERACTIONS
GROUP BY ISSUE, GROUP, PROGRAMID, CHANNEL
ORDER BY 1 ASC, 2 ASC, 3 ASC, 4 ASC
只需将Y查询放入X查询