与其他一起获取唯一列值。使用内连接时的列

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.Can 的唯一值 任何人都可以帮忙吗?我正在使用 oracle 数据库

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

[编辑]

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

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在事务table中,那么条件应该移动到子查询。