简体   繁体   English

在 Cognos 中创建左外连接查询

[英]Creating Left Outer Join Query in Cognos

I have the following problem.我有以下问题。 In one table I have a list of Clients with ID, in another table, I have a list of Calls for those Clients.在一个表中,我有一个带有 ID 的客户列表,在另一个表中,我有一个这些客户的呼叫列表。 In the datamodel I have marked relationship 0 to many.在数据模型中,我将关系标记为 0 到许多。 1 client can have 0 or many calls. 1 个客户端可以有 0 个或多个调用。 When I create my query I add Client Id and Client Name from Client Table and then add count from Calls Table.创建查询时,我从客户端表中添加客户端 ID 和客户端名称,然后从呼叫表中添加计数。 In the filter section, I have a filter on Client id and filter on Date Range on Calls Table.在过滤器部分,我有一个关于客户端 ID 的过滤器和一个关于调用表上的日期范围的过滤器。

This way I only get clients that had calls and clients without calls do not appear on in the results.这样我只得到有电话的客户,没有电话的客户不会出现在结果中。 Using SQL I wrote the same query to test results and here is what I found.我使用 SQL 编写了相同的查询来测试结果,这是我发现的。

Using this query I get both clients that have calls and do not have calls.使用这个查询,我得到了有电话和没有电话的两个客户端。 Count(CallId) returns 0 . 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

Using this query I only get counts for clients with calls.使用这个查询,我只能得到有呼叫的客户的计数。 and clients without calls do not appear in results没有电话的客户不会出现在结果中

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

I can figure out a way to simulate this in Cognos.我可以想出一种在 Cognos 中模拟这一点的方法。 I solved this problem by creating additional Cognos queries just for the client and then joining it to count query and then using results to display.我通过为客户端创建额外的 Cognos 查询,然后加入它来计算查询,然后使用结果显示来解决这个问题。 I want to know if there a way to do it in 1 COGNOS query.我想知道是否有办法在 1 个 COGNOS 查询中做到这一点。

Please do not post SQL Queries.请不要发布 SQL 查询。 The first query listed is what I'm trying to do in Cognos Application.列出的第一个查询是我在 Cognos 应用程序中尝试执行的操作。

Here is another way to get the same results as the first query这是获得与第一个查询相同结果的另一种方法

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

如果您只想要有呼叫的客户端,则必须将left outer join替换为inner join

You should do :你应该做 :

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM