I want to get the count how many values are repeating in the result in a column in result of a query.
The result set I am getting from a complex query is –
svn rvn eng count(*)
1 1 Boy 1
2 1 Teacher 1
3 1 Chair 1
3 2 Chairwoman 1
3 3 Chairperson 1
4 1 Without 1
4 2 Without fail 1
5 1 Anyone 1
5 2 Anyone else 1
6 1 Permission 1
I just want to get the number of duplicate values in SVN coloumn in fourth coloumn.
i.e.
svn rvn eng count(*)
1 1 Boy 1
2 1 Teacher 1
3 1 Chair 3
3 2 Chairwoman 3
3 3 Chairperson 3
4 1 Without 2
4 2 Without fail 2
5 1 Anyone 2
5 2 Anyone else 2
6 1 Permission 1
Please Help me out on this, Also please tell me what effect this can cause on the efficiency on the query??
*Note – * I want the count of the values that are picked up in the result set.
there are more entries with svn 1,2,4,6 in the table. but count only how many has been selected.
Thanks in advance 🙂
EDIT1
Here is my current query :-
SELECT `svn` , `rvn` ,`eng` , count(*) FROM
(SELECT `svn`, `rvn`, `eng`, `hin`
FROM `table1`
WHERE `SN` = @sn
UNION DISTINCT
SELECT `table1_refer`.`sn_svn` AS 'svn',
`table1`.`rvn`, `table1`.`eng` ,
`vocab_rel`.`hin`
FROM `table1`
JOIN `table1_refer`
WHERE `table1_refer`.`rSN` = `table1`.`SN`
AND `table1_refer`.`svn` = `table1`.`svn`
AND `table1_refer`.`SN` = @sn
) AS SUBQUERY
GROUP BY `svn`,`rvn`
ORDER BY `svn`, `rvn`
You can use a correlated subquery inside your current complex query to do this like so:
SQL Fiddle Demo
This will give you: