繁体   English   中英

与其他一起获得唯一列值。 使用内部联接时的列

[英]Get Unique column value along with other. Columns when using inner join

我有以下查询,并且我需要列的唯一值

Select unique(t.id), log.* 
from tableA log 
inner join tableT t on t.id1=log.id1 
where log.time>=(somedate) 
and log.time<(somedate) 
and ref-id=20 
and ref-id=30 
and t.id not in (select unique t.id 
                 from   tableA log 
                 inner join tableT t on t.id1=log.id1 
                 where log.time>=(somedate) 
                 and log.time<(somedate) 
                 and ref-id=20 
                 and ref-id=30);

我没有获得t.id的唯一值。有人可以帮忙吗? 我正在使用oracle数据库

从SELECT中删除LOG。*; UNIQUE(以及DISTINCT)将应用于您选择的所有列,而不只是应用于放在方括号中的列。

[编辑]

Scott的EMP表包含在不同部门工作的员工。 不同部门的清单是:

SQL> select distinct deptno from emp;

    DEPTNO
----------
        30
        20
        10

SQL>

如果包括其他列,例如JOB,则列表将更改为

SQL> select distinct deptno, job from emp order by deptno, job;

    DEPTNO JOB
---------- ---------
        10 CLERK
        10 MANAGER
        10 PRESIDENT
        20 ANALYST
        20 CLERK
        20 MANAGER
        30 CLERK
        30 MANAGER
        30 SALESMAN

9 rows selected.

SQL>

现在每个DEPTNO 每个JOB都不同。 您无法在这样的查询中仅获得不同的DEPTNO。

[编辑2:汇总]

如果“ distinct”表示不同的DEPTNO和该DEPTNO的第一份工作 ,那么您可能需要这样做

SQL> select deptno, min(job) first_job from emp
  2  group by deptno
  3  order by deptno;

    DEPTNO FIRST_JOB
---------- ---------
        10 CLERK
        20 ANALYST
        30 CLERK

SQL>

[编辑#3,基于您的查询的示例]

Select t.id, 
       min(log.id) log_id, 
       min(log.time) log_time,
       <the rest of LOG table columns goes here, all of them with MIN>
from tableA log 
inner join table ...
<the rest of your query goes here>
group by t.id;

这样的事情怎么样?

Select t.id, log.* 
from tableA log inner join
     (select t.*, row_number() over (partition by id1 order by id) as seqnum
      from tableT t
     ) t
     on t.id1 = log.id1 
where log.time >= (somedate) and
      log.time < (somedate) and
      t.seqnum = 1
      ref_id = 20 and ref_id = 30 -- this is ALWAYS false, but I'm ignoring that

笔记:

  • 我不知道子查询应该做什么。
  • ref_id上的条件将始终为FALSE,因此逻辑几乎可以肯定不是您想要的。
  • 如果ref_id在事务表中,则应将条件移至子查询。

暂无
暂无

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

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