Count appearances of a value in any column of a table - mysql

How can I count up the total appearances of a value in multiple columns?
Raw Table:
col1 col2
---- ----
don sam
jon sam
mike lisa
sam lisa
lisa beth
Result:
name appearances
---- -----------
don 1
sam 3
jon 1
mike 1
lisa 3
beth 1
I've tried writing a join where getting the total count is a subquery, but this seems awkward. I'm assuming MySQL has some way to handle this nicely.

Here's one way using UNION ALL:
select name, count(1) cnt
from (
select col1 as name from yourtable
union all
select col2 from yourtable
) t
group by name
SQL Fiddle Demo
Results:
NAME CNT
-----------
beth 1
don 1
jon 1
lisa 3
mike 1
sam 3

try this
select name , count(*) as appearances from
(
select col1 as name from Raw
union all
select col2 as name from Raw
)t
group by name
DEMO HERE

Related

MySQL count multiple GroupBy

I have data like this
id otherid name
1 123 banana
2 123 banana
3 123 banana
4 456 grape
5 456 grape
6 789 orange
7 111 banana
How can I get output like this: (with MySQL query)
name count
banana 2
grape 1
orange 1
Try this:
SELECT
f.`name`,
COUNT(DISTINCT (f.`otherid`))
FROM
`fruits` f
GROUP BY f.`name`
You can use COUNT with GROUP BY:
SELECT
`name`, COUNT(*)
FROM
write_you_table_name
GROUP BY
`name`,`otherid`
Using distinct in the count function is recommended. But if you prefer not to using it, try this:
select name,count(*) from
(select name from fruit group by name,otherid) t
group by name;
SELECT name , count(*) as count FROM tb_stock GROUP BY other_id

MySQL order by certain values even if not existing in the table

I have a Table is looking like this
id name
----- ------
1 Mark
2 Mike
3 John
4 Mike
5 Mike
6 John
7 Mark
Notice that there isn't name Paul in the table so when ordering by name I want it to be like this ('Mark', 'Paul', 'Mike','John')
id name
----- ------
1 Mark
7 Mark
Null Paul
2 Mike
4 Mike
5 Mike
3 John
6 John
I already have tried the field method and it is removing Paul because it is not in the table
ORDER BY Field(name, 'Mark', 'Paul', 'Mike','John')
you can't order by a value that not exist so you should add a proper set of data for ordering
select a.id, t.name
from (
select 'Mark' name, 1 my_order
union
select 'Paul', 2
union
select 'Mike', 3
union
select 'John', 4 ) t
left join my_table a on a.name = t.name
order by t.my_order
Instead of a subquery with union you could use a proper utility table

Removing redundant rows from db table

I have a database table like the following (bad design I know, but there are a ton of rows like this):
person1 | person2 | counselor
Jane Doe | John Doe | Mary Smith
John Doe | Jane Doe | Mary Smith
Frank Jones| Ann Jones | Tom Jones
Ann Jones | Frank Jones | Tom Jones
I'm trying to figure out how to just select one of the 'unique' rows so that a result would look like:
person1 | person2 | counselor
Jane Doe | John Doe | Mary Smith
Frank Jones| Ann Jones | Tom Jones
I've tried various things like SELECT distinct and SELECT MIN(person1), etc., but am striking out.
You will have 6 permutations of (person1,person2,counselor) and you can use union with all of them. Finally use a where clause so only one row per combination will be returned.
Fiddle with sample data
select * from (
select person1,person2,counselor
from tablename
union
select person1,counselor,person2
from tablename
union
select person2,person1,counselor
from tablename
union
select person2,counselor,person1
from tablename
union
select counselor,person2,person1
from tablename
union
select counselor,person1,person2
from tablename) t
where person1 < person2 and person2 < counselor
SQL Fiddle Demo
I include a case where no reverse duplicated and also another case where person1 = person2, also include P2.* in select just for debug.
SELECT P1.person1, P1.person2, P1.counselor, P2.*
FROM patient P1
LEFT JOIN patient P2
ON P1.person1 = P2.person2
AND P1.person2 = P2.person1
AND P1.counselor = P2.counselor
WHERE
concat(P1.person1, P1.person2) <= concat(P2.person1, P2.person2)
OR P2.person1 is null
When P2 is NULL mean there isn't a reverse combination of person1, person2
But when the combination exists only choose the smaller one as string concatenation.
With ranked_records AS
(
select *,
ROW_NUMBER() OVER(Partition By person1, person2, counselor
Order By person) [ranked]
from address
)
select * from ranked_records
where ranked > 1
For more detail, like how to delete latest records and keep the older one.
For MySql and if you have Id, try this:
DELETE A1
From Address A1
Where Exists (Select * From Address A2
Where A2.person1= A1.person1
AND A2.person2 = A1.person2
AND A2.counselor = A1.counselor
AND A1.AddressID > A2.AddressID)

mysql - get number of occurrences from "different" query results

I was wondering how could I get the number of occurrences of a common string from different results (or using OR in my query, as example below).
table example:
id | name | rank
1 | name1 | 1
2 | name1 | 1
3 | name2 | 1
4 | name3 | 1
5 | name1 | 2
6 | name1 | 2
7 | name3 | 2
Now, I need to count number of occurrences for rank = 1 and rank = 2, without duplicating the count.
doing something like this:
SELECT name, COUNT(DISTINCT name)
AS name_num FROM table WHERE rank = 1 GROUP BY name;
results is
name1 | 1
name2 | 1
name3 | 1
perfect, but now I need to include some other result (i.e. rank = 1 OR rank = 2) and get the occurrences from each name, without duplicating it.
the wanted result for query example using table example and rank = 1 OR rank = 2 should be:
name1 | 2
name2 | 1
name3 | 2
I'll try to explain the result I want:
name1 is present when rank = 1 (+1) and when rank=2 (+1);
name 2 is only present when rank=1
name3 is present when rank = 1 (+1) and when rank=2 (+1);
Is it possible?
Select Name,
Count(Distinct Rank) as Ranks
from TableName
where Rank=1 or Rank=2
Group By Name
Sql Fiddle Demo
You want COUNT(DISTINCT rank), not COUNT(DISTINCT name). Since you're grouping by name, there will only be one distinct name in each group.
SELECT name, COUNT(DISTINCT rank) name_num
FROM table
WHERE rank in (1, 2)
GROUP BY name
Select Name,
Count(distinct Rank) as Ranks
from TableName
where Rank=1 or Rank=2
Group By Name
This is what you asked for?

MySQL query help wanted

I'm relatively new to MySQL, and it's possible that what I want may not be possible to achieve with one single query. I have the following table structure:
name | value1 | value2
I have several records where the names are the same but different values. What I would like to do is to select distinct names, but their values should be added. Here's an example:
john | 1 | 2
jane | 6 | 3
mark | 2 | 5
mark | 3 | 1
So the query (if possible) would return
john | 1 | 2
jane | 6 | 3
mark | 5 | 6
Yes, you can using group by.
Like
select name , sum(value1) as sum1, sum(value2) as sum2 from mytable group by name
You use group by with agregating functions.
SELECT name, sum(value1) as total1,sum(value2) as total2 FROM mytable GROUP BY name
SELECT name, sum(value1) as column1 ,sum(value2) as column2
FROM mytable
GROUP BY name
I recommend to learn a few lessons on MySQL.
Me as a beginner it was very helpful.
$query = "SELECT name, SUM(value1) as v1, SUM(value2) as v2 FROM table GROUP BY name";