我正在寻找解决我的问题的方法,但遗憾的是我无法找到它。所以也许有人可以在这里提供帮助。
我有一个当前查询,它将根据特定用户汇总标识符的总频率。这是查询
SELECT trk_sum.user_id,trk_sum.trk_data_identifier, trk_sum.trk_data_type, trk_sum.trk_date,MAX(sum_data_frequency)
FROM (
SELECT
user_id,trk_data_identifier, trk_data_type, trk_date,sum(trk_data_frequency) as sum_data_frequency
FROM user_tracker_data
WHERE user_id = '37557'
GROUP BY CONCAT(trk_data_type , trk_data_identifier)
ORDER BY trk_date DESC ,trk_data_type ASC , sum_data_frequency DESC
) trk_sum
GROUP BY trk_sum.trk_data_type
ORDER BY trk_sum.trk_date DESC ,trk_sum.trk_data_type ASC
它产生这样的东西:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>user_tracker_data</title>
<meta name="GENERATOR" content="HeidiSQL 9.3.0.4984">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
thead tr {background-color: ActiveCaption; color: CaptionText;}
th, td {vertical-align: top; font-family: "Tahoma", Arial, Helvetica, sans-serif; font-size: 8pt; padding: 3px; }
table, td {border: 1px solid silver;}
table {border-collapse: collapse;}
thead .col0 {width: 65px;}
thead .col1 {width: 117px;}
thead .col2 {width: 109px;}
thead .col3 {width: 91px;}
thead .col4 {width: 159px;}
.col4 {text-align: right;}
</style>
</head>
<body>
<table caption="user_tracker_data (10 rows)">
<thead>
<tr>
<th class="col0">user_id</th>
<th class="col1">trk_data_identifier</th>
<th class="col2">trk_data_type</th>
<th class="col3">trk_date</th>
<th class="col4">MAX(sum_data_frequency)</th>
</tr>
</thead>
<tbody>
<tr>
<td class="col0">37557</td>
<td class="col1">ACCO 1850D</td>
<td class="col2">advert_model</td>
<td class="col3">2015-10-28</td>
<td class="col4">12</td>
</tr>
<tr>
<td class="col0">37557</td>
<td class="col1">741</td>
<td class="col2">category_id</td>
<td class="col3">2015-10-28</td>
<td class="col4">13</td>
</tr>
<tr>
<td class="col0">37557</td>
<td class="col1">9</td>
<td class="col2">industry_id</td>
<td class="col3">2015-10-28</td>
<td class="col4">22</td>
</tr>
<tr>
<td class="col0">37557</td>
<td class="col1">7081</td>
<td class="col2">make_id</td>
<td class="col3">2015-10-28</td>
<td class="col4">12</td>
</tr>
<tr>
<td class="col0">37557</td>
<td class="col1">44759</td>
<td class="col2">model_id</td>
<td class="col3">2015-10-28</td>
<td class="col4">12</td>
</tr>
<tr>
<td class="col0">37557</td>
<td class="col1">4305</td>
<td class="col2">post_code</td>
<td class="col3">2015-10-28</td>
<td class="col4">12</td>
</tr>
<tr>
<td class="col0">37557</td>
<td class="col1">QLD</td>
<td class="col2">state</td>
<td class="col3">2015-10-28</td>
<td class="col4">23</td>
</tr>
<tr>
<td class="col0">37557</td>
<td class="col1">1093</td>
<td class="col2">subcategory_id</td>
<td class="col3">2015-10-28</td>
<td class="col4">13</td>
</tr>
<tr>
<td class="col0">37557</td>
<td class="col1">Australia</td>
<td class="col2">country</td>
<td class="col3">2015-09-27</td>
<td class="col4">61</td>
</tr>
<tr>
<td class="col0">37557</td>
<td class="col1">test</td>
<td class="col2">keywords</td>
<td class="col3">2015-06-27</td>
<td class="col4">3</td>
</tr>
<tr>
<td class="col0">37557</td>
<td class="col1">ACCO 1850D</td>
<td class="col2">advert_model</td>
<td class="col3">2015-01-28</td>
<td class="col4">12</td>
</tr>
</tbody>
</table>
<p>
<em>generated 2015-10-29 08:27:35 by <a href="http://www.heidisql.com/">HeidiSQL 9.3.0.4984</a></em>
</p>
</body>
</html>
&#13;
我想做的最后一件事就是获得最新的15个独特的trk_date。请注意,我不希望它像过去15天一样#34;但它应该像包含多行的15个先前日期点。正如您所看到的,记录底部的trk_date存在差距
答案 0 :(得分:0)
您可以JOIN
原始查询到包含数据集中出现的最近15个日期的临时表。连接操作充当过滤器,为您提供所需的结果。
SELECT trk_sum.user_id,trk_sum.trk_data_identifier, trk_sum.trk_data_type, trk_sum.trk_date,MAX(sum_data_frequency)
FROM (
SELECT
user_id,trk_data_identifier, trk_data_type, trk_date,sum(trk_data_frequency) as sum_data_frequency
FROM user_tracker_data
WHERE user_id = '37557'
GROUP BY CONCAT(trk_data_type , trk_data_identifier)
ORDER BY trk_date DESC ,trk_data_type ASC , sum_data_frequency DESC
) trk_sum
INNER JOIN
(
SELECT DISTINCT trk_date FROM user_tracker_data
ORDER BY trk_date DESC
LIMIT 15
) t
ON trk_sum.trk_date = t.trk_date
GROUP BY trk_sum.trk_data_type
ORDER BY trk_sum.trk_date DESC ,trk_sum.trk_data_type ASC