1

我有linkage以下值的表格

++++++++++++++++++++++++++
+ company_id +  industry +
++++++++++++++++++++++++++
+     1      +    a      +
+     1      +    b      +
+     2      +    a      +
+     2      +    c      +
+     3      +    a      +
+     4      +    c      +
+     5      +    a      +
++++++++++++++++++++++++++

有没有一种方法可以将我的行业分组以通过 desc 顺序示例获得最高计数排序。

a = count 4
c = count 2
b = count 1

然后删除重复的行业,只留下每个行业计数较高的行业company_id


编辑 1

此编辑基于 OP 评论I wish to only have the industry with the highest count, and deleting the rest of the entry for the same company_id. say for company_id 1, we will delete the second row, for company_id 2 we will delete the forth row.

下面是我所拥有的。

++++++++++++++++++++++++++
+ company_id +  industry +
++++++++++++++++++++++++++
+     1      +    a      +
+     1      +    b      +
+     1      +    c      +
+     2      +    a      +
+     2      +    c      +
+     3      +    a      +
+     4      +    c      +
+     5      +    a      +
++++++++++++++++++++++++++

正如我们在列行业中看到的那样,a 具有最大计数,我想保留每个重复的 company_id 的条目并删除其余的所有条目。

考虑 company_id=1。我需要删除第二行和第三行。考虑 company_id=2。我需要删除第五行。对于 id=3,4,5 什么都不会发生,因为它们没有重复。

所以应该在我的表中的最终数据是

++++++++++++++++++++++++++
+ company_id +  industry +
++++++++++++++++++++++++++
+     1      +    a      +
+     2      +    a      +
+     3      +    a      +
+     4      +    c      +
+     5      +    a      +
++++++++++++++++++++++++++
4

3 回答 3

1

这个怎么样?

SELECT industry, count(industry) as "total" 
FROM linkage 
GROUP BY industry 
ORDER BY total DESC

在 sqlfiddle 演示


编辑 1

你能看看下面的问题。

如何从我的数据库中删除重复记录

我认为这就是你要找的。

于 2013-02-21T06:54:01.693 回答
1
select n.industry,count(n.industry) count from linkage n
group by n.industry
order by count desc

select t3.company_id,t4.industry from
(select t2.company_id,max(t2.count) count from(
select m.company_id,m.industry,t1.count from linkage m
join
(select n.industry,count(n.industry) count from linkage n
group by n.industry
order by count desc)t1
on m.industry = t1.industry
order by m.company_id)t2
group by t2.company_id
order by t2.company_id)t3
join
(
select m.company_id,m.industry,t1.count from linkage m
join
(select n.industry,count(n.industry) count from linkage n
group by n.industry
order by count desc)t1
on m.industry = t1.industry
order by m.company_id)t4
on t3.company_id = t4.company_id 
and t3.count = t4.count

在 sqlfiddle 演示

于 2013-02-21T07:35:18.837 回答
1
select t6.company_id,t6.industry from
(select t5.company_id,t5.industry,
row_number() over (partition by t5.company_id order by t5.company_id) rn
from 
(select t3.company_id,t4.industry from
(select t2.company_id,max(t2.count) count from(
select m.company_id,m.industry,t1.count from linkage m
join
(select n.industry,count(n.industry) count from linkage n
group by n.industry
order by count desc)t1
on m.industry = t1.industry
order by m.company_id)t2
group by t2.company_id
order by t2.company_id)t3
join
(
select m.company_id,m.industry,t1.count from linkage m
join
(select n.industry,count(n.industry) count from linkage n
group by n.industry
order by count desc)t1
on m.industry = t1.industry
order by m.company_id)t4
on t3.company_id = t4.company_id 
and t3.count = t4.count)t5
)t6
where t6.rn = '1'
于 2013-02-21T08:34:21.687 回答