i have a table like this
id | block1 | block2 | block3
-----------------------------------------------
1 | John | John |
2 | Mark | |
3 | | Frank | Frank
how i count how many john, Mark and Frank are in the table? es.
John - 2
Mark - 1
Frank - 2
i use the query below but is only for one column and i don't know how have the same result for all the columns. I think is a helpful query becouse i read is possible have the results maybe for the last month or year
SELECT block1,
COUNT(*) AS total
FROM table
GROUP BY block1
ORDER BY total DESC;
thank you
(sorry for my english)
You can use UNION ALL to unpivot the table, then apply grouping:
SELECT block, COUNT(*) AS 'total'
FROM (
SELECT block1 AS block
FROM mytable
UNION ALL
SELECT block2
FROM mytable
UNION ALL
SELECT block3
FROM mytable) AS t
GROUP BY block
Select columns separately and use UNION to merge them. Then COUNT them. You can try this -
SELECT col_block, COUNT(col_block) FROM (
(SELECT block1 col_block,
FROM table)
UNION ALL
(SELECT block2 col_block,
FROM table)
UNION ALL
(SELECT block3 col_block,
FROM table)) tbl
GROUP BY col_block
Related
I have a table in mysql
==================================================
ID | TEAM_ID | PTS | COMPETITION
==================================================
1 | 1 | 10 | zc
--------------------------------------------------
2 | 1 | 15 | po
--------------------------------------------------
3 | 1 | 5 | sp
I'm trying to find out the right query to get result of SUM of columns PTS where column team id equals 1. This is what I've got
SELECT SUM(pts) FROM
(SELECT SUM(pts) FROM `stats_2016/2017` WHERE `team_id`='1' AND `competition`='zc'
UNION ALL
SELECT SUM(pts) FROM `stats_2016/2017` WHERE `team_id`='1' AND `competition`='po'
UNION ALL
SELECT SUM(pts) FROM `stats_2016/2017` WHERE `team_id`='1' AND `competition`='sp'
)
I would like to get a result of 30 (10+15+5)...
Three changes:
Assign an alias to the inline view (derived table)
Assign an alias to the expression in the first SELECT of the inline view. (That alias will be used the column name in the derived table.(
in the outer query, reference the column in the inline view by the assigned alias.
SELECT SUM(v.totpts) AS totpts
-- 3.-- ^^^^^^
FROM ( SELECT SUM(pts) AS totpts FROM `stats_2016/2017` ...
-- 2.-- ^^ ^^^^^^
UNION ALL
SELECT SUM(pts) FROM `stats_2016/2017` ...
) v
-- 1.-- ^
Try with Group by and IN mysql operator,which work with dynamic value of team_id
Like this
SELECT SUM(pts) FROM `stats_2016/2017` WHERE `competition` in ('zc','po','sp')
group by `team_id`
I have this two mysql tables
main_date_family
main_date_non_family
I want to count duplicate records from two tables like this
---------------------
| id_file | c |
---------------------
| 1 | 3 |
| 6 | 2 |
| 14 | 5 |
You need to use UNION ALL to achieve the output:
SELECT
t.id_file,
COUNT(*) AS c
FROM
(
SELECT id_file FROM main_date_family
UNION ALL
SELECT id_file FROM main_date_non_family
) AS t
GROUP BY t.id_file
EDIT:
SELECT
t.id,
COUNT(t.id) AS c
FROM
(
SELECT id_file AS id FROM main_date_family
UNION ALL
SELECT id_file FROM main_date_non_family
) AS t
WHERE t.id IN ( SELECT A.id_file
FROM main_date_family A
INNER JOIN main_date_non_family B ON A.id_file = B.id_file
)
GROUP BY t.id
Explaining the idea:
Get all the common id_files.
Now combine all the id_files from those two tables. Mind it that
this combination holds all the id_files from those two tables as
though it's a single table.
Now get only those id_files from second step which are found in
the first step.
Now the only step left is the GROUP BY & COUNT. I guess
you know well what's group by
I am trying to select distinct rows within my SQL table, however I'm not having luck in labeling the returned rows appropriately using the code below:
SELECT #row:=#row+1 as rank,
a.id,
a.name
FROM table a,
( SELECT #row:=0) b
GROUP BY a.id
ORDER BY a.name ASC
This query will return the following:
| RANK | ID | NAME
--------------------------
2 | 4483 | Bob
8 | 9453 | Joe
10 | 4543 | Maurice
What I want it to return is this, however:
| RANK | ID | NAME
--------------------------
1 | 4483 | Bob
2 | 9453 | Joe
3 | 4543 | Maurice
Would it be more appropriate for me to use a DISTINCT query for a query of this magnitude?
As per Marc B's solution, I decided to wrap my query with another one however instead I decided to Select DISTINCT columns rather than grouping them which would remove my margin of error, by using this code
SELECT #row:=#row+1 as rank, a.id, a.name FROM
(
SELECT DISTINCT id, name
FROM Table1
) a, (SELECT #row:=0) b
ORDER BY a.name ASC
I have a table that looks like this
id + kID
--------------------------
0 | 3
1 | 6
2 | 7
3 | 6
4 | 7
5 | 5
What I want to do is find the amount of rows where the kID occurs only once. So in this case the value of the variable should be 2 because kID: 3 and 5 occurs only once so i'm trying to count that while ignoring everything else. I am really stumped, thanks for any help.
This will show kIDs that occur only once:
SELECT kID, COUNT(kID)
FROM table
GROUP BY kID
HAVING COUNT(kID) < 2
Result
| KID | COUNT(KID) |
--------------------
| 3 | 1 |
| 5 | 1 |
See the demo
Then to get the total count of those:
SELECT Count(*) AS count
FROM (SELECT kid,
Count(kid)
FROM tbl
GROUP BY kid
HAVING Count(kid) < 2) a
Result
| COUNT |
---------
| 2 |
See the demo
Try this
SELECT
id,
count(kID) as `Count`
FROM mytable as t
GROUP BY kID
HAVING Count = 1
How about
select count(*) from
(select kid, count(*) from table group by kid having count(*) = 1)
You could do the following:
select count(*) from
(
select kID, COUNT(*) [c] from tableName
group by kID
) t
where t.c = 1
SELECT kID,
COUNT(kID)
FROM tableName
GROUP BY kID
HAVING COUNT(kID) = 1
You could do it with a sub-select. This should work, though might not be extremely efficient:
SELECT id, kID, COUNT(1) FROM (SELECT COUNT(1),kID FROM TABLE
GROUP BY kID
HAVING COUNT = 1)
One more way to do it. It will work as long as the (id) is the primary key of the table or there is a unique constraint on (kid, id):
SELECT COUNT(*) AS cnt
FROM
( SELECT NULL
FROM tableX
GROUP BY kid
HAVING MIN(id) = MAX(id)
) AS g ;
Tested at SQL-Fiddle
An index on (kid, id) will improve efficiency - and only one COUNT() will be done, not 2.
I am trying to find a MySQL query that will find distinct values in a particular field, count the number of occurrences of that value in 2 fields (1_user, 2_user) and then order the results by the count.
example db
+------+-----------+-----------+
| id | 1_user | 2_user |
+------+-----------+-----------+
| 1 | 2 | 1 |
| 2 | 3 | 2 |
| 3 | 8 | 7 |
| 4 | 1 | 8 |
| 5 | 2 | 8 |
| 6 | 3 | 8 |
+------+-----------+-----------+
expected result
user count
----- -----
8 4
2 3
3 2
1 2
The Query
SELECT user, count(*) AS count
FROM
(
SELECT 1_user AS USER FROM test
UNION ALL
SELECT 2_user FROM test
) AS all_users
GROUP BY user
ORDER BY count DESC
Explanation
List all the users in the first column.
SELECT 1_user AS USER FROM test
Combine them with the users from the second column.
UNION ALL
SELECT 2_user FROM test
The trick here is the UNION ALL which preserves duplicate values.
The rest is easy -- select the results you want from the subquery:
SELECT user, count(*) AS count
aggregate by user:
GROUP BY user
and prescribe the order:
ORDER BY count DESC
SELECT u, count(u) AS cnt
FROM (
SELECT 1_user AS u FROM table
UNION ALL
SELECT 2_user AS u FROM table
) subquery
GROUP BY u
ORDER by cnt DESC
Take the 2 queries:
SELECT COUNT(*) FROM table GROUP BY 1_user
SELECT COUNT(*) FROM table GROUP BY 2_user
Now combine them:
SELECT user, SUM(count) FROM
((SELECT 1_user as user FROM table)
UNION ALL
(SELECT 2_user as user FROM table))
GROUP BY user, ORDER BY count DESC;
I think this what you are looking for since your expected result did not include 7
select usr, count(usr) cnt from
(
select user_1 usr from users
union all
select user_2 usr from users
) u
where u.usr in (select user_1 from users)
group by usr
order by count(u.usr) desc