I have a problem on how to fetch the number of occurrences of a value in between two columns in MySQL.
id1 col1 col2
2 5 3
3 3 4
4 2 1
5 1 3
6 null 2
How am I able to get the number of occurences between the two columns like the following?
value occurrence
3 3
1 2
2 2
4 1
5 1
You could union all the columns and then apply a count aggregate function:
SELECT val, COUNT(*) AS occurrence
FROM (SELECT col1 AS val
FROM mytable
UNION ALL
SELECT col2 AS val
FROM mytable) x
GROUP BY val
ORDER BY occurrence DESC
Depending on the actual data (number of rows per value) pre-aggregating might be more efficient, simply try it out:
SELECT val, SUM(occurrence) AS occurrence
FROM (SELECT col1 AS val, COUNT(*) AS occurrence
FROM mytable
UNION ALL
SELECT col2 AS val, COUNT(*) AS occurrence
FROM mytable) x
GROUP BY val
ORDER BY occurrence DESC
Related
I have a MySQL table (below):
I can fetch the min or max of the column values simply but my problem is:
If any 3 or more fields value matched then fetch that value (if 3 fields values matched more than one value then the lowest of those values), for row #1 2100 will be the output and for row#3 55 will be the output.
If no field value matched with a minimum of 3 fields the lowest value of the entire row will be fetched, for row# 2 the output will be 1900.
I can use IF (e.g. select if(col1 = col2, col1, col2) from table) in select but I didn't find a solution for this situation. Can anyone help to write a MySQL query for this?
Thanks in advance!
You can use this query to get the results you want. It uses two nested subqueries: the first unpivots the data into a single column; the second then counts how many of each column value occur for a given ID value. The outer query then tests the maximum of the counts; if it is >= 3 then the minimum value which has a count of 3 or more is chosen, otherwise the minimum column value is chosen as the minimum column:
SELECT ID,
CASE WHEN MAX(cnt) >= 3 THEN MIN(CASE WHEN cnt >= 3 THEN col END)
ELSE MIN(col)
END AS min_col
FROM (
SELECT ID, col, COUNT(col) AS cnt
FROM (SELECT ID, col1 AS col FROM data
UNION ALL
SELECT ID, col2 FROM data
UNION ALL
SELECT ID, col3 FROM data
UNION ALL
SELECT ID, col4 FROM data
UNION ALL
SELECT ID, col5 FROM data
UNION ALL
SELECT ID, col6 FROM data
UNION ALL
SELECT ID, col7 FROM data
) d
GROUP BY ID, col
) dd
GROUP BY ID
Output (for your sample data):
ID min_col
1 2100
2 1900
3 55
Demo on SQLFiddle
hi I have one table which I am trying to get the count of different rows on the same row field for example I a have this rows
Function field1 field2 field3
'dog' 1 1 5
'dog' 1 1 5
'cat' 1 1 5
'dog' 1 2 6
The output for this table should be:
Function count
'dog' 2
'cat' 1
because there are two different rows for 'dog' (some of the field are different) and one row for 'cat'
thank you so much
Assuming that you want to count rows identical based on a given subset of fields, this would be:
SELECT func, COUNT(*) FROM (
SELECT DISTINCT func, field1, field2 FROM yourtable
) AS tmp GROUP BY func;
This first distincts all the rows, so you get
'dog' 1 1 5
'cat' 1 1 5
'dog' 1 2 6
and then counts the first field, thus giving 2 and 1.
You can even do something like
SELECT func, COUNT(*) AS different, SUM(total) AS total FROM (
SELECT func, field1, field2, field3, COUNT(*) AS total
FROM yourtable
GROUP BY func, field1, field2, field3
) AS tmp GROUP BY func;
which will give:
func different total
dog 2 3
cat 1 1
This should give you the results you are looking for:
Select Function, Count(*) As Count
From
(
Select Distinct
Function,
Field1,
Field2,
Field3
From YourTable
) As A
Group By Function
I'd like to select all rows of a subquery with the minimum value in a given field. Here's some toy examples of the techniques I've tried so far:
-- 1.
select
id, min(foo)
from
(select 1 AS id, 2 AS foo
union select 2 AS id, 2 AS foo
union select 3 AS id, 3 AS foo) a;
-- 2.
select
min(foo)
from
(
select 1 AS id, 2 AS foo, 0 AS const
union select 2 AS id, 2 AS foo, 0 AS const
union select 3 AS id, 3 AS foo, 0 AS const) a
group by const;
-- 3.
select
id
from
(select 1 AS id, 2 AS foo
union select 2 AS id, 2 AS foo
union select 3 AS id, 3 AS foo) a
where id = (select id from a where min(foo) = foo);
-- 4.
select
id
from
(select 1 AS id, 2 AS foo
union select 2 AS id, 2 AS foo
union select 3 AS id, 3 AS foo) a
where foo = (select min(foo));
-- 5.
select r.*
from
(
select min(foo) t
from
(select 1 AS id, 2 AS foo
union select 2 AS id, 2 AS foo
union select 3 AS id, 3 AS foo) a
) m
INNER JOIN a ON m.t = r.foo;
The actual query I'm working on is similar to the examples, in that it's made up of several smaller queries UNIONed together. The overall goal here is to lookup a row in a central table based on the fields of an association table k to which it is joined, where k is highest priority table. The result is a sort of tree view of rows from similar (but different tables).
I've mentioned this so in case someone can see that I'm going about this in a roundabout way they can shed some light on the bigger picture. But for now my angle is to select by a taking a minimum value on a field in the subquery.
Use order by and limit:
select t.*
from t
order by foo
limit 1;
Note: this only returns one row with the minimum, even if there are duplicates. The t is your subquery or table.
If you want all of them, then you need to include the table definition twice:
select t.*
from t
where t.foo = (select min(t2.foo) from t t2);
Could you help me with simple table SUM and COUNT calculating?
I've simple table 'test'
id name value
1 a 4
2 a 5
3 b 3
4 b 7
5 b 1
I need calculate SUM and Count for "a" and "b". I try this sql request:
SELECT name, SUM( value ) AS val, COUNT( * ) AS count FROM `test`
result:
name val count
a 20 5
But should be
name val count
a 9 2
b 11 3
Could you help me with correct sql request?
Add GROUP BY. That will cause the query to return a count and sum per group you defined (in this case, per name).
Without GROUP BY you just get the totals and any of the names (in your case 'a', but if could just as well have been 'b').
SELECT name, SUM( value ) AS val, COUNT( * ) AS count
FROM `test`
GROUP BY name
You need group by
select
name,
sum(value) as value,
count(*) as `count`
from test group by name ;
So I'm trying to write a mysql script to find the number of consecutive repeats in 'value' column of this table.
id value result
-- ----- ------
1 1 0
2 1 1
3 2 0
4 3 0
5 3 1
So in this case I want get the value 2
Get the next value using user variables,
GROUP so consecutive values more than 2 are not counted again,put all in a subquery,and use a simple CASE to increment the value you need in case value=next value.Add salt and pepper.
SELECT SUM(CASE WHEN y.value=y.next_value THEN #var+1 ELSE #var END) consecIds
FROM
(SELECT t.id, t.value, next_id, n.value next_value
FROM
(
SELECT t.id, t.value,
(
SELECT id
FROM table1
WHERE id > t.id
ORDER BY id
LIMIT 1
) next_id
FROM table1 t,(SELECT #var:=0)x
) t LEFT JOIN table1 n
ON t.next_id = n.id
GROUP BY t.value,n.value)y
FIDDLE
SELECT COUNT(DISTINCT column_name) FROM table_name;
DISTINCT will erase duplicated repetitions from specified column in result.
COUNT will count the rows in result.
The COUNT(DISTINCT column_name) function returns the number of distinct values of the specified column.