SELECT DUPLICATE MAX(ID) - mysql

I have data similar to the following in my table:
id territory_id platform_id title_id other columns
1 US ITUNES 155 10 others columns...
100 US ITUNES 155 10 others columns...
101 FR ITUNES 155 10 others columns...
I need to SELECT MAX(ID) on all duplicate rows, based on (territory_id, platform_id, title_id).
The query on the above data set should return the id 100, since the only duplicate above based on (territory_id, platform_id, title_id) is ('US', 'ITUNES', 155) and the MAX(ID) of that duplicate entry is 100 (not 1).
How would I build this query? So far I have:
SELECT id FROM table GROUP BY territory_id, platform_id, title_id

You can GROUP BY the 3 fields, then using HAVING clause you can identify duplicates. Using MAX you can get the max ID for each of those groups having duplicates:
SELECT MAX(ID), territory_id, platform_id, title_id
FROM MyTable
GROUP BY territory_id, platform_id, title_id
HAVING COUNT(*) > 1
SQL Fiddle Demo

I think you are looking for something like this
select DISTINCT territory_id, max(ID)
,platform_id,title_id
from test t
GROUP BY territory_id,platform_id,title_id
HAVING COUNT(*)>1
Here are the results from the replicated table
territory_id id platform_id title_id
US 100 ITUNES 155
If you needed to you could go one step further and declare a variable if this will make its way to a report.

Related

select all rows with lowest product_id

i have this table and i want to select all rows with product id 105. i can easily do this if i know product id and put in where clause but i dont know product id and it increases once this id work is completed it will delete these rows and search for next lowest product id.
with order bt product_id i can do but how to ensure that all rows are listed. if i will give 100 limit then first 10-15 may be lowest after that it will start listing next higher product_id rows
here is table
id name product_id some_columns
1 dff 105 gfff
2 fg 109 ffgfgf
3 tt 106 gttytt
4 tt 105 trtr
5 trr 112 trrrt
6 rrr 111 rttttr
7 ttyt 108 ttrtrtr
8 rrrr 105 rrerer
SELECT id, name, product_id, some_columns
FROM table_name
WHERE product_id = (SELECT MIN(product_id) FROM table_name)
here you can see that lowest product_id is 105 but i dont have any control on how many times it will appear in table. in some case it may be 10 rows and in some case it may be 250 rows.
so order by product_id will not work. as giving limit will list all id initial few rows may be 105 but after than when all 105 rows are listed it will start listing all rows which is higher than 105
the best solution would be if i could use where product_id=105 but my bad luck i dont have any control on product id so cant use product id in where clause. next big problem is i want to use it efficiently so we have indexed product_id column
i was exploring min value option but i am highly doubtful about its efficiency and probable affect on mysql
so any help will be great
You can try something like
SELECT id, name, product_id, some_columns
FROM table_name
WHERE product_id = (SELECT MIN(product_id) FROM table_name)
As far as I understood you want to select all the rows that match the minimum product_id. Be it 101, 102 or whatever, it's uncontrollable, so you get the minimum product_id in the row and then you select the rows that has the current minimum product_id.
You could try using a subquery for min_id group by product
select m.*
from table_name m
inner join (
select min(id) min_id, product_id
from table_name
group by product_id
) t on t.min_id = m.id

Order by top five repeated IDs

There is a table in my database duplicate_id which contains multiple ids and also contains many duplicate ids in it. What I have been trying to do is to sort top five ids which are being repeated the most in the table duplicate_id. Kindly let me know how can i do that
Table Structure: ID | Message
Expected output:
ID | Number of repeats
201 8
212 7
205 5
209 3
229 2
SELECT ID, COUNT(*) AS `Number of repeats`
FROM duplicate_id
GROUP BY ID
ORDER BY COUNT(*) DESC
LIMIT 5
Try the order by so sort your results:
Select * from table order by repeats desc limit 5
well... try this (I don't know mysql, so I have to guess)
select ID,
count(*) as 'Number of Repeats'
from duplicate_ID
group by ID
order by 2
Another approach would be
select ID, 'Number of Repeats'
from (
select ID,
count(*) as 'Number of Repeats'
from duplicate_ID
group by ID
) x
order by 'Number of Repeats'

SQL query distinct with additional select

I need to perform a query in MySQL that returns distinct values for product_id but also I need to select and return 'id' field which is in that particular table.
This query will return distinct product_id's without id:
SELECT DISTINCT product_id FROM orders_cart
This query will use distinct on both fields which and I want to use it on product_id and see the id
SELECT DISTINCT id, product_id FROM orders_cart
It would be quite easy to do on pgsql but I have no idea how to do this on mysql.
Your query is not well-defined: Consider this table
id product_id
1 1
2 2
3 1
4 2
What should your query result be? If you mean
id product_id
1 or 3 1
2 or 4 2
you are in the land of non-deterministic queries.
What you could do is
SELECT MIN(id), product_id FROM orders_cart GROUP BY product_id
which would deterministically produce
id product_id
1 1
2 2
Thats my final code:
The most important bits for this issue were line 1,2 and 4 :)
GROUP BY did the trick :)
SELECT orders_cart.id, product_id, order_id
FROM orders_cart
LEFT JOIN orders_order ON orders_cart.order_id=orders_order.id
WHERE orders_order.status='Wysłano'
GROUP BY orders_cart.product_id

Condition for counting distinct rows in an SQL query

I have a table in a MySQL database with an ID column. This is not a key of the table and several rows can have the same ID.
I don't really know SQL but I already figured out how to obtain the number of distinct IDs:
SELECT COUNT(DISTINCT ID) FROM mytable;
Now I want to count only those IDs which appear more than 2 times in the table.
So if the ID column contains the values
3 4 4 5 5 5 6 7 7 7
the query should return 2.
I have no idea how to do this. I hope someone can help me!
Btw, my table contains a huge number of rows. So if there are several possibilities I would also be happy to know which solution is the most efficient.
Try this:
SELECT COUNT(ID) FROM (
SELECT ID FROM mytable
GROUP BY ID
HAVING COUNT(ID) > 2) p
select count(*) from
(select count(id) as cnt,id from mytable group by id) da
where da.cnt>2
The inner query will give you how many elements does each id have. And the outer query will filter this.
SELECT
COUNT(ids)
FROM
(SELECT
COUNT(ID)AS ids
FROM
mytable
GROUP BY
ID
HAVING
ids>2
)AS tbl1
Updated :
SELECT count(ID)
FROM (
SELECT ID FROM mytable
GROUP BY ID
HAVING count(ID) > 2
) p
should do what you need

Select distinct column along with some other columns in MySQL

I can't seem to find a suitable solution for the following (probably an age old) problem so hoping someone can shed some light. I need to return 1 distinct column along with other non distinct columns in mySQL.
I have the following table in mySQL:
id name destination rating country
----------------------------------------------------
1 James Barbados 5 WI
2 Andrew Antigua 6 WI
3 James Barbados 3 WI
4 Declan Trinidad 2 WI
5 Steve Barbados 4 WI
6 Declan Trinidad 3 WI
I would like SQL statement to return the DISTINCT name along with the destination, rating based on country.
id name destination rating country
----------------------------------------------------
1 James Barbados 5 WI
2 Andrew Antigua 6 WI
4 Declan Trinidad 2 WI
5 Steve Barbados 4 WI
As you can see, James and Declan have different ratings, but the same name, so they are returned only once.
The following query returns all rows because the ratings are different. Is there anyway I can return the above result set?
SELECT (distinct name), destination, rating
FROM table
WHERE country = 'WI'
ORDER BY id
Using a subquery, you can get the highest id for each name, then select the rest of the rows based on that:
SELECT * FROM table
WHERE id IN (
SELECT MAX(id) FROM table GROUP BY name
)
If you'd prefer, use MIN(id) to get the first record for each name instead of the last.
It can also be done with an INNER JOIN against the subquery. For this purpose the performance should be similar, and sometimes you need to join on two columns from the subquery.
SELECT
table.*
FROM
table
INNER JOIN (
SELECT MAX(id) AS id FROM table GROUP BY name
) maxid ON table.id = maxid.id
The problem is that distinct works across the entire return set and not just the first field. Otherwise MySQL wouldn't know what record to return. So, you want to have some sort of group function on rating, whether MAX, MIN, GROUP_CONCAT, AVG, or several other functions.
Michael has already posted a good answer, so I'm not going to re-write the query.
I agree with #rcdmk . Using a DEPENDENT subquery can kill performance, GROUP BY seems more suitable provided that you have already INDEXed the country field and only a few rows will reach the server. Rewriting the query giben by #rcdmk , I added the ORDER BY NULL clause to suppress the implicit ordering by GROUP BY, to make it a little faster:
SELECT MIN(id) as id, name, destination as rating, country
FROM table WHERE country = 'WI'
GROUP BY name, destination ORDER BY NULL
You can do a GROUP BY clause:
SELECT MIN(id) AS id, name, destination, AVG(rating) AS rating, country
FROM TABLE_NAME
GROUP BY name, destination, country
This query would perform better in large datasets than the subquery alternatives and it can be easier to read as well.