如何显示未包含在group by子句中的列?

时间:2013-02-18 11:51:53

标签: sql oracle10g

如何显示未包含在group by子句中的列?

select  user_id,DecryptString(first_name) as first_name, DecryptString(last_name) as last_name, DecryptString(middle_name) as middle_name,count(*)
from mamt
where middle_name<>'9ZnIFajWgaw='
  group by first_name,middle_name,last_name having count(*) >1;

在上面的查询中,user_id不在group by子句中。但是我希望它显示出来。 怎么办呢?

2 个答案:

答案 0 :(得分:2)

你也可以这样做:

select *
from (
select user_id, 
       DecryptString(first_name),
       DecryptString(middle_name),
       DecryptString(last_name), 
       count(*) over (partition by first_name,  middle_name, last_name) cn
from mamt
where middle_name<>'9ZnIFajWgaw=')
where cn > 1;

Here is a sqlfiddle demo

答案 1 :(得分:0)

类似这样的事情

select  user_id, DecryptString(a.first_name),DecryptString(a.last_name), DecryptString(a.middle_name)
from mamt
inner join
(
select first_name, last_name,middle_name,count(*) as c
from mamt
where middle_name<>'9ZnIFajWgaw='
group by first_name,middle_name,last_name having count(*) >1
) a on a.first_name = mamt.first_name AND a.last_name = mamt.last_name AND a.middle_name = mamtmiddle_name