由于ClickHouse不支持查询的WITH子句,因此在实现用于计算MAU的查询时遇到了麻烦(每月活跃用户每天都在计算,而30天范围也在每天移动)。 在Postgres中,我会这样做:
$("#submitboeking").click(function(event) {
event.preventDefault();
//Cursisten
var voornamen = [];
var achternamen = [];
var geslachten = [];
var geboortedata = [];
$("[id^='txtCursistVoornaam']").each(function() {
voornamen.push($(this).val());
});
$("[id^='txtCursistAchternaam']").each(function() {
achternamen.push($(this).val());
});
$("[id^='radCursistGeslacht']:checked").each(function() {
geslachten.push($(this).val());
});
$("[id^='txtCursistGeboortedatum']").each(function() {
geboortedata.push($(this).val());
});
$.post('/wp-content/themes/tweb/processboeking.php',
{
'geslachten': geslachten,
'voornamen': voornamen,
'achternamen': achternamen,
'geboortedata': geboortedata,
})
.done(function(data)
{
// console.log(data)
$('#overzicht').html(data);
}).fail(function(data) {
alert(response.responseText);
});
var li_count = $('.nav-tabs li').length;
var current_active = $('.nav-tabs li.active').index();
if (current_active < li_count) {
$('.nav-tabs li.active').next('li').find('a').attr('data-toggle', 'tab').tab('show');
var txt = $(".oplselect option:selected").text();
var val = $(".oplselect option:selected").val();
$('.showoplnaam').html('Uw selectie: ' + txt);
}
});
所需结果看起来像这样
WITH days AS (
SELECT created_at::DATE AS day,
FROM events
WHERE created_at > '2019-04-01'
GROUP BY 1
)
SELECT day,
(SELECT count(distinct user_id)
FROM events
WHERE events.created_at::DATE BETWEEN days.day-29 AND days.day
AND created_at > '2019-04-01'
) AS mau
FROM days
答案 0 :(得分:1)
您可以尝试这样的事情:
SELECT
created_at,
uniqExact(user_id) AS mau
FROM
(
SELECT
created_at + n AS created_at,
user_id
FROM
(
SELECT
today() - 14 AS created_at,
123 AS user_id
UNION ALL
SELECT
today() - 20 AS created_at,
456 AS user_id
)
ARRAY JOIN range(30) AS n
)
WHERE created_at <= today()
GROUP BY created_at
FORMAT TSV
2019-04-11 1
2019-04-12 1
2019-04-13 1
2019-04-14 1
2019-04-15 1
2019-04-16 1
2019-04-17 2
2019-04-18 2
2019-04-19 2
2019-04-20 2
2019-04-21 2
2019-04-22 2
2019-04-23 2
2019-04-24 2
2019-04-25 2
2019-04-26 2
2019-04-27 2
2019-04-28 2
2019-04-29 2
2019-04-30 2
2019-05-01 2
21 rows in set. Elapsed: 0.005 sec.
答案 1 :(得分:0)
您始终可以内联CTE,例如
SELECT
d.day,
COUNT(DISTINCT e.user_id) AS mau
FROM
(
SELECT created_at::DATE AS day,
FROM events
WHERE created_at > '2019-04-01'
GROUP BY 1
) d
LEFT JOIN events e
ON e.created_at::DATE BETWEEN d.day-29 AND d.day
GROUP BY
d.day;
以上已将CTE替换为子查询别名d
。我继续将您的查询重构为使用左联接,替换了您已有的相关子查询。
答案 2 :(得分:0)
您还可以创建视图或临时表。