I am on MySQL and having trouble calculating totals.I am trying to figure out an SQL query which will return the correct result using the following data and query. I want to get a count of total cases. In my result from my query the F and I counts are right the M case count is off. If the case has an F then it should be counted as an F case only not also an M case which my query is doing.
Data:
BKNUM CG Charge Description Severity
123456 1 POSS CONT SUB W/I MAN/DEL/SELL F
123456 1 POSS LEGEND DRUG W/O PRESCRIPTION M
654321 1 VIOLATION OF PROBATION M I
987654 1 AGGRAVATED ASSAULT F
987654 1 POSS OF HANDGUN WHILE INTOXICATED M
987654 1 POSSESSION OF PROHIBITED WEAPON M
876543 1 VIOLATION OF PROBATION M I
765432 1 CRIM ATT-BURGLARY BUILDING F
765432 1 POSSESSION OF BURGLARY TOOLS M
234567 1 POSS MARIJUANA W/I MAN/DEL/SELL F
234567 1 DRIVING WHILE LICENSE S/R/C M
234567 1 IMPROPER DISPLAY OF REGISTRATION M
345678 1 DRIVING WHILE LICENSE S/R/C M
345678 1 EVADING ARREST M
345678 1 RESISTING OFFICIAL DETENTION M
345678 1 UNLAWFUL POSS WEAPON M
345678 1 POSS OF CONT SUBSTANCE M
345678 1 CRIM ATT-POSS OF CONT SUB-MARIJUANA M
Query:
Select Severity as 'Charge CLass',
Count(Distinct BKNUM, CG) as 'Total Cases',
Count(BKNUM) as 'Toatl Charges'
From test
group by severity
Result I get:
Charge Class Total Cases Total Charges
F 4 4
I 2 2
M 5 12
Result should be:
Charge Class Total Cases Total Charges
F 4 4
I 2 2
M 1 12
Any help would be greatly appreciated.