Mysql Select GROUP BY with value greater than 1 - mysql

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
so it should look like
Any idea how, would be great.
Thanks.

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

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;

Related

SQL Query for Test Review [duplicate]

Is it possible to group results and then filter by how many rows are in the group?
Something like this:
SELECT * FROM mytable WHERE COUNT(*) > 1 GROUP BY name
You want to use HAVING to filter on the aggregate function.
SELECT name, COUNT(*)
FROM mytable
GROUP BY name
HAVING COUNT(*) > 1
You need to use HAVING
SELECT * FROM mytable GROUP BY name HAVING COUNT(*) > 1
Although, SELECT * doesn't make much sense when you're grouping. I assume it's just for an example
You want a HAVING clause.
SELECT *
FROM mytable
GROUP BY name
HAVING COUNT(*) > 1
Use having in your query:
SELECT * FROM mytable GROUP BY name having COUNT(*) > 1

how to find next max(id) in a table

lets say
My table contains
Id Status
1 0
2 1
3 0
4 0
5 1
6 0
I need output like
Id Status
5 1
I tried like Max(id) but it gives output as
id status
6 0
I can only suppose, that you want to know about the maximum id of those entries having Status=1, right? Then use
select max(id) from mytable where Status=1
Try this:
SELECT id, status
FROM myTable ORDER BY `id` DESC LIMIT 1 , 1
I have assumed that you are looking for the second highest Id record values.
I guess you would like to have the max(id) where Status=1?
Then use select max(id) from table where Status=1
Since you want to get the absolute max from both columns use :
SELECT GREATEST(MAX(id), MAX(status));
Your question is a bit unclear what it is you actually want. From your example, I guess you want the maximum ID for the max status.
This is the max id for every status:
select max(id), status from table
group by status;
This will result in 5,1 and 6,0.
You could then filter out what you don't need, e.g.
select * from (
select max(id), status from t
group by status
) maxidperstatus
where maxidperstatus.status = (select max(status) from t);
Try this also
select Id,Status
from table
where Id=(
select MAX(Id) from table where Id <> (select MAX(Id) from table)
)
I assume that you want the Id where the status is the biggest
Try this
select max(status),id from table group by id order by max(status) DESC

How can I return rows where only duplicates within a certain column exist?

Suppose that my database looks something like the following:
First Entry | Second Entry | Third Entry
0 0 0
0 1 2
2 1 0
2 0 1
3 0 0
I am trying to return the subset of this table where an element in the FirstEntry column repeats at least once. So in this case it would return all but the final row. How can I go about this? I've tried using things such as count() but have only managed to achieve grouping instead of returning the actual rows I am curious in (in particular, I care aboout Second and Third entry but only when First entry has repeated at least once).
Using IN()
select * from your_table
where FirstEntry in
(
select FirstEntry
from your_table
group by FirstEntry
having count(*) > 1
)
or using a JOIN
select t1.*
from your_table t1
join
(
select FirstEntry
from your_table
group by FirstEntry
having count(*) > 1
) t2 on t1.FirstEntry = t2.FirstEntry
Try this:
select *
from my_table
where First_Entry IN(SELECT First_Entry From my_table
group by First_entry having count(*) > 1)

SELECT * FROM Table WHERE col has at least 3 identical

I need to a MySQL query that returns the results of a table where the elements of a specific column are identical in at least 'n' records.
example pseudocode:
SELECT * FROM Table WHERE col has at least 3 identical
select * from table where col in (
select col from table group by col having count(*) > 3
)
SELECT col, COUNT(*) AS total FROM tbl GROUP BY col HAVING total >= 3
select * from table a
inner join (
select col,n=count(*)
from table
group by col
having count(*) >= 3 ) as b
on (a.col=b.col)
mysql allows you to write something like the following:
select *
from table
group by col
having count(*) >= 3
other dbmss force you to specify all columns explicitly and use MAX or MIN on them
SELECT DISTINCT * FROM TableName t WHERE 2 < (SELECT COUNT(*) FROM TableName p WHERE p.col = t.col)

Filter by COUNT(*)?

Is it possible to group results and then filter by how many rows are in the group?
Something like this:
SELECT * FROM mytable WHERE COUNT(*) > 1 GROUP BY name
You want to use HAVING to filter on the aggregate function.
SELECT name, COUNT(*)
FROM mytable
GROUP BY name
HAVING COUNT(*) > 1
You need to use HAVING
SELECT * FROM mytable GROUP BY name HAVING COUNT(*) > 1
Although, SELECT * doesn't make much sense when you're grouping. I assume it's just for an example
You want a HAVING clause.
SELECT *
FROM mytable
GROUP BY name
HAVING COUNT(*) > 1
Use having in your query:
SELECT * FROM mytable GROUP BY name having COUNT(*) > 1