I currently have:
SELECT Name, COUNT(*) as Total
FROM DataTable
WHERE Name IN ('A', 'B', 'C')
GROUP BY Name
Resulting output:
Name Total
--------------
A 2
B 5
C 3
Instead I want this:
Name Total
--------------
A 10
B 10
C 10
Here 10 is a total of 2 + 5 + 3 (total number of records with name = A/B/C)
How do I do this?
To get your desired result you can use SUM() OVER ()
on the grouped COUNT(*)
. Demo
SELECT Name,
SUM(COUNT(*)) OVER () as Total
FROM DataTable
WHERE Name IN ('A', 'B', 'C')
GROUP BY Name
Get rid of the group by
and use distinct
:
select distinct Name, count(*) over() as Total
from t
where name in ('A', 'B', 'C')
rextester demo: http://rextester.com/WDMT68119
returns:
+------+-------+
| name | Total |
+------+-------+
| A | 10 |
| B | 10 |
| C | 10 |
+------+-------+
If you count all of the records and then do a cross join on all the different names
SELECT a.NAME
,x.Total
FROM DataTable a
CROSS JOIN (
COUNT(*) AS Total FROM DataTable
) x
GROUP BY a.NAME
,x.Total
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.