Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Group by Having > 1

Tags:

sql

sybase

having

Maybe the title was not the best I could use to discribe the issue an example of the table structure I am dealing with is in the image below. I need to write a query to pull all records for "Manufactures" that have more than one record. So the end result I would have LINUX UBUNTU 5.6 and LINUX REDHAT 7.8

Just returning the duplicated MANUFACTURE is easy and I can do that with using grouping having count(*) > 1 but when it comes to returning the duplicated manufacture and the corresponding columns with it is the issue I am coming up with.

Table Example

like image 474
scripter78 Avatar asked Feb 15 '26 16:02

scripter78


2 Answers

returning the duplicated MANUFACTURE is easy and I can do that with using grouping having count(*) > 1

That's a good start. Now use that list of manufactures to select the rest of the data:

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
)
like image 108
Sergey Kalinichenko Avatar answered Feb 18 '26 09:02

Sergey Kalinichenko


try this:

  Select * from myTable
  Where Manufacture In
      (Select Manufacture
       from myTable
       Group By Manufacture
       Having count(*) > 1)
like image 25
Charles Bretana Avatar answered Feb 18 '26 09:02

Charles Bretana