2015-11-11 72 views
1

我想获得表中最大重复的整数我尝试了很多方法,但无法使其工作。我在寻找的结果是:计数重复的数据

"james";"108"

由于这108当我Concat的两个领域LOCA + locb重复了两次,但别人没有我尝试用以下样本表结构和查询sqlfiddle链接我想... sqlfiddle link

查询我想的是:

select * from (
select name,CONCAT(loca,locb),loca,locb 
, row_number() over (partition by CONCAT(loca,locb) order by CONCAT(loca,locb)) as att 

from Table1 
) tt 
where att=1 

please click这里,所以你可以看到完整的示例表和查询我试过了。

Edite:添加完整的表结构及数据:

CREATE TABLE Table1 
    (name varchar(50),loca int,locb int) 
; 

insert into Table1 values ('james',100,2); 
insert into Table1 values ('james',100,3); 
insert into Table1 values ('james',10,8); 
insert into Table1 values ('james',10,8); 
insert into Table1 values ('james',10,7); 
insert into Table1 values ('james',10,6); 
insert into Table1 values ('james',0,7); 
insert into Table1 values ('james',10,0); 
insert into Table1 values ('james',10); 
insert into Table1 values ('james',10); 

什么我要找的是作为价值被重复两次在整个数据获取(詹姆斯,108),还有的repetion (james,10)但是loca的值为null,所以Zero值和Null值只能被视为在两个(loca,locb)中都有值。

+1

排序由你的分区上并没有真正意义相同的值。请将示例数据添加到您的问题 - SQLFiddle不是真的可靠,目前不适合我。 –

+0

你好,谢谢我更新了样本数据和数据.. – hi4ppl

+0

'CONCAT(loca,locb)'没有意义。 'concat()'是连接字符串(文本)的值,而不是数字 –

回答

0
WITH concat AS (
    -- get concat values 
    SELECT name,concat(loca,locb) as merged 
    FROM table1 t1 
    WHERE t1.locb NOTNULL 
    AND t1.loca NOTNULL 
), concat_count AS (
    -- calculate count for concat values 
    SELECT name,merged,count(*) OVER (PARTITION BY name,merged) as merged_count 
    FROM concat 
) 
SELECT cc.name,cc.merged 
FROM concat_count cc 
WHERE cc.merged_count = (SELECT max(merged_count) FROM concat_count) 
GROUP BY cc.name,cc.merged; 
+0

嗨,谢谢你的回答和时间,但是当108的计数变化像现在一样重复2次,这可以看作是数据集非常小,样本但我需要在大数据集中运行它,我不知道cc.merged_count = 2 ...无论如何,这可以是动态的,所以它会自己挑选最大值......这些数据仅仅是样本来指出我需要的。 .. – hi4ppl

+0

@ hi4ppl:你需要找到最出现的组合?请具体说明问题。 –

+0

嗨Dmity,是的,正是最重复的组合...我只给了108作为例子抱歉混淆。 – hi4ppl

0

SQL Fiddle

select distinct on (name) * 
from (
    select name, loca, locb, count(*) as total 
    from Table1 
    where loca is not null and locb is not null 
    group by 1,2,3 
) s 
order by name, total desc 
+0

嗨谢谢,但这类记录并没有选择顶部的特定记录... – hi4ppl

+0

@ hi4ppl你只想要最上面的那个?将'limit 1'添加到查询的底部。 –

+0

嗨,这是样本记录,只有当我在真实数据集中运行时,它将是百万条记录,所以如果我限制1,它将只选择一条记录,它不会从每个名称返回1条记录,gaol将为concat选择最大重复所有名字... – hi4ppl

0

SqlFiddleDemo

select name, 
     newvalue 
from (
    select name, 
      CONCAT(loca,locb) newvalue, 
      COUNT(CONCAT(loca,locb)) as total, 
      row_number() over (order by COUNT(CONCAT(loca,locb)) desc) as att 
    from Table1 
    where loca is not null 
     and locb is not null 
    GROUP BY name, CONCAT(loca,locb) 
) tt 
where att=1 
+0

你解决了你的问题吗? –