1

如何显示未包含在 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 子句中。但我希望它显示出来。怎么做到呢?

4

2 回答 2

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;

这是一个 sqlfiddle 演示

于 2013-02-18T14:07:40.393 回答
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
于 2013-02-18T12:13:36.877 回答