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
Related
i have a sample data like this:
Now i want to get lastest data used to have user_id = 41 and highest price, output like this:
How can i do it with SQL command line ?
Thank for read
Try this query
select User_id,auction_id,price from tablename where price in(select price from tablename where id in(select max(id) from tablename group by user_id))
As per your output that you wish try this:
select user_id,auction_id,price
from table_name
where user_id=41
order by id desc;
If you have updated output incorrectly then try it as per your provided conditions-
select user_id,auction_id,price
from mytable
where user_id=41
order by auction_id;
But if as per your output you want 1 latest row of user_id=41 and then all rows of highest price then you can try it-
SELECT user_id,auction_id,price
FROM mytable
WHERE user_id=41
ORDER BY id DESC LIMIT 1
UNION
SELECT b.user_id,b.auction_id,price
FROM mytable b
JOIN (
SELECT MAX(price) AS price
FROM mytable
) a ON a.price=b.price;
m new in mysql
here is my table
now i want to count "count_id" where count of 'questionID' greater than 2
Try this :
SELECT COUNT(count_id) FROM myTable WHERE questionID > 2
select count(Count_ID),QuestionID,SurveyId from table
where QuestionID>2
group by QuestionID,SurveyID
select count(count_id) from yourtable where questionID > 2
If you want to count unique ID:
select count(DISTINCT count_id) from table_name where questionID > 2
SELECT COUNT(count_id) FROM table_name WHERE questionID > 2
Group by Count_ID and count their distinct questions. Stay with those that have more than two. Then count how many IDs you got.
select count(*)
from
(
select count_id
from mytable
group by count_id
having count(distinct questionid) > 2
) x;
EDIT: If count_id + questionid happen to be unique for the table, you can replace count(distinct questionid) with count(*).
You can also try the statement below:
select count(count_id) CountOfID,count_id from mytable
where questionID > 2 group by count_id;
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;
is it possible in an SQL query to add UNION after order by 1
SELECT * FROM table1 WHERE etc='1' ORDER BY 11;
can we add a union select query beside 11 to be like this ?
SELECT * FROM table1 WHERE etc='1' ORDER BY 11 UNION select etc etc etc ...;
In MySQL, you can enclose the order by clause within a subquery and union the results of multiple subqueries; something like this:
SELECT * FROM (SELECT * FROM table1 WHERE etc='1' ORDER BY 11) sq1
UNION ALL
SELECT * FROM (SELECT * FROM table2 WHERE etc='2' ORDER BY 12) sq2
...
Maybe something like this:
SELECT
*,
11 AS orderby
FROM
table1
WHERE
etc='1'
UNION
select
*,
10 AS orderby
FROM
table2
ORDER BY
orderby
You can use like this:
SELECT * FROM table1 WHERE etc='1'
union
SELECT * FROM table1 WHERE etc='1'
ORDER BY any_column
You can only use ORDER BY at the end of the query after the last union. Basically all SELECTs are done first, then the entire result set is ordered.
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