简体   繁体   中英

trying to get count statistics for simple db table

I want to extract name, # kills, # deaths from a simple database table.

The table has

_from (killer), _to (person who died), and then a wiretap of the event. In the case of pvp, we want to count if the person died or got a kill and add it to their total

Can this be done with a query or will I have to script something? This don't work @ all:

SELECT (select COUNT( _from ) from wiretaps group by _from) as num_kills, 
       (select COUNT( _to ) from wiretaps group by _to) as num_deaths, 
        _from as name
FROM wiretaps
WHERE message LIKE  "%PvP:%"

http://sqlfiddle.com/#!2/1e5d9/1

| _FROM |    _TO | MESSAGE | ID |
---------------------------------
|  naez |  salty |    PvP: |  1 |
|  naez | prince |    PvP: |  2 |
| chuck |   naez |    PvP: |  3 |

expected output:

| name |    num_kills | num_deaths |
---------------------------------
|  naez   |  2 |    1 | 
|  prince |  0 |    1 | 
| chuck   |  1 |    0 | 
| salty   |  0 |    1 | 
select name, sum(num_kills) num_kills, sum(num_deaths) num_deaths
from (
    select _from name, count(*) num_kills, 0 num_deaths
    from wiretaps
    where message like '%PvP:%'
    group by _from
    union all
    select _to name, 0 num_kills, count(*) num_deaths
    from wiretaps
    where message like '%PvP:%'
    group by _to) x
group by name

SQLFIDDLE

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