-1

我有以下问题。在一个表中,我有一个带有 ID 的客户列表,在另一个表中,我有一个针对这些客户的呼叫列表。在数据模型中,我将关系标记为 0 到很多。1 个客户端可以有 0 个或多个调用。当我创建查询时,我从客户端表中添加客户端 ID 和客户端名称,然后从呼叫表中添加计数。在过滤器部分,我有一个客户 ID 过滤器和呼叫表上的日期范围过滤器。

这样,我只会得到有电话的客户,而没有电话的客户不会出现在结果中。我使用 SQL 编写了相同的查询来测试结果,这就是我发现的。

使用此查询,我得到了有呼叫和没有呼叫的两个客户端。Count(CallId)返回0

select ct.clientid
,ct.ClientName
,count(cs.callid)
from client ct
left outer join calls cs
on ct.clientid = cs.clientid
and cs.CallRecievedDateTime > '1/1/2012' 
and cs.CallRecievedDateTime < '1/2/2012'
where ct.clientid in (1,2,5)
group by ct.clientid, ct.ClientName

使用此查询,我只能获得呼叫客户的计数。没有来电的客户不会出现在结果中

select ct.clientid
,ct.ClientName
,count(cs.callid)
from client ct
left outer join calls cs
on ct.clientid = cs.clientid
where ct.clientid in (1,2,5)
and cs.CallRecievedDateTime > '1/1/2012' 
and cs.CallRecievedDateTime < '1/2/2012'
group by ct.clientid, ct.ClientName

我可以想办法在 Cognos 中模拟这种情况。我通过仅为客户端创建额外的 Cognos 查询,然后加入它来计算查询,然后使用结果显示来解决了这个问题。我想知道是否有办法在 1 COGNOS 查询中做到这一点。

请不要发布 SQL 查询。列出的第一个查询是我在 Cognos Application 中尝试执行的操作。

这是获得与第一个查询相同的结果的另一种方法

SELECT ClientId,
ClientName,
counts
FROM (SELECT clientid,
ClientName
FROM Client
WHERE clientid in (1,2,5) ) cd
LEFT OUTER JOIN
(SELECT clientid,
COUNT(*) counts
FROM calls cs
WHERE cs.CallRecievedDateTime > '1/1/2012'
AND cs.CallRecievedDateTime < '1/2/2012'
GROUP BY clientid) b
ON cd.clientid = b.clientid
4

2 回答 2

0

如果你只想要有电话的客户,你必须left outer joininner join

于 2012-01-31T19:28:13.907 回答
0

你应该做 :

select ct.clientid
,ct.ClientName
,count(cs.callid)
from client ct
left outer join calls cs
on ct.clientid = cs.clientid
where ct.clientid in (1,2,5)
and cs.clientid not in (select ct.clientid from client ct inner join calls cs
on ct.clientid = cs.clientid   and cs.CallRecievedDateTime > '1/1/2012' 
and cs.CallRecievedDateTime < '1/2/2012')
and cs.CallRecievedDateTime > '1/1/2012' 
and cs.CallRecievedDateTime < '1/2/2012'
group by ct.clientid, ct.ClientName
于 2012-01-31T20:11:00.220 回答