I need to know how many dates a team won (value1 > value2) consecutively and know the date of the first and the last match played won. And show a list of 10 ranges order by won and date. I know its not easy. Its not easy to me.. I try to make some queries and subquery but without good results. Thanks I have a table With
CREATE TABLE `games` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`value1` int(11) NOT NULL,
`value2` int(11) NOT NULL,
`played` date NOT NULL,
PRIMARY KEY (`id`),
KEY `played` (`played`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
insert into games (value1, value2, played)
values
("2", "3", "1943-05-09"),
("4", "3", "1943-08-15"),
("1", "8", "1943-08-22"),
("0", "4", "1943-08-29"),
("1", "0", "1943-09-12"),
("1", "3", "1943-09-26"),
("6", "1", "1943-10-03"),
("3", "2", "1943-10-10"),
("3", "3", "1944-07-16"),
("1", "1", "1944-08-06"),
("4", "1", "1944-09-24"),
("0", "7", "1944-10-08"),
("0", "1", "1945-05-13"), // 1
("4", "2", "1945-11-04"), // 2
("3", "2", "1946-05-12"), // 3 second 3 consecutives win
("4", "2", "1946-11-17"),
("2", "2", "1946-11-24"),
("1", "5", "1946-12-01"),
("1", "0", "1947-05-18"),
("3", "0", "1947-10-05"),
("2", "3", "1948-11-07"),
("0", "1", "1948-11-14"),
("1", "4", "1948-11-21"),
("3", "1", "1949-06-12"), // 1
("4", "0", "1949-06-19"), // 2
("5", "1", "1949-07-24"), // 3
("3", "1", "1949-08-06") // 4 first consecutives win
I need a result like this
From To games_won
1949-06-12 1949-08-06 4
1945-11-04 1946-11-17 3
1943-10-03 1943-10-10 2
...
...
You can count the number of wins and the number of rows using variables. The difference is constant for consecutive wins. This gives you a value for aggregation to get the final result:
select min(played), max(played), count(*)
from (select g.*,
(@rn := @rn + 1) as rn,
(@rnw := if(value1 > value2, @rnw + 1, @rnw) as rnw
from games g cross join
(select @rn := 0, @rnw := 0) params
order by played
) g
where value1 > value2
group by (rn - rnw);
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.