【问题标题】:query to ignore duplicate/null records查询以忽略重复/空记录
【发布时间】:2016-10-12 23:25:47
【问题描述】:

下面是我们需要加入的 3 个表来获取数据

 TableA         TableB               TableC
    AId          BId     Name      CId       DeclareDate  value
    1            1       abc       1         September      11
    2            2       def       1         October        12
    3            3       xyz       1         November       13
    4            4       pqr       1         December       14
    5            5       ghi       2         September      15
                                   3         October        16
                                   4         August         17
                                   5         October        18
                                   5         December       19

在 TableC 中,我们有相同的基金,CId 1 在所有 4 个月内都在声明,在这种情况下,我只需要 12 月的行,如果基金在 12 月没有声明,那么我需要获取该 C id 的空值. 所以输出表应该如下所示。

输出表

   AId    BId  name     DeclareDate   value
    1      1    abc     December      14
    2      2    def     null          null
    3      3    xyz     null          null
    4      4    pqr     null          null
    5      5    ghi     december      19

所以最后如果 12 月没有声明日期则返回 null,如果有多个声明日期则仅返回 12 月记录。

请提出建议。

【问题讨论】:

  • 请分享您到目前为止所尝试的内容。
  • 你的逻辑不清楚。
  • @TimBiegeleisen 如果您考虑来自 tableC 的 Id 1 和 5 的资金,他们的声明日期是多个月,在这种情况下,我们只需要获得声明日期为 12 月的资金并忽略剩余,如果您考虑其他基金 id 2,3,4 他们没有在 12 月声明,在这种情况下,我们需要返回 null 值而不是返回声明日期
  • 为什么 1 和 5 选择了 12 月?是因为是最近一个月吗?
  • 我试过这个,从 tableA 中选择(表中的列名) A 内连接 tableB B on A.AId = B.BId 内连接 TableC C on A.AId = C.CId where C.declareDate在“01-DEC-16”和“31-DEC-16”之间;上面的查询只返回了两条记录 1 和 5 AId BId name CId DeclareDate value 1 1 abc 1 December 14

标签: sql oracle oracle11g


【解决方案1】:

在表 C 中使用左连接

  select a.*,b.*, c.DeclareDate, c.Value from TableA a
   inner join TableB b on  a.AI =b.BId
  left join 
  (select * from TableC where DeclareDate='December') c
   on b.BId = c.CId

输出

【讨论】:

  • 这仅返回 12 月的行,我们需要将非 12 月的行也作为空值获取
  • 同理,(select * from TableC where DeclareDate='December') 返回两条记录 12 月,然后左连接到 tableA 和 tableB,如果不是 12 月行则返回 null。你是不是已经尝试过了?
  • 如 Id 的输出表所示,如果在多个月内有声明日期,那么我们需要获取 Id 的 12 月记录,如果 Id 在 12 月不存在则我们需要为我返回空值
  • @RMS -- 我对样本数据进行了测试,它确实产生了您想要的结果...注意左连接
  • @RMS -- 如果工作,你能把我的答案改成真正的答案吗? :-D
【解决方案2】:

如果你的意思是说 DeclareDate 是一个实际的日期,而你只想要 12 月的值,也许这样的方法会起作用?

select
  a.AId, b.BId, b.Name,
  coalesce (c.CId, A.AId) as CId,
  c.DeclareDate, c.Value
from
  TableA a
  join TableB b on a.AId = b.BId
  left join TableC c on
    a.AId = c.CId and
    extract (month from c.DeclareDate) = 12

这假设很多 - 特别是如果您在 12 月有多个日期,它们都会出现。此外,它不检查年份......任何旧的 12 月都足够了吗?

如果您的字面意思是文本字符串“December”,那么您可以将左连接中的第二个条件更改为 c.DeclareDate = 'December'。

如果你有一些更广泛的例子,也许它会帮助确定确切的逻辑。

-- 编辑--

这是一些构建和测试的示例代码:

insert into tablea values (1);
insert into tablea values (2);
insert into tablea values (3);
insert into tablea values (4);
insert into tablea values (5);
insert into tableb values (1, 'abc');
insert into tableb values (2, 'def');
insert into tableb values (3, 'xyz');
insert into tableb values (4, 'pqr');
insert into tableb values (5, 'ghi');
insert into tablec values (1, 'September', 11);
insert into tablec values (1, 'October', 12);
insert into tablec values (1, 'November', 13);
insert into tablec values (1, 'December', 14);
insert into tablec values (2, 'September', 15);
insert into tablec values (3, 'October', 16);
insert into tablec values (4, 'August', 17);
insert into tablec values (5, 'October', 18);
insert into tablec values (5, 'December', 19);

以及解决方案:

select
  a.AId, b.BId, b.Name,
  coalesce (c.CId, A.AId) as CId,
  c.DeclareDate, c.Value
from
  TableA a
  join TableB b on a.AId = b.BId
  left join TableC c on
    a.AId = c.CId and
    c.DeclareDate = 'December';

结果:

AID BID NAME  CID   DECLAREDATE VALUE
1   1   abc   1     December    14
2   2   def   2     
3   3   xyz   3     
4   4   pqr   4     
5   5   ghi   5     December    19

【讨论】:

  • 这只会获取 12 月的记录。如 Id 的输出表所示,如果在多个月中有声明日期,那么我们需要获取 Id 的 12 月记录,如果 Id 在 12 月不存在,那么我们需要为其返回 null 值我会
  • 我相信这正是这样做的......“左连接”即使没有匹配项也能保证记录回来。
  • 我添加了一些示例代码来摆弄......这肯定会保留非 12 月的行。
  • 我刚刚测试了,看起来不错,明天我会实施并告诉你,非常感谢
【解决方案3】:

试试这个

select a.AID,b.DeclareDate,b.VAL from TABLEA a left outer join (select * from TABLEB where DeclareDate='December') b on a.AID=b.BID order by a.AID asc

【讨论】:

    猜你喜欢
    • 2017-08-01
    • 2018-04-26
    • 2017-02-21
    • 1970-01-01
    • 1970-01-01
    • 2020-12-02
    • 2011-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多