I have table(T1) with userid(int), user_name(char) and contact(char) as columns.
I need to iterate over the table to find out which user_name is present multiple times in the table and the count. And I will be using the count to delete the user_name if the occurrence is more than three times.
I tried using
select user_name,count(user_name) from T1 group by user_name having count(*)>3
The output is
user_name EXPR_1
chris 4
Fred 5
Now if I need to use the values in EXPR_1. How to iterate over this result set without creating a new/temp table.
Thanks
Use your query as a sub-select:
DELETE FROM T1 WHERE user_name IN (
SELECT user_name
FROM T1
GROUP BY user_name
HAVING COUNT(user_name) > 3
)
Response to comment:
Well, now I'm confused as to what you're asking for. If all you want to do is sort by highest count, then just do this:
SELECT
user_name,
COUNT(user_name) AS numOccurrences
FROM T1
GROUP BY user_name
HAVING COUNT(user_name) > 3
ORDER BY numOccurrences DESC
Now when you iterate through them, it will be ordered by the number of occurrences, highest to lowest.
Are you asking how to iterate over the results? If so, that completely depends on what tool you're using to execute this query and how you're sending it to your other application. Please elaborate on that.
Related
If i have multiple tables like on the picture below, then I want to get the most active user (counted from posted_by column), for example on this picture Mike is the most active user, what query should i run? Thank you
SELECT posted_by, count(*) AS total
FROM(SELECT posted_by FROM article
UNION ALL
SELECT posted_by FROM ebook
UNION ALL
SELECT posted_by FROM forum_thread) AS counted
GROUP BY posted_by
ORDER BY total DESC
LIMIT 1
Since all tables got the columns, you can use UNION ALL. Also I suggest to use UserID on posted_by field instead of name. If you want to output the name just join the tables with user table.
right now I'm trying to return the biggest COUNT(DISTINCT column)-number from a mysql table.
It's hard to describe, so I'll give you an example:
My table has the following columns: s_id, k_id, p_id.
Now I want to count the different s with the condition that every entry has the same p_id, too. I need this to prepare a HTML-Table (so i know how many Columns this table will have).
Data Example:
This is what I got, so far:
SELECT COUNT(DISTINCT k_id) AS a FROM `table`
the problem with this is, that there may be 4 different k_ids but 3 of them are related to p_id = 1 and the last one is releated to p_id = 2.
a returns 4 instead of 3.
Thanks for support!
I think you want this:
select p_id, count(distinct s_id) as cnt
from table
group by p_id
order by cnt desc
limit 1;
Please consider this:
select max(count(distinct(k_id))) from table
group by p_id
I am trying to perform a search on a table with structure like
id, mls_id, address, agent_id. What I would like to do is pull all the records for agent_id but not pull more than one if there is same mls. For example:
Select * From table WHERE agent_ID = 1234
might pull up 5 records but let's say two of the records have an mls_id that is the same. Is there a way to just pull one of those and still keep all the other results in tact?
This seems to do the trick:
What it does is to choose the record with the minimal id from those that have the same mls_id
SELECT id, mls_id, address, agent_id
FROM MyTable t1
WHERE t1.agent_id=1 AND t1.id =
(SELECT Min(t2.id)
FROM MyTable t2
WHERE agent_id=1 AND t2.mls_id=t1.mls_id
GROUP BY t2.mls_id)
Here is the fiddle example : SqlFiddle
SELECT DISTINCT *
FROM table
WHERE agent_ID = 1234
Using the DISTINCT keyword will drop duplicate records from your result set.
I am trying to select the latest entry of the duplicate entries against the ticket_id in a mysql table , My current is something like this.
SELECT * ,
COUNT(*) AS cnt ,
ticket_id
FROM temp_tickets
GROUP BY ticket_id
ORDER BY id DESC
It gives the number of times a row is duplicated but i am able to select the latest one of those mulptiple rows
Let say i have 3 ticket_id's which got duplicated for 5 times so now i want to select the latest occurrence from all these 3 id's .
Lemme know if i have to be more specific.
Thanks
Here's one way (assuming "latest" means "greatest id")
SELECT temp_tickets.*
FROM temp_tickets
JOIN ( SELECT MAX(id) AS id
FROM temp_tickets
GROUP BY ticket_id
HAVING COUNT(*) > 1
) latest ON latest.id = temp_tickets.id
I suspect it might be possible to come up with a more efficient solution involving user variables but I'll leave that to someone else...
I have a problem ordering my results correctly when using the group by. It seems to show the first entry in the database instead of the most recent in the group.
Example:
id(autoincrement) | name
1 | anne
2 | james
3 | anne
4 | brad
As you can see I have "anne" entered multiple times which is why I am using the group by. I would then like it to display the "anne" that is the most recent, which would be the entry "3". Instead it displays the first "anne"(1)
My query
"Select * FROM TABLE GROUP BY name ORDER BY id DESC
Any help would be greatly appreciated.
The problem is that you're selecting all the fields (using * is seldom a good idea) so each row is unique therefore there is nothing to group on.
Try:
SELECT
Name,
MAX(ID)
FROM
TABLE
GROUP BY
Name
A possible solution:
SELECT id, name
FROM TABLE
WHERE id IN (SELECT MAX(id) FROM TABLE GROUP BY name)
Try this; it will work:
SELECT * FROM TABLE
INNER JOIN (SELECT MAX(id) AS id
FROM TABLE
group by name)
ids ON TABLE.id = ids.id
Try
SELECT DISTINCT name
FROM table
ORDER BY id DESC
Building on another anwer already provided, this SQL will avoid having to GROUP BY Name:
SELECT
DISTINCT Name,
MAX(ID)
FROM
TABLE