如何选择不同日子的兑换价值? [关闭]
Posted
技术标签:
【中文标题】如何选择不同日子的兑换价值? [关闭]【英文标题】:How to select the exchange values in different days? [closed] 【发布时间】:2020-02-04 09:00:52 【问题描述】:我正在使用 ClickHouse,我想知道这个“搜索词”中的哪个在不同的日子里是不同的路径值
我的数据
date searchword path
1 2019-10-02 word1 url-1
2 2019-10-03 word1 url-2
3 2019-10-04 word1 url-1
4 2019-10-11 word1 url-1
5 2019-10-07 word2 url-4
6 2019-10-08 word3 url-5
7 2019-10-09 word3 url-5
8 2019-10-10 word3 url-5
9 2019-10-08 word4 url-5
10 2019-10-09 word4 url-5
11 2019-10-10 word4 url-5
12 2019-10-10 word4 url-6
13 2019-10-11 word4 url-5
14 2019-10-10 word5 url-7
15 2019-10-09 word5 url-8
16 2019-10-11 word5 url-7
想要的结果
word1
word5
但不是word4,因为没有交换价值,而是同一天的两个价值
word1(两条路径,每天一条,第 2 行的值为 url2,其他天,第 1 行和第 3 行的值为 url1)
word5(多个值,第 14 行的值为 url-7,第 15 行的值为 url-8)。
我想我应该使用 sequenceMatch 或 windowFunnel 函数,知道吗?
【问题讨论】:
请考虑修改您在此问题中发布的代码示例。就目前而言,它的格式和范围使我们很难为您提供帮助;这是一个great resource,可以帮助您开始。 -1,不要走错路。否决票是我们在这里指出内容问题的方式;改进您的格式和代码示例,我(或有人会)很乐意将其还原。祝你的代码好运! 【参考方案1】:试试这个查询:
SELECT searchword
FROM (
/* intermediate result of SELECT below
┌─searchword─┬─date_paths_array──────────────────────────────────────────────────────────────────────────────────────────────┬─isExchanged─┐
│ word3 │ [('2019-10-09',['url-5']),('2019-10-10',['url-5']),('2019-10-08',['url-5'])] │ 0 │
│ word5 │ [('2019-10-10',['url-7']),('2019-10-11',['url-7']),('2019-10-09',['url-8'])] │ 1 │
│ word1 │ [('2019-10-04',['url-1']),('2019-10-02',['url-1']),('2019-10-03',['url-2']),('2019-10-11',['url-1'])] │ 1 │
│ word4 │ [('2019-10-08',['url-5']),('2019-10-10',['url-5','url-6']),('2019-10-09',['url-5']),('2019-10-11',['url-5'])] │ 0 │
└────────────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────┴─────────────┘
*/
SELECT
searchword,
groupArray(date_paths) date_paths_array,
arrayFirstIndex((x, index) ->
index != length(date_paths_array)
AND hasAny(x.2, date_paths_array[index + 1].2) = 0,
date_paths_array,
arrayEnumerate(date_paths_array)) > 0 isExchanged
FROM (
/* intermediate result of SELECT below
┌─searchword─┬─date_paths───────────────────────┐
│ word1 │ ('2019-10-04',['url-1']) │
│ word3 │ ('2019-10-09',['url-5']) │
│ word5 │ ('2019-10-10',['url-7']) │
│ word1 │ ('2019-10-02',['url-1']) │
│ word4 │ ('2019-10-08',['url-5']) │
│ word2 │ ('2019-10-07',['url-4']) │
│ word5 │ ('2019-10-11',['url-7']) │
│ word5 │ ('2019-10-09',['url-8']) │
│ word3 │ ('2019-10-10',['url-5']) │
│ word1 │ ('2019-10-03',['url-2']) │
│ word3 │ ('2019-10-08',['url-5']) │
│ word1 │ ('2019-10-11',['url-1']) │
│ word4 │ ('2019-10-10',['url-5','url-6']) │
│ word4 │ ('2019-10-09',['url-5']) │
│ word4 │ ('2019-10-11',['url-5']) │
└────────────┴──────────────────────────────────┘
*/
SELECT searchword, (date, groupArray(path)) date_paths
FROM (
/* test dataset */
SELECT d.1 order_num, toDate(d.2) date, d.3 searchword, d.4 path
FROM (
SELECT arrayJoin([
(1, '2019-10-02', 'word1', 'url-1'),
(2, '2019-10-03', 'word1', 'url-2'),
(3, '2019-10-04', 'word1', 'url-1'),
(4, '2019-10-11', 'word1', 'url-1'),
(5, '2019-10-07', 'word2', 'url-4'),
(6, '2019-10-08', 'word3', 'url-5'),
(7, '2019-10-09', 'word3', 'url-5'),
(8, '2019-10-10', 'word3', 'url-5'),
(9, '2019-10-08', 'word4', 'url-5'),
(10, '2019-10-09', 'word4', 'url-5'),
(11, '2019-10-10', 'word4', 'url-5'),
(12, '2019-10-10', 'word4', 'url-6'),
(13, '2019-10-11', 'word4', 'url-5'),
(14, '2019-10-10', 'word5', 'url-7'),
(15, '2019-10-09', 'word5', 'url-8'),
(16, '2019-10-11', 'word5', 'url-7')]) d))
WHERE date >= '2019-10-01' AND date < '2019-11-01'
GROUP BY searchword, date)
GROUP BY searchword
HAVING count() > 1)
WHERE isExchanged;
/* Result:
┌─searchword─┐
│ word5 │
│ word1 │
└────────────┘
*/
【讨论】:
以上是关于如何选择不同日子的兑换价值? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章
Postgres - 仅选择具有 FAILURE 状态的日期