SQL Group by Having> 1

时间:2015-08-06 14:43:16

标签: sql sybase having

也许标题不是我用来描述问题的最佳标题 我正在处理的表结构的一个例子如下图所示。我需要编写一个查询来提取"制造商"有多个记录。所以最终结果我会有LINUX UBUNTU 5.6和LINUX REDHAT 7.8

只需返回复制的MANUFACTURE很容易,我可以使用具有count(*)>的分组来实现这一点。 1但是当涉及到返回复制的制造和相应的列时,我想出的问题。

Table Example

3 个答案:

答案 0 :(得分:4)

  

返回复制的MANUFACTURE很简单,我可以使用分组having count(*) > 1

来做到这一点

这是一个好的开始。现在使用manufacture的列表来选择其余数据:

SELECT *
FROM software
WHERE manufacture IN (
    -- This is your "HAVING COUNT(*) > 1" query inside.
    -- It drives the selection of rows in the outer query.
    SELECT manufacture
    FROM software
    GROUP BY manufacture
    HAVING COUNT(*) > 1
)

答案 1 :(得分:3)

试试这个:

  Select * from myTable
  Where Manufacture In
      (Select Manufacture
       from myTable
       Group By Manufacture
       Having count(*) > 1)

答案 2 :(得分:0)

你有没有试过像:

select p.manufacture, p.product, p.version
from (select manufacture, count(*)
    from products
    group by manufacture) as my_count
inner join products as p on p.manufacture = my_count.manufacture
where my_count > 1