MySQL SELECT to Rank A Number out of a set of numbers - mysql

I have rows of data from a SELECT query with a few prices (say three for this example). One is our price, one is competitor1 price, one is competitor2 price. I want to add a column that spits out the rank of our price as compared to the other two prices; if our price is the lowest it would spit out the number 1 if the highest it would spit out the number it is out of.
Something like this:
Make | Model | OurPrice | Comp1Price | Comp2Price | Rank | OutOf
MFG1 MODEL1 350 100 500 2 3
MFG1 MODEL2 50 100 100 1 3
MFG2 MODEL1 100 NULL 50 2 2
MFG2 MODEL2 9999 500 NULL 2 2
-Sometimes the competitor price will be NULL as seen above, and I believe this is where my issue lies. I have tried a CASE and it works when only on one competitor but when I add a AND statement it spits out the ranks as all NULL. Is there a better way of doing this through a MySQL query?
SELECT
MT.MAKE as Make,
MT.MODEL as Model,
MT.PRICE as OurPrice,
CT1.PRICE as Comp1Price,
CT2.PRICE as Comp2Price,
CASE
WHEN MT.PRICE < CT1.PRICE AND MT.PRICE < CT2.PRICE
THEN 1 END AS Rank
(CT1.PRICE IS NOT NULL) + (CT2.PRICE IS NOT NULL) + 1 as OutOf
FROM mytable MT
LEFT JOIN competitor1table as CT1 ON CT1.MODEL = MT.MODEL
LEFT JOIN competitor2table as CT2 ON CT2.MODEL = MT.MODEL
ORDER BY CLASS

Not tested, but you can try:
SELECT
a.MAKE AS Make,
a.MODEL AS Model,
a.PRICE AS OurPrice
MAX(CASE WHEN a.compnum = 1 THEN pricelist END) AS Comp1Price,
MAX(CASE WHEN a.compnum = 2 THEN pricelist END) AS Comp2Price,
FIND_IN_SET(a.PRICE, GROUP_CONCAT(a.pricelist ORDER BY a.pricelist)) AS Rank,
COUNT(a.pricelist) AS OutOf
FROM
(
SELECT MAKE, MODEL, PRICE, PRICE AS pricelist, 0 AS compnum
FROM mytable
UNION ALL
SELECT a.MAKE, a.MODEL, a.PRICE, CT1.PRICE, 1
FROM mytable a
LEFT JOIN competitor1table CT1 ON a.MODEL = CT1.MODEL
UNION ALL
SELECT a.MAKE, a.MODEL, a.PRICE, CT2.PRICE, 2
FROM mytable a
LEFT JOIN competitor2table CT2 ON a.MODEL = CT2.MODEL
) a
GROUP BY
a.MAKE, a.MODEL

(CT1.PRICE IS NOT NULL AND CT1.PRICE < MT.PRICE) + (CT2.PRICE IS NOT NULL AND CT2.PRICE < MT.PRICE) + 1 as Rank

Related

Getting percentage of total in SQL with two joins

So I'm trying to do something that I think should be fairly simple with SQL. But I'm having a hard time figuring it out. Here is the format of my data:
One table with user information, let's call it User:
ID name_user Drive_Type
1 Tim Stick shift
2 Jim Automatic
3 Bob Automatic
4 Lisa Stick shift
Then I have one table used for the join, let's call it Join_bridge:
user_ID car_has_ID
1 12
2 13
3 14
4 14
And one table with car information, let's call it Car:
car_ID name
12 Honda
13 Toyota
14 Ford
Then what I want is something that looks like this with the total number of Ford's that are stick shift and the percentage
name Total percentage
Ford 1 25%
I have tried the following, which gets the total right, but not the percentage:
select Drive_Type,
name,
count(Drive_Type) as Total,
(count(Drive_Type) / (select count(*)
from User
join Join_bridge
on User.ID = user_ID
join Car
on Car.car_ID = Join_bridge.car_has_ID
) * 100.0 as Percent
from User
join Join_bridge
on User.ID = Join_bridge.user_ID
join Car
on Car.car_ID = Join_bridge.car_has_ID
where name = 'Ford' and Drive_Type = "Automatic"
;
What am I missing? Thanks.
See this SQL Fiddle with the query - the trick is to SUM over CASE that returns 1 for rows you look for and 0 for the rest in order to calculate "Total" at the same time you can also count all rows to calculate percentage.
Here's the SQL query:
SELECT
'Ford' name,
SUM(a.ford_with_stack_flag) Total,
100.0 * SUM(a.ford_with_stack_flag) / COUNT(*) percentage
FROM (
SELECT
Car.name,
(CASE WHEN User.Drive_Type = 'Stick Shift' and Car.name = 'Ford' THEN 1 ELSE 0 END) ford_with_stack_flag
FROM User
JOIN Join_bridge on User.ID = Join_bridge.user_ID
JOIN Car ON Car.car_ID = Join_bridge.car_has_ID
) a
Compute percent and join to Car. Window functions are supported in MySql 8.0
select c.car_ID, c.name, p.cnt, p.Percent
from car c
join (
select car_has_ID, u.Drive_Type,
count(*) cnt,
count(*) / count(count(*)) over() Percent
from Join_bridge b
join user u on u.ID = b.user_ID
group by b.car_has_ID, u.Drive_Type
) p on p.car_has_ID = c.car_ID
where c.name = 'Ford' and p.Drive_Type='Stick shift';
db<>fiddle

Group_Concat with multiple joined tables

I have two main tables that comprise bookings for events.
A Registrants table (Bookings) R and an Events table E.
There are also two connected tables, Field_Values V and Event_Categories C
This diagram shows the relationship
What I am trying to do is create an Invoice query that mirrors the user's shopping cart. Often a user will book multiple events in one transaction, so my invoice should have columns for the common items e.g. User Name, User Email, Booking Date, Transaction ID and aggregated columns for the invoice line item values e.g. Quantity "1,2" Description "Desc1, Desc2" Price "10.00, 20.00" where there are two line items in the shopping cart.
The Transaction ID (dcea4_eb_registrant.transaction_id) is unique per Invoice and repeated per line item in that sale.
I have the following query which produces rows for each line item
SELECT
R.id as ID,
E.event_date as ServiceDate,
E.event_date - INTERVAL 1 DAY as DueDate,
Concat('Ad-Hoc Booking:',E.title) as ItemProductService,
Concat(R.first_name, ' ',R.last_name) as Customer,
R.first_name as FirstName,
R.last_name as LastName,
R.email,
R.register_date as InvoiceDate,
R.amount as ItemAmount,
R.comment,
R.number_registrants as ItemQuantity,
R.transaction_id as InvoiceNo,
R.published as Status,
E.event_date AS SERVICEDATE,
Concat('Ad-Hoc Booking:',E.title) AS DESCRIPTION,
R.number_registrants AS QUANTITY,
FORMAT(R.amount / R.number_registrants,2) AS RATE,
R.amount AS AMOUNT,
C.category_id as CLASS,
Concat(Group_Concat(V.field_value SEPARATOR ', '),'. ',R.comment) as Memo
FROM dcea4_eb_events E
LEFT JOIN dcea4_eb_registrants R ON R.event_id = E.id
LEFT JOIN dcea4_eb_field_values V ON V.registrant_id = R.id
LEFT JOIN dcea4_eb_event_categories C ON C.event_id = R.event_id
WHERE 1=1
AND V.field_id IN(14,26,27,15)
AND R.published <> 2 /*Including this line omits Cancelled Invoices */
AND R.published IS NOT NULL
AND (R.published = 1 OR R.payment_method = "os_offline")
AND (R.register_date >= CURDATE() - INTERVAL 14 DAY)
GROUP BY E.event_date, E.title, R.id, R.first_name, R.last_name, R.email,R.register_date, R.amount, R.comment
ORDER BY R.register_date DESC, R.transaction_id
This produces output like this
I'm using the following query to try to group together the rows with a common transaction_ID (rows two and three in the last picture) - I add group_concat on the columns I want to aggregate and change the Group By to be the transaction_id
SELECT
R.id as ID,
E.event_date as ServiceDate,
E.event_date - INTERVAL 1 DAY as DueDate,
Concat('Ad-Hoc Booking:',E.title) as ItemProductService,
Concat(R.first_name, ' ',R.last_name) as Customer,
R.first_name as FirstName,
R.last_name as LastName,
R.email,
R.register_date as InvoiceDate,
R.amount as ItemAmount,
R.comment,
R.number_registrants as ItemQuantity,
R.transaction_id as InvoiceNo,
R.published as Status,
Group_ConCat( E.event_date) AS SERVICEDATE,
Group_ConCat( Concat('Ad-Hoc Booking:',E.title)) AS DESCRIPTION,
Group_ConCat( R.number_registrants) AS QUANTITY,
Group_ConCat( FORMAT(R.amount / R.number_registrants,2)) AS RATE2,
Group_ConCat( R.amount) AS AMOUNT,
Group_ConCat( C.category_id) as CLASS,
Concat(Group_Concat(V.field_value SEPARATOR ', '),'. ',R.comment) as Memo
FROM dcea4_eb_events E
LEFT JOIN dcea4_eb_registrants R ON R.event_id = E.id
LEFT JOIN dcea4_eb_field_values V ON V.registrant_id = R.id
LEFT JOIN dcea4_eb_event_categories C ON C.event_id = R.event_id
WHERE 1=1
AND V.field_id IN(14,26,27,15)
AND R.published <> 2 /*Including this line omits Cancelled Invoices */
AND R.published IS NOT NULL
AND (R.published = 1 OR R.payment_method = "os_offline")
AND (R.register_date >= CURDATE() - INTERVAL 14 DAY)
GROUP BY R.transaction_id
ORDER BY R.register_date DESC, R.transaction_id
But this produces this output
It seems to be multiplying the rows. The Quantity column in the first row should just be 1 and in the second row it should be 2,1 .
I've tried using Group_Concat with DISTINCT but this doesn't work because often the values being concatenated are the same (e.g. the price for two events being booked are both the same) and the query only returns one value e.g. 10 and not 10, 10. The latter being what I need.
I'm guessing the issue is around the way the tables are joined but I'm struggling to work out how to get what I need.
Pointers in the right direction most appreciated.
You seem determined to go in what seems to me to be the wrong direction, so here's a gentle nudge down that hill...
Consider the following...
CREATE TABLE users
(user_id SERIAL PRIMARY KEY
,username VARCHAR(12) UNIQUE
);
INSERT INTO users VALUES
(101,'John'),(102,'Paul'),(103,'George'),(104,'Ringo');
DROP TABLE IF EXISTS sales;
CREATE TABLE sales
(sale_id SERIAL PRIMARY KEY
,purchaser_id INT NOT NULL
,item_code CHAR(1) NOT NULL
,quantity INT NOT NULL
);
INSERT INTO sales VALUES
( 1,101,'A',1),
( 2,103,'A',2),
( 3,103,'A',3),
( 4,104,'A',1),
( 5,104,'A',2),
( 6,104,'A',3),
( 7,103,'B',2),
( 8,103,'B',2),
( 9,104,'B',3),
(10,103,'B',2),
(11,104,'B',2),
(12,104,'B',1);
SELECT u.*
, x.sale_ids
, x.item_codes
, x.quantities
FROM users u
LEFT
JOIN
( SELECT purchaser_id
, GROUP_CONCAT(sale_id ORDER BY sale_id) sale_ids
, GROUP_CONCAT(item_code ORDER BY sale_id) item_codes
, GROUP_CONCAT(quantity ORDER BY sale_id) quantities
FROM sales
GROUP
BY purchaser_id
) x
ON x.purchaser_id = u.user_id;
+---------+----------+---------------+-------------+-------------+
| user_id | username | sale_ids | item_codes | quantities |
+---------+----------+---------------+-------------+-------------+
| 101 | John | 1 | A | 1 |
| 102 | Paul | NULL | NULL | NULL |
| 103 | George | 2,3,7,8,10 | A,A,B,B,B | 2,3,2,2,2 |
| 104 | Ringo | 4,5,6,9,11,12 | A,A,A,B,B,B | 1,2,3,3,2,1 |
+---------+----------+---------------+-------------+-------------+

SQL - Select distinct column without excluding rows

I'm running a query that aggregates sales information by either category or subcategory within some date range. I was asked to add budget information to it for a report that displays the information fetched by this query.
SELECT
DATE_FORMAT(B.TxnDate, "%Y-%m") AS FormattedTxnDate,
SUM(B.Quantity) AS QuantitySum,
SUM(B.Quantity * B.Amount) AS Revenue,
SUM(CASE WHEN B.AverageCost = 0 THEN (B.Quantity * C.PurchaseCost) ELSE (B.Quantity *
B.AverageCost) END) AS COGS,
A.CustomerRefFullName,
SUM(D.Budget_2018) AS Budget,
D.Brand, D.Category, D.Subcategory, D.ProductManager, C.VendorRefFullName
FROM
qb_invoice_info A, qb_invoice_line_info B, qb_item_info C, qb_item_group D
WHERE
A.TxnID = B.TxnID
AND
B.Item_ListID = C.ListID
AND
C.Parent_ListID = D.ListID
AND
(C.Type = "Inventory" OR C.Type = "InventoryAssembly")
AND
B.TxnDate BETWEEN ? AND ?
GROUP
BY D.Category, YEAR(B.TxnDate), QUARTER(B.TxnDate)
ORDER
BY D.Category ASC, YEAR(B.TxnDate) ASC, QUARTER(B.TxnDate) ASC
Every subcategory has its own budget amount. The problem is that some subcategories share all of the same information except for their unique IDs. A few records might look like this within the qb_item_group table.
qb_item_group
id | Category | Subcategory | Budget
------------------------------------
1A | Lights | DMX | 4000
1B | Lights | DMX | 4000
1C | Lights | DMX | 4000
2A | Lights | Flash | 5000
3A | Lights | Bulbs | 1000
In this case, the total budget for lights would be 10,000 because we ignore two of the DMX budgets. I tried SUM(DISTINCT D.Budget_2018 AS Budget earlier today but it failed as I expected because it's only adding unique budget values. How can I adapt the query I have above so that I can retrieve all sales records by either category or subcategory but still get a total budget that is the sum of all unique subcategories under the parent category?
SELECT
DATE_FORMAT(B.TxnDate, "%Y-%m") AS FormattedTxnDate,
SUM(B.Quantity) AS QuantitySum,
SUM(B.Quantity * B.Amount) AS Revenue,
SUM(
CASE
WHEN B.AverageCost = 0 THEN (B.Quantity * C.PurchaseCost)
ELSE (B.Quantity * B.AverageCost)
END
) AS COGS,
A.CustomerRefFullName,
COALESCE(category_budgets.budget, 0) AS budget,
D.Brand,
D.Category,
D.Subcategory,
D.ProductManager,
C.VendorRefFullName
FROM
qb_invoice_info A,
qb_invoice_line_info B,
qb_item_info C,
qb_item_group D
LEFT JOIN
(
SELECT
a.category,
SUM(a.budget) as budget
FROM
(
SELECT DISTINCT
category
budget
FROM
budgets
) a
) category_budgets
ON
category_budgets.category = D.category
WHERE
A.TxnID = B.TxnID
AND
B.Item_ListID = C.ListID
AND
C.Parent_ListID = D.ListID
AND
(C.Type = "Inventory" OR C.Type = "InventoryAssembly")
AND
B.TxnDate BETWEEN ? AND ?
GROUP BY
D.Category, YEAR(B.TxnDate), QUARTER(B.TxnDate)
ORDER BY
D.Category ASC, YEAR(B.TxnDate) ASC, QUARTER(B.TxnDate) ASC
;
You can left join with the sum of distinct categories and budgets. This will give you all of your output rows desired but will also give you $0 budgets for categories that don't have entries in the budgets table. Good luck!

SQL query unique values based on 2 colums

I need help figuring this out.
At the moment I have this query:
SELECT Table1.Column1 AS Company
,SUM(DATEDIFF(Day, Table2.Column2, Table1.Column3)) AS DiffDate
FROM Table1
INNER JOIN Table2 ON Table1.Column6 = Table2.Column6
AND Table1.Column5 = Table2.Column5
GROUP BY Company
The result of this is...
Company DiffDate
company1 8
But this is not what I want because there are values that are the combinations of column5 and column6 that are the same. See below.
Company | Column6 | Column5 | DiffDate
Company1 | 5782 | 10 | 2
Company1 | 5782 | 10 | 2
Company1 | 5782 | 20 | 2
Company1 | 5782 | 30 | 2
So the result I'm after is 6, not 8.
I tried using SELECT DISTINCT but that doesn't do anything.
Thank you in advance.
edit 2016-02-09
I've made the following query in SSMS.
;WITH cte AS (SELECT DISTINCT bestlevdat, bestradnr, bestnr FROM dbo.bpa)
SELECT dbo.bp.ftgnr AS Företagsnr,SUM(DATEDIFF(Day,cte.bestlevdat, dbo.bp.bestberlevdat)) AS Diff_Bekräftat_dat,
SUM(DATEDIFF(Day,cte.bestlevdat, dbo.bp.bestbeglevdat)) AS Diff_Önskat_dat, COUNT(dbo.bp.bestradnr) AS AntalRader, SUM(CASE WHEN datediff(day, cte.bestlevdat, dbo.bp.bestberlevdat) < - 0 THEN 1 ELSE 0 END) AS Antal_avvikande_rader, ROUND((COUNT(dbo.bp.bestradnr) - SUM(CASE WHEN datediff(day, cte.bestlevdat, dbo.bp.bestberlevdat) < - 0 THEN 1.0 ELSE 0.0 END)) * 100 / COUNT(dbo.bp.bestradnr), 1) AS Levsäk
FROM dbo.bp INNER JOIN
cte ON dbo.bp.bestnr = cte.bestnr AND dbo.bp.bestradnr = cte.bestradnr
WHERE (YEAR(dbo.bp.bestberlevdat) = '2015')
GROUP BY dbo.bp.ftgnr
ORDER BY AntalRader DESC
This works a charm. But when I create a new view and paste this query there and try to save I get an error on the ";" again. If I remove ";" in SSMS I get error "The multi-part identifier "cte.bestlevdat" could not be bound" and also converts my query to the following:
WITH cte AS (SELECT DISTINCT bestlevdat, bestradnr, bestnr
FROM dbo.bpa AS bpa_1)
SELECT TOP (100) PERCENT dbo.bp.ftgnr AS Företagsnr, SUM(DATEDIFF(Day, cte_1.bestlevdat, dbo.bp.bestberlevdat)) AS Diff_Bekräftat_dat, SUM(DATEDIFF(Day,
cte_1.bestlevdat, dbo.bp.bestbeglevdat)) AS Diff_Önskat_dat, COUNT(dbo.bp.bestradnr) AS AntalRader, SUM(CASE WHEN datediff(day, cte.bestlevdat,
dbo.bp.bestberlevdat) < - 0 THEN 1 ELSE 0 END) AS Antal_avvikande_rader, ROUND((COUNT(dbo.bp.bestradnr) - SUM(CASE WHEN datediff(day, cte.bestlevdat,
dbo.bp.bestberlevdat) < - 0 THEN 1.0 ELSE 0.0 END)) * 100 / COUNT(dbo.bp.bestradnr), 1) AS Levsäk
FROM dbo.bp INNER JOIN
cte AS cte_1 ON dbo.bp.bestnr = cte_1.bestnr AND dbo.bp.bestradnr = cte_1.bestradnr
WHERE (YEAR(dbo.bp.bestberlevdat) = '2015')
GROUP BY dbo.bp.ftgnr
ORDER BY AntalRader DESC
Sorry if I've missed something obvious, but I'm pretty new with querys and this is my first attempt on "View".
Sounds like you want to do something like you want the distinct rows from Table2, and to join/aggregate them from Table1, so using a CTE may help, so something like:
;WITH cte AS (
SELECT DISTINCT
Column2,
Column5,
Column6
FROM Table2
)
SELECT
Table1.Column1 AS Company,
SUM(DATEDIFF(Day, cte.Column2, Table1.Column3)) AS DiffDate
FROM Table1
INNER JOIN cte
ON Table1.Column6 = cte.Column6
AND Table1.Column5 = cte.Column5
GROUP BY Company

MySQL - Complex COUNT Query

I have a table called user_scores as below:
id | af_id | uid | level | record_date
----------------------------------------
1 | 1.1 | 1 | 3 | 2012-01-01
2 | 1.1 | 1 | 4 | 2012-02-01
3 | 1.2 | 1 | 3 | 2012-01-01
4 | 1.2 | 1 | 5 | 2012-03-01
...
I have another table call user_info as below:
uid | forename | surname | gender
-----------------------------------
1 | Homer | Simpson | M
2 | Marge | Simpson | F
3 | Bart | Simpson | M
4 | Lisa | Simpson | F
...
In user scores uid is the user id of a registered user on the system, af_id identifies a particular test a user submits. A user scores a level between 1 - 5 for each test, which can be submitted every month.
My problem is I need to produce an analysis at the end of the year to COUNT the number of users that have achieved each level for a particular test. The analysis is to show a gender split for male and female.
So for example an administrator would select test 1.1 and the system would generate stats based that would COUNT of the total MAX level achieved by each user in the year, with a gender split.
Any help is much appreciated. Thank you in advance.
-
I think I need to clarify myself a bit. Because a user can complete the test multiple times throughout the year, there will be multiple scores for the same test. The query should take the highest level achieved and include this in the count. An example result would be:
Male Results:
level1 | level2 | level3 | level4 | level5
------------------------------------------
2 | 5 | 10 | 8 | 1
I am not certain I get exactly what you mean, but as always I'll have a go. As I understand it you want to know how many people from each gender reached each level in a certain year.
SELECT MaxLevel,
COUNT(CASE WHEN ui.Gender = 'M' THEN 1 END) AS Males,
COUNT(CASE WHEN ui.Gender = 'F' THEN 1 END) AS Females
FROM User_Info ui
INNER JOIN
( SELECT MAX(Level) AS MaxLevel,
UID
FROM User_Scores us
WHERE af_ID = '1.1'
AND YEAR(Record_Date) = 2012
GROUP BY UID
) AS MaxUs
ON MaxUs.uid = ui.UID
GROUP BY MaxLevel
I've put some sample data on SQL Fiddle so you see if it is what you were after.
EDIT
To transpose the data so levels are along the top and Gender in the rows the following will work:
SELECT Gender,
COUNT(CASE WHEN MaxLevel = 1 THEN 1 END) AS Level1,
COUNT(CASE WHEN MaxLevel = 2 THEN 1 END) AS Level2,
COUNT(CASE WHEN MaxLevel = 3 THEN 1 END) AS Level3,
COUNT(CASE WHEN MaxLevel = 4 THEN 1 END) AS Level4,
COUNT(CASE WHEN MaxLevel = 5 THEN 1 END) AS Level5
FROM User_Info ui
INNER JOIN
( SELECT MAX(Level) AS MaxLevel,
UID
FROM User_Scores us
WHERE af_ID = '1.1'
AND YEAR(Record_Date) = 2012
GROUP BY UID
) AS MaxUs
ON MaxUs.uid = ui.UID
GROUP BY Gender
Note, that if there are ever more than 5 levels you will need to add more to the select statement, or start building dynamic SQL.
Assuming record_date holds only dates (without time parts):
SELECT
s.maxlevel,
COUNT(NULLIF(gender, 'F')) AS M,
COUNT(NULLIF(gender, 'M')) AS F
FROM user_info u
INNER JOIN (
SELECT
uid,
MAX(level) AS maxlevel
FROM user_scores
WHERE record_date > DATE_SUB(CURDATE(), INTERVAL DAYOFYEAR(CURDATE()) DAY)
AND af_id = '1.1'
GROUP BY
uid
) s ON s.uid = u.uid
GROUP BY
s.maxlevel
That will show you only the maximum levels found in the user_scores table. If you have a Levels table where all possible levels (1 to 5) are listed, you could use that table to get a complete list of levels. If some levels are not present in the requested subset of data, the corresponding rows will show 0s in both columns.
Here's the above script with minor changes to show the complete chart of levels:
SELECT
l.level AS maxlevel,
COUNT(NULLIF(gender, 'F')) AS M,
COUNT(NULLIF(gender, 'M')) AS F
FROM user_info u
INNER JOIN (
SELECT
uid, MAX(level) AS maxlevel
FROM user_scores
WHERE record_date > DATE_SUB(CURDATE(), INTERVAL DAYOFYEAR(CURDATE()) DAY)
AND af_id = '1.1'
GROUP BY
uid
) s ON s.uid = u.uid
RIGHT JOIN Levels l ON s.maxlevel = l.level
GROUP BY
l.level
Hope this is what your looking for!
Show number of records group by userid and gender of the max score for af_id '1.1'.
select count(*), info.uid, info.gender, max(score.level)
from user_info as info
join user_scores as score
on info.uid = score.uid
where score.af_id = '1.1'
group by info.uid, info.gender;
EDITED based on your edit.
select sum(if(a.gender="M",1,0)) Male_users, sum(if(a.gender="F",1,0)) Female_users
from myTable a where
a.level = (select max(b.level) from myTable b where a.uid=b.uid)
group by af_id.
I typed this in a rush. But it should work or at least get you where you need to go. E.G. if you need to specify time frame, add that.
You need something like
SELECT
uid,
MAX(level)
WHERE
record_date BETWEEN '2012-01-01' AND '2012-12-31'
AND af_id='1.1'
GROUP BY uid
If you need the gender splits then depending on what stat you need per gender you can either add a JOIN on the user_info table into this query (to get the MAX per gender) to wrap this as a sub-query and JOIN on the whole thing.