简体   繁体   中英

How Count or Total number of row after on groupby in mysql

Hy I have 2 tables

1.application

id | name    | status
====================
1  | morvick | complete
2  | siti    | prosess
3  | boby    | complete

2.application_test

id | application_id | test_id | result
======================================
1 | 1 | 1 | 70
2 | 1 | 2 | 80
3 | 1 | 3 | 90
4 | 2 | 1 | 60
5 | 2 | 2 | 80
6 | 2 | 3 | 70
7 | 3 | 1 | 90
8 | 3 | 2 | 70
9 | 3 | 3 | 60
10| 3 | 4 | 80

Myquery Like This :

SELECT test_id, 
SUM(IF(app.status='complete',apt.result,0)) AS complete_sum, 
SUM(IF(app.status='process',apt.result,0)) AS process_sum  
FROM application_test AS apt 
JOIN application AS app ON app.id=apt.application_id 
GROUP BY apt.test_id

and to be like this :
test_id | SUM(result = complete) | SUM(result = proses) |
1 | 90 | 50
2 | 80 | 40
3 | 90 | 60
4 | 80 | 70

My quetion is: how I can get the total or Count number of rows after I do a query GROUPBY.. ? For example, 4 total number

is this what you want?

SELECT (select count(*) from application_test) as total_row, 
test_id, 
SUM(IF(app.status='complete',apt.result,0)) AS complete_sum, 
SUM(IF(app.status='process',apt.result,0)) AS process_sum  
FROM application_test AS apt 
JOIN application AS app ON app.id=apt.application_id 
GROUP BY apt.test_id

Use subquery,

Select *, Count(*) as total_rows From(
SELECT test_id, 
SUM(IF(app.status='complete',apt.result,0)) AS complete_sum, 
SUM(IF(app.status='process',apt.result,0)) AS process_sum  
FROM application_test AS apt 
JOIN application AS app ON app.id=apt.application_id 
GROUP BY apt.test_id)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM