2012-02-09 3 views
0

단순한 것으로 보이는 문제를 발견했지만 문제가 있습니다. 어떤 도움이라도 대단히 감사합니다. 내가보고 싶은카운트 쿼리가 적절하게 그룹화되지 않았습니다.

CREATE TABLE IF NOT EXISTS `match_history` (
    `id` int(11) NOT NULL auto_increment, 
    `match_id` int(11) NOT NULL, 
    `team_id` int(11) NOT NULL, 
    `player_id` int(11) NOT NULL, 
    `map` varchar(150) NOT NULL, 
    `score` int(11) NOT NULL, 
    `outcome` varchar(25) NOT NULL, 
    `notes` longtext NOT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=271 ; 

match_id links to matches.id 
team_id links to matchteams.id 
player_id links to persons.id 

는 승리 팀 당 손실이지만, 위의 match_history 표는 경기마다 팀에 여러 행이 때문에 나는 문제가 있어요.

INSERT INTO `match_history` (`id`, `match_id`, `team_id`, `player_id`, `map`, `score`, `outcome`, `notes`) VALUES 
(221, 44, 2, 124, 'Village', 1570, 'Win', ''), 
(220, 44, 2, 115, 'Village', 1600, 'Win', ''), 
(219, 44, 2, 92, 'Village', 2740, 'Win', ''), 
(218, 44, 4, 105, 'Village',1000, 'Loss', ''), 
(217, 44, 4, 111, 'Village', 1220, 'Loss', ''), 
(216, 44, 4, 130, 'Village', 1440, 'Loss', ''), 
(215, 44, 4, 122, 'Village', 2160, 'Loss', ''), 
(214, 44, 4, 130, 'Seatown', 1410, 'Loss', ''), 
(213, 44, 4, 122, 'Seatown', 1600, 'Loss', ''), 
(212, 44, 4, 111, 'Seatown', 1790, 'Loss', ''), 
(211, 44, 4, 105, 'Seatown', 1790, 'Loss', ''), 
(210, 44, 2, 113, 'Seatown', 1020, 'Win', ''), 
(209, 44, 2, 124, 'Seatown', 1480, 'Win', ''), 
(207, 44, 2, 115, 'Seatown', 2850, 'Win', ''), 
(208, 44, 2, 92, 'Seatown', 2160, 'Win', ''), 
(222, 44, 2, 113, 'Village', 900, 'Win', ''), 
(223, 45, 1, 123, 'Hardhat', 2970, 'Win', ''), 
(224, 45, 1, 26, 'Hardhat', 2930, 'Win', ''), 
(225, 45, 1, 107, 'Hardhat', 1710, 'Win', ''), 
(226, 45, 3, 101, 'Hardhat', 1530, 'Loss', ''), 
(227, 45, 3, 100, 'Hardhat', 1420, 'Loss', ''), 
(228, 45, 3, 125, 'Hardhat', 1010, 'Loss', ''), 
(229, 45, 1, 107, 'Seatown', 2520, 'Win', ''), 
(230, 45, 1, 123, 'Seatown', 2260, 'Win', ''), 
(231, 45, 1, 26, 'Seatown', 1560, 'Win', ''), 
(232, 45, 3, 101, 'Seatown', 1510, 25, 3, 42, 0.6, 0, 0, 0, 'Loss', ''), 

이 내가 사용하던 쿼리이지만 1

select mh.team_id as team_id, COUNT(distinct(mh.match_id)) as matches, 
    count(mh.map) as maps, mh.outcome, SUM(IF(mh.outcome='Win',1,0)) as wins, 
    SUM(IF(mh.outcome='Loss',1,0)) as losses, m.id, mt.name as teamname 
FROM match_history mh, matches m, ladders l, match_teams mt 
WHERE mh.team_id = mt.id and mh.match_id = m.id and 
    m.ladder_id = l.id and l.type = 'internal' 
GROUP by mh.team_id 
ORDER by wins desc 
+0

마지막 삽입물은 무엇입니까? –

답변

0

각 행을 세고 난 havent 한 한동안 SQL을 시도하기 때문에 확실하지 오전하지만 시도 : 예를 들어

이 .. 이 오류를

select team_id, count(*) from match_history 
where outcome = 'Win' 
group by team_id 

을주는 경우가 리터와 그 다음 우리가 할 수있는 노동 조합으로 작동하는지 알려주세요 osses

편집 : 이러한 귀찮은 일이 ... 당신은 더 나은이의 좋은 사용을 ... 내가 보냈다 일 (물론,이어야 분 : P)이 에지고 난에 SQL 서버에서 테스트 한 필요한 경우 mysql에 맞게 조정하십시오. 행운을 비네! : D

select aa.tid, aa.win, COALESCE(c.loss, 0) from 
(SELECT distinct a.team_id tid, COALESCE(b.win, 0) win 
    FROM [match_history] a 
    left join 
    (select team_id, count(*) win 
    from [match_history] 
    where outcome = 'Win' 
    group by team_id) b 
    on a.team_id = b.team_id)aa 

    left join 
    (select team_id, count(*) loss 
    from [match_history] 
    where outcome = 'Loss' 
    group by team_id) c 
    on aa.tid = c.team_id 

결과 :

tid win Loss 
1 6 0 
2 7 0 
3 0 3 
4 0 8 
+0

Bhrugesh, 아직도 너무 많은 것을 보여 줘서 고마워. ruakh,이 데이터의 정규화에 대한 귀하의 제안은 무엇입니까? –

+0

@ PaulIlle이 새로운 쿼리가 도움이 되었기를 바랍니다. 행운을 빈다. : D –

1

MySQL은 표준 SQL과는 달리, 당신은 GROUP BY에없는과하지 않은 필드 목록의 열합니다 (SELECT 절)를 포함 할 수 있습니다 집계 열 — http://dev.mysql.com/doc/refman/5.6/en/group-by-hidden-columns.html —을 참조하십시오. 그러나 조심해야하거나 예기치 않은 결과가 발생할 수 있습니다. 이들은에 의존하기 때문에 귀하의 경우에는, 당신은 GROUPBYmh.team_id을 보내고, 그래서 당신은 안전하게 팀-ID (예를 들어, mt.name는 안전합니다)에 따라 열을 포함 할 수 있지만 mh.outcomem.id 선택하지 수 특정 검색 그래서, 나는 그 열을 제거 할 것입니다.

그런 다음, 우리는 우리가 별개의 일치 ID를 계산되도록 SUM(1-or-0)COUNT(DISTINCT match-ID-or-NULL)에 변경해야합니다

select mh.team_id as team_id, 
     COUNT(distinct(mh.match_id)) as matches, 
     count(mh.map) as maps, 
     COUNT(DISTINCT IF(mh.outcome='Win',m.id,NULL)) as wins, 
     COUNT(DISTINCT IF(mh.outcome='Loss',m.id,NULL)) as losses, 
     mt.name as teamname 
    FROM match_history mh, 
     matches m, 
     ladders l, 
     match_teams mt 
WHERE mh.team_id = mt.id 
    and mh.match_id = m.id 
    and m.ladder_id = l.id 
    and l.type = 'internal' 
GROUP by mh.team_id 
ORDER by wins desc 

당신이 원하는 일을해야한다.

+0

좋은 답변이지만 그룹별로 열을 선택하는 경우조차도 피해야한다고 주장합니다. –

+0

빠른 응답을 주셔서 감사합니다. 그러나 나는 여전히 내가 기대하는 데이터를 얻지 못하고있는 것처럼 보입니다. –

+0

와우. 나는 입력을 누르십시오 ... 나는 각 사람 입력에 대해 계산을 되찾고 있습니다. 예를 들어 한 팀에 4 명이있을 경우 match_id와 team_id의 고유 한 조합 대신 입력 된 행을 각각 계산하는 것처럼 보이기 때문에 1 대신 4를 얻습니다. –

관련 문제