SQL:MAX(id)in子句子查询与group by

时间:2015-05-15 12:03:36

标签: sql oracle group-by subquery

我在查询中有一个查询和一个子查询,它应该给出一个组中不同行的日期字符串(group by)。

这有效:

enter image description here  现在我想在报告中获取此字符串,其他行具有相同的max(id),但因为我不能在内部的where子句中使用max(inner.id),如min(outer.id)查询我不知道怎么做,有人提出建议吗?

我的代码(简单版):

select  
(select LISTAGG(TO_TIMESTAMP('1970/01/01 00:00:00 ', 'yyyy/mm/dd HH24:MI:SS')  + 
TIMESTAMP/86400, ', ') WITHIN GROUP (ORDER BY id) AS timestamps
from tbl inner
where 

max(inner.id) like max(outer.id)

and

mrdin.timestamp between 
time and time

group by customer having count (2) > 1) as alias

from tbl outer

where timestamp between 
time
and 
time

group by  

customer;

enter image description here

我希望使用两者的max(id)来比较第二张图片的日期列中第一张图片的字符串(时间戳)。但我无法做到这一点,因为我无法在内部选择的where clausule中使用max()函数。

2 个答案:

答案 0 :(得分:0)

我真的不明白你想做什么,但这对你有用吗?

select (select LISTAGG(TO_TIMESTAMP('1970/01/01 00:00:00 ', 'yyyy/mm/dd HH24:MI:SS')  + TIMESTAMP/86400, ', ') WITHIN GROUP (ORDER BY id) AS timestamps
        from   tbl inner
        and    inner.timestamp between time and time
        group by customer 
        having count (2) > 1
        and max(inner.id) = outer_id
       ) as alias
from 
( select max(id) as outer_id
  from   tbl outer
  where timestamp between time and time
  group by customer
);

答案 1 :(得分:0)

您可以将分析函数与listagg

一起使用

喜欢这个

 select  owner, table_name, listagg(column_name,',') 
 within group( order by table_name) over (partition by table_name,owner) as all_columns
from all_tab_columns