I need to find the row with maximum count. There's only one table, so it should be easy, but it isn't.
Below is the content of table:
+------+------+------+
| row1 | row2 | row3 |
+------+------+------+
| 3 | 2 | 1 |
| 6 | 4 | 5 |
| 6 | 2 | 1 |
+------+------+------+
I need to find maximum count item for row1:
Using:
SELECT COUNT(*) AS c, row1 AS name
FROM draw
GROUP BY name;
Give me the result:
+------+------+
| c | name |
+------+------+
| 1 | 3 |
| 2 | 6 |
+------+------+
But I want to display only one row with the maximum "c":
+------+------+
| c | name |
+------+------+
| 2 | 6 |
+------+------+
Using:
SELECT MAX(c), name (
SELECT COUNT(*) AS c, row1 AS name
FROM draw GROUP BY name
) AS counts;
Give me result:
+------+------+
| c | name |
+------+------+
| 2 | 3 |
+------+------+
It means that it gives maximum count (c), but give first number in name row.
Is there way to fix it?
I think this is what you want, but your requirements are a little unclear. If this was any other platform you could use windowing functions would would be better.
SELECT *
FROM (
SELECT COUNT(*) AS c, row1
FROM draw
GROUP BY row1
) as Sub1
WHERE Sub.c = (
SELECT Max(c)
FROM (
SELECT COUNT(*) AS c
FROM draw
GROUP BY row1
) as Sub2
) as Sub3
select top 1 row1, count(*)
from draw
group by row1
order by count(*) desc
Order by the count descending (i.e. highest count first) and take the first one only.
You can try this
SELECT COUNT( `row1` ) AS c, `row1`
FROM `draw`
GROUP BY `row1`
ORDER BY `row1` DESC
LIMIT 1
Related
I am trying to form a mysql query where I want to concat all the previous values until the current row -1 . For example
select * from a;
+------+
| id |
+------+
| 1 |
| 3 |
| 4 |
+------+
Desired o/p
+------+============
| id |concat_prev_to_cur
+------+============
| 1 |null
| 3 |1
| 4 |1,3
+------+============
Can this be achieved with using SELECT only
Tried this but this doesn't work
with recursive b as (select id from a union all select concat(a.id,',',b.id) from b join a on a.id=b.id) select * from b;
Update: This seems to be close to the desired output
With b as (Select id, row_number() Over(order by id) r from a) select c.id,group_concat(b.id) from b join b c on b.r < c.r group by c.r ;
+------+--------------------+
| id | group_concat(b.id) |
+------+--------------------+
| 3 | 1 |
| 4 | 1,3 |
+------+--------------------+
Maybe a recursive query is not needed.
SELECT id, (
SELECT GROUP_CONCAT(id) AS ids
FROM a AS agg
WHERE agg.id < a.id
) AS ids
FROM a
db<>fiddle
Is there a way to sum rows only when they meet certain condition?, If they dont they must be copied to the new table. For example, if i have this table
| id | A | B |
--------------
| 1 | a | 2 |
| 1 | b | 4 |
| 1 | c | 1 |
| 2 | a | 4 |
| 3 | a | 1 |
| 3 | b | 5 |
I want an output like this
| id | A | B |
--------------
| 1 | a,b | 6 |
| 1 | c | 1 |
| 2 | a | 4 |
| 3 | a,b | 6 |
It will only sum if column 'A' is 'a' or 'b', it will just copy the value if its 'c'
You can do this in two different Select queries. In first Select query, just consider the cases where A has either values 'a' or 'b'. In the second Select query, consider the rest of the values (A NOT IN ('a','b'))
Use UNION ALL to combine the results into a Derived Table.
Order the Derived table result-set in ascending order by id.
We use aggregation functions like Group_concat() and Sum() to get comma separated string (for a and b), and sum of the B values, respectively.
Try the following:
SELECT dt.*
FROM
(
SELECT id,
GROUP_CONCAT(DISTINCT A) AS A,
SUM(B) AS B
FROM your_table
WHERE A IN ('a','b')
GROUP BY id
UNION ALL
SELECT id,
A,
B
FROM your_table
WHERE A NOT IN ('a', 'b')
GROUP BY id, A, B
) AS dt
ORDER BY dt.id ASC
Your example does not show all possible cases.
But I think that probably you don't need to overcomplicate solution with UNION
http://sqlfiddle.com/#!9/d473d3/6
SELECT id,
GROUP_CONCAT(A),
SUM(B)
FROM abc
GROUP BY CONCAT(id, IF(A IN ('a','b'),'$',A))
My records is:
name | id | AVG(point) as point
a | 1 | 6
b | 2 | 6
c | 3 | 5
d | 4 | 5
e | 5 | 4
f | 6 | 3
g | 7 | 2
How to select record below:
1.I want to select top 3 record, result follow:
name | id | AVG(point) as point
a | 1 | 6
b | 2 | 6
c | 3 | 5
d | 4 | 5
e | 5 | 4
2.I want to select record not into top 3, result follow:
name | id | AVG(point) as point
f | 6 | 3
g | 7 | 2
How can I do?
There are several ways to do these. Here's a couple using in and not in.
For the top 3, you can use in:
select *
from yourtable
where point in (select distinct point
from yourtable
order by 1 desc
limit 3)
For the rest, use not in instead:
select *
from yourtable
where point not in (select distinct point
from yourtable
order by 1 desc
limit 3)
Other methods include exists with not exists and distinct with joins.
select *
from yourtable as t1
inner join (select distinct point
from yourtable
order by 1 desc
limit 3) as t2
on t1.point = t2.point
For the second part of your question, do not use
desc
Table Mytable1
Id | Actual
1 ! 10020
2 | 12203
3 | 12312
4 | 12453
5 | 13211
6 | 12838
7 | 10l29
Using the following syntax:
SELECT AVG(Actual), CEIL((#rank:=#rank+1)/3) AS rank FROM mytable1 Group BY rank;
Produces the following type of result:
| AVG(Actual) | rank |
+-------------+------+
| 12835.5455 | 1 |
| 12523.1818 | 2 |
| 12343.3636 | 3 |
I would like to take AVG(Actual) column and UPDATE a second existing table Mytable2
Id | Predict |
1 | 11133
2 | 12312
3 | 13221
I would like to get the following where the Actual value matches the ID as RANK
Id | Predict | Actual
1 | 11133 | 12835.5455
2 | 12312 | 12523.1818
3 | 13221 | 12343.3636
IMPORTANT REQUIREMENT
I need to set an offset much like the following syntax:
SELECT #rank := #rank + 1 AS Id , Mytable2.Actual FROM Mytable LIMIT 3 OFFSET 4);
PLEASE NOTE THE AVERAGE NUMBER ARE MADE UP IN EXAMPLES
you can join your existing query in the UPDATE statement
UPDATE Table2 T2
JOIN (
SELECT AVG(Actual) as AverageValue,
CEIL((#rank:=#rank+1)/3) AS rank
FROM Table1, (select #rank:=0) t
Group BY rank )T1
on T2.id = T1.rank
SET Actual = T1.AverageValue
Given the following table:
id | group_s | name
_____________________
1 | 1 | pollo
2 | 1 | cordero
3 | 1 | cerdo
4 | 2 | tomates
5 | 2 | naranjas
6 | 2 | manzanas
I would like to randomly select one line from every group.
Example of possible outputs (since it is random):
id | group_s | name
_____________________
3 | 1 | cerdo
5 | 2 | naranjas
or
id | group_s | name
_____________________
1 | 1 | pollo
6 | 2 | manzanas
and so on..
I don't have a clue how to do it. I suppose I should multiselect the table.
I did try the following without success:
SELECT T2.* FROM (
SELECT group_s
FROM mytable
GROUP BY group_s ORDER BY RAND() LIMIT 1) AS T1
JOIN mytable AS T2
ON T1.group_s = T2.group_s;
Use the window function ROW_NUMBER() OVER(PARTITION BY group_s) with ORDER BY NEWID() to randomly get the ordering, something like this:
WITH CTE
AS
(
SELECT *,
ROW_NUMBER() OVER(PARTITION BY group_s
ORDER BY newid()) AS RN
FROM yourTable
)
SELECT id , group_s , name
FROM CTE
WHERE RN = 1;
See it in action here:
SQL Fiddle Demo