在 Cognos 中创建左外连接查询

发布于 2024-12-31 22:58:29 字数 1598 浏览 0 评论 0原文

我有以下问题。在一个表中,我有一个带有 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 应用程序中尝试执行的操作。

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

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

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. In the datamodel I have marked relationship 0 to many. 1 client can have 0 or many calls. When I create my query I add Client Id and Client Name from Client Table and then add count from Calls Table. In the filter section, I have a filter on Client id and filter on Date Range on Calls Table.

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.

Using this query I get both clients that have calls and do not have calls. Count(CallId) returns 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. 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. I want to know if there a way to do it in 1 COGNOS query.

Please do not post SQL Queries. The first query listed is what I'm trying to do in Cognos Application.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘和柠 2025-01-07 22:58:29

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

If you want only clients that had calls , You have to replace the left outer join with inner join

[浮城] 2025-01-07 22:58:29

你应该这样做:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文