select
ad.*,
ab.balance
from
(
select
account_id, max(date) as max_date
from tmp_account_balance
group by account_id
) ad
inner join tmp_account_balance ab on ad.account_id = ab.account_id and ab.date = ad.max_date;
select
ct.id,
count(1) as allnews_
from dle_category ct
left join dle_post ps on ps.id = ct.id
where true
and ps.approve = '1'
group by ct.id
html_template = """
<html>
<head>
<title>Отчет номер: {current_date}</title>
</head>
<body>
<table border="1">
<tr>
<td>Имя</td>
<td>Результат</td>
</tr>
<tr>
<td>{arg_w_name}</td>
<td>{arg_w_res}</td>
</tr>
</table>
</body>
</html>
"""
print html_template.format(current_date="2015-03-31", arg_w_name="Vasiliy", arg_w_res="...")