我想要做的是对表的外连接,我在其中根据匹配常量从连接表中排除记录,但是保留主表中的记录.例如:
SELECT a.id, a.other, b.baz
FROM a
LEFT OUTER JOIN b
ON a.id = b.id
AND b.bar = 'foo'
Run Code Online (Sandbox Code Playgroud)
预期成绩:
id other baz
-- ---------- -------
1 Has foo Include
2 Has none (null)
3 Has foobar (null)
将它置于过滤条件下,我无法得到相同的结果.如果我使用以下内容:
SELECT a.id, a.other, b.baz
FROM a
LEFT OUTER JOIN b
ON a.id = b.id
WHERE (b.bar IS NULL OR b.bar = 'foo')
Run Code Online (Sandbox Code Playgroud)
我得到这些不正确的结果:
id other baz
-- -------- -------
1 Has foo Include
2 Has none (null)
如果它排除了恰好与B记录匹配的A记录,其中bar ='foobar'.我不希望这样,我希望A出现,但在这种情况下B为空.
表B将有多个需要排除的记录,所以我不认为我可以在Crystal端过滤这个,而不会进行大量的处理以避免来自表A的重复记录的问题.
我不能使用SQL命令对象,因为我们运行报告的第三方应用程序似乎阻塞了SQL命令对象.
我不能使用视图,因为我们的支持合同不允许数据库修改,我们的供应商考虑添加视图数据库修改.
我正在使用Crystal Reports XI,特别是版本11.0.0.895.如果它有所不同,我使用SQL-92 ODBC驱动程序运行Progress 9.1E04数据库.
可以使用以下内容创建示例中使用的示例表和数据:
CREATE TABLE a (id INTEGER, other VARCHAR(32));
CREATE TABLE b (id INTEGER, bar VARCHAR(32), baz VARCHAR(32));
insert into A (id, other) values ('1', 'Has foo');
insert into A (id, other) values ('2', 'Has none');
insert into A (id, other) values ('3', 'Has foobar');
insert into B (id, bar, baz) values ('1', 'foo', 'Include');
insert into B (id, bar, baz) values ('1', 'foobar', 'Exclude');
insert into B (id, bar, baz) values ('1', 'another', 'Exclude');
insert into B (id, bar, baz) values ('1', 'More', 'Exclude');
insert into B (id, bar, baz) values ('3', 'foobar', 'Exclude');
Run Code Online (Sandbox Code Playgroud)
不确定是否可以在 Crystal 中执行此操作,但加入 Select 怎么样?
SELECT a.id, x.baz
FROM a
LEFT OUTER JOIN
(SELECT id, baz FROM b WHERE bar = 'foo') As x ON a.id = x.id
Run Code Online (Sandbox Code Playgroud)