i have a table name 'room' with room_id and rental.
mysql> select * from room;
+---------+--------+
| room_id | rental |
+---------+--------+
| 1 | 2000 |
| 2 | 1890 |
| 3 | 1832 |
| 4 | 1833 |
| 5 | 1850 |
| 6 | 1700 |
| 7 | 2100 |
| 8 | 2000 |
| 9 | 2000 |
| 10 | 2000 |
+----------+--------+
10 rows in set (0.00 sec)
i tried find maximum matches rows and count into number from rental column.
mysql> select count(*),rental from room group by rental having count(*) >1;
+----------+--------+
| count(*) | rental |
+----------+--------+
| 4 | 2000 |
+----------+--------+
1 row in set (0.08 sec)
but my problem is i just want only one max number from rental which has max matches values and output like above.in above query will take a condition like count(*) > 1.but i want to check all rows from rental column instead of it condition.
USE ORDER BY AND LIMIT 1
select count(*) as cnt,rental from room group by rental order by cnt DESC limit 1;
Related
This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 3 years ago.
I'm struggling to do something in SQL which I'm sure must be simple, but I can't figure it out. I want the MAX() value of a group, but I also want the value of another column in the same row as the max value. Here is an example table definition:
mysql> desc Sales;
+---------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+-------+
| StoreID | int(11) | YES | | NULL | |
| Day | int(11) | YES | | NULL | |
| Amount | int(11) | YES | | NULL | |
+---------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)
And here is some data for it:
mysql> SELECT * FROM Sales;
+---------+------+--------+
| StoreID | Day | Amount |
+---------+------+--------+
| 1 | 1 | 44 |
| 1 | 2 | 31 |
| 1 | 3 | 91 |
| 2 | 1 | 93 |
| 2 | 2 | 32 |
| 2 | 3 | 41 |
| 3 | 1 | 48 |
| 3 | 2 | 95 |
| 3 | 3 | 12 |
+---------+------+--------+
9 rows in set (0.00 sec)
What I want to know is, what Day had the most sales (Amount) for each StoreID.
Now I know I can do this:
SELECT StoreID, MAX(Amount) FROM Sales GROUP BY StoreID;
+---------+-------------+
| StoreID | MAX(Amount) |
+---------+-------------+
| 1 | 91 |
| 2 | 93 |
| 3 | 95 |
+---------+-------------+
3 rows in set (0.00 sec)
That tells me the max amount of each store, but really what I'm after is the day that it occured. But I can't add Day back in to the query because it's not in the group by, and I don't think I really want to group by that value do I?
I'm not sure where to go from here.
In short, the results I want should look like this:
+---------+------+--------+
| 1 | 3 | 91 |
| 2 | 1 | 93 |
| 3 | 2 | 95 |
+---------+------+--------+
You want to filter. Here is one simple method using a correlated subquery:
select s.*
from s
where s.sales = (select max(s2.sales)
from sales s2
where s2.storeId = s.storeId
);
If your data is on the large side, you will want an index on sales(storeId, sales).
For the maximum amounts per store there won't exist a higher amount for the same store.
SELECT *
FROM Sales s
WHERE NOT EXISTS (
SELECT 1
FROM Sales s2
WHERE s2.StoreID = s.StoreID
AND s2.Amount > s.Amount
)
ORDER BY Amount ASC, StoreID ASC;
Test here
Typically you can just join the aggregating query back to get the rest of the row data...
SELECT s.*
FROM Sales AS s
INNER JOIN (
SELECT StoreID, MAX(Amount) AS MaxAmount
FROM Sales
GROUP BY StoreID
) AS m ON s.StoreID = m.StoredID AND s.Amount = m.MaxAmount
;
If there are multiple Sales with the MaxAmount for the same StoreID, the query will return all of them, not just one of them.
I have table like this
user_id workplace
1 Microsoft
1 Google
2 eBay
3 Panadora
3 Netflix
What I want is to have a table like this:
user_id places_worked
1 Microsoft,Google
2 eBay
3 Panadora,Netflix
Is there anyway in SQL that can do this?
As mentioned by #jarlh you could do this using group_concat
SELECT user_id,GROUP_CONCAT(workplace)
FROM yourtable
GROUP BY user_id;
you can use group by with group concat operation
SELECT user_id,GROUP_CONCAT(workplace)
FROM yourtable
GROUP BY user_id;
check following example
select * from payments;
+----+------------+---------+-------+
| id | date | user_id | value |
+----+------------+---------+-------+
| 1 | 2016-06-22 | 1 | 10 |
| 2 | 2016-06-22 | 3 | 15 |
| 3 | 2016-06-22 | 4 | 20 |
| 4 | 2016-06-23 | 2 | 100 |
| 5 | 2016-06-23 | 1 | 150 |
+----+------------+---------+-------+
5 rows in set (0.00 sec)
select c.user_id,group_concat(p.value) from calls c inner join payments p on p.user_id=c.user_id group by c.user_id;
+---------+-----------------------+
| user_id | group_concat(p.value) |
+---------+-----------------------+
| 1 | 10,150,10,150,10,150 |
| 2 | 100 |
+---------+-----------------------+
2 rows in set (0.00 sec)
i am not asking about 2nd highest salary in employee table , i am asking about 2nd highest salary of each employee.
click here to see employee table
inner query will return employees with their highest salary and then from out query those highest salaries will be filtered out
so you will get the second highest salary
SELECT MAX(T.salery),T.NAME FROM TABLE T
INNER JOIN (SELECT MAX(salery),NAME FROM TABLE GROUP BY NAME) TT
ON TT.NAME=T.NAME AND TT.SALERY!= T.SALERY
GROUP BY T.NAME;
example
mysql> SELECT * FROM payments;
+----+------------+---------+-------+
| id | date | user_id | value |
+----+------------+---------+-------+
| 1 | 2016-06-22 | 1 | 10 |
| 2 | 2016-06-22 | 3 | 15 |
| 3 | 2016-06-22 | 4 | 20 |
| 4 | 2016-06-23 | 2 | 100 |
| 5 | 2016-06-23 | 1 | 150 |
| 6 | 2016-06-23 | 2 | 340 |
+----+------------+---------+-------+
6 rows in set (0.00 sec)
mysql> select max(value),user_id from payments group by user_id;
+------------+---------+
| max(value) | user_id |
+------------+---------+
| 150 | 1 |
| 340 | 2 |
| 15 | 3 |
| 20 | 4 |
+------------+---------+
4 rows in set (0.00 sec)
mysql> select max(T.value),TT.user_id from payments T inner join (select max(value) as val,user_id from payments group by user_id) TT on T.value!=TT.val and T.user_id=TT.user_id group by T.user_id;;
+--------------+---------+
| max(T.value) | user_id |
+--------------+---------+
| 10 | 1 |
| 100 | 2 |
+--------------+---------+
2 rows in set (0.00 sec)
I think a very quick solution could be like this:
SELECT MAX(column) FROM table WHERE column < (SELECT MAX(column) FROM table)
Hope it helps-
select distinct (pc.id,pc.PolicyPremiumID) ,pc.policyPremiumCatID,b.nin, b.firstname+' '+coalesce((b.middleInitial),'')+' '+b.lastname as fullname, b.gender,ppc.amount from
bio_data b, medicalInsurance m, policy p, policyPremium pp,premiumCategories pc,policyPremiumCategory ppc
where b.nin=m.patientBin and m.policyID=p.id and (p.id=pp.policyID and pp.policyID=p.id) and pp.id=pc.policyPremiumID and pc.policyPremiumcatID=ppc.id
and p.id=82
in the above query, I want to have a distinct of those two columns
it is duplicating instead of 7 return values it bring 14
Can some please help? Thanks in advance!!!
Use the following method:
Sample select query which list all values:
mysql> select * from new_table;
+---------+------------+
| premium | sumassured |
+---------+------------+
| 1000 | 100000 |
| 2000 | 200000 |
| 3000 | 300000 |
| 1000 | 100000 |
| 1000 | 100000 |
| 3000 | 300000 |
| 4000 | 400000 |
+---------+------------+
7 rows in set (0.00 sec)
Select Query which lists Distinct Value from multiple Tables:
mysql> select distinct premium,sumassured from new_table;
+---------+------------+
| premium | sumassured |
+---------+------------+
| 1000 | 100000 |
| 2000 | 200000 |
| 3000 | 300000 |
| 4000 | 400000 |
+---------+------------+
4 rows in set (0.00 sec)
You can use the same for N number of Rows.
How can I update this to select all duplicates?
SELECT address FROM list
GROUP BY address HAVING count(id) > 1
Currently, I think it just returs the addresses which are duplciated. I want all duplicates.
Select * from list
where address in (
select address from list group by address
having count(*) > 1);
Look at this sample query I ran:
mysql> select * from flights;
+--------+-------------+
| source | destination |
+--------+-------------+
| 1 | 2 |
| 3 | 4 |
| 5 | 6 |
| 6 | 1 |
| 2 | 4 |
| 1 | 3 |
| 5 | 2 |
| 6 | 3 |
| 6 | 5 |
| 6 | 4 |
+--------+-------------+
10 rows in set (0.00 sec)
mysql> select * from flights where source in
(select source from flights group by source having count(*) > 1);
+--------+-------------+
| source | destination |
+--------+-------------+
| 1 | 2 |
| 5 | 6 |
| 6 | 1 |
| 1 | 3 |
| 5 | 2 |
| 6 | 3 |
| 6 | 5 |
| 6 | 4 |
+--------+-------------+
8 rows in set (0.00 sec)
If I'm correct, you're looking for the actual rows that contain duplicates -- so that if you have three rows with the same address, you return all three rows.
Here's how to do it:
SELECT * FROM list
WHERE address in (
SELECT address FROM list GROUP BY address HAVING count(id) > 1
);
This should generally work unless your address is a 'text' field or if your address table has more than a few thousand duplicates.
Are you looking for this?
SELECT * FROM list
WHERE id IN (
SELECT id FROM list
GROUP BY address HAVING count(id) > 1
);