Question

Hi I want to GROUP BY file_serial but only when file_serial > 1 that mean no duplicate file_serial with value bigger than 1

Current Table

so it should look like

Wanted Table

Any idea how, would be great. Thanks.

Was it helpful?

Solution

One way would be

select * from table
where  file_serial > 1
group by file_title
having count(*) > 1

UNION

select * from table

UPDATE

The above will work only if all the selecting columns with the same value. In this case you may need to change as since file_id is different for the same title.

    select * from table
    where  file_serial > 1
    group by file_title
    having count(*) > 0 

    UNION

    select * from table
    file_serial <= 1 

DEMO

OTHER TIPS

Try below query, where no need of extra having clause-

SELECT 
file_id,file_title,file_serial 
FROM mytable 
WHERE file_serial<=1 
UNION ALL 
SELECT 
file_id,file_title,file_serial 
FROM mytable 
WHERE file_serial>1 
GROUP BY file_serial;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top