I can't resolve this problem
I have 2 tables and I want to count highest version per country for all users
Example:
John is from the USA, and he has versions 1,2,3,4,5 and 6
Bob is from France, and he has versions 1,2 and 3
Mary is from France too, and she has versions 1,2 and 3
I want to count just the last versions from anny of them
Becouse :
John has higher version 6 he won't be counted in versions lower than 6
Bob has higher version 3 he won't be counted in versions lower than 3
Mary has higher version 3 she won't be counted in versions lower than 3
So the final result will be as the Desired result set at bottom
Table1
ID | USER | COUNTRY
---- ----- ----------
1 | John | USA
2 | Bob | FRANCE
3 | Mary | FRANCE
Table2
ID | USER | VERSION
---- ------- ---------
1 | John | 1
2 | Bob | 1
3 | John | 2
4 | Mary | 1
5 | Mary | 2
6 | John | 3
7 | John | 4
8 | Bob | 2
9 | John | 5
10 | Bob | 3
11 | Mary | 3
12 | John | 6
Desired result set:
Country | V1| V2 | V3 | V4 | V5 | V6
-------- ---- --- ---- ---- ---- ----
USA | 0 | 0 | 0 | 0 | 0 | 1
FRANCE | 0 | 0 | 2 | 0 | 0 | 0
I made some try without luck
SELECT`Table1`.`Country` AS `C`,
count(max( `Table2`.`Version` )) AS `V`
FROM
( `Table1` JOIN `Table2` ON ( ( `Table1`.`USER` = `Table2`.`USER` ) ) )
GROUP BY
`Table1`.`COUNTRY`
OR SOMETHING LIKE THIS
SELECT
`Table1`.`COUNTRY` AS `COUNTRY`,
count(
DISTINCT
IF
(
(
NOT (
`Table2`.`VERSION` IN (
SELECT
`Table2`.`VERSION`
FROM
( `Table1` JOIN `Table2` ON ( ( `Table1`.`USER` = `Table2`.`USER` ) ) )
WHERE
( `Table2`.`VERSION` < 2 )
)
)
),
`Table2`.`ID`,
NULL
)
) AS `V2`
FROM
( `Table1` JOIN `Table2` ON ( ( `Table1.`USER` = `Table2`.`USER` ) ) )
GROUP BY
`Table1`.`COUNTRY`
Please help me , thanks