所以我有一个这样的表:
Taxi Client Time
Tom A 1
Tom A 2
Tom B 3
Tom A 4
Tom A 5
Tom A 6
Tom B 7
Tom B 8
Bob A 1
Bob A 2
Bob A 3
,预期结果将是这样:
Tom 3
Bob 1
我已经使用分区函数对连续值进行计数,但是结果变为:
Tom A 2
Tom A 3
Tom B 2
Bob A 2
请帮助,我的英语不好,谢谢!
答案 0 :(得分:0)
使用非重复计数
select taxi ,count( distinct cient)
from table_name
group by taxi
您的预期输出似乎不正确
答案 1 :(得分:0)
我看不到您从哪里获得3。如果您要尝试按照自己的问题回答问题,并且仅按连续的顺序按客户分组,然后获取不同的分组数量,那么我可以通过以下查询为您提供帮助。鲍勃(Bob)有1组,汤姆(Tom)有4组。
按出租车划分,按出租车排序,时间,并检查此客户是否与该出租车的先前客户匹配。如果是,则不计此行。如果否,请计数此行,这是一个新组。
SELECT FEE.taxi,
SUM(FEE.clientNotSameAsPreviousInSequence)
FROM
(
SELECT taxi,
CASE
WHEN PreviousClient IS NULL THEN
1
WHEN PreviousClient <> client THEN
1
ELSE
0
END AS clientNotSameAsPreviousInSequence
FROM
(
SELECT *,
LAG(client) OVER (PARTITION BY taxi ORDER BY taxi, time) AS PreviousClient
FROM table
) taxisWithPreviousClient
) FEE
GROUP BY FEE.taxi;
答案 2 :(得分:0)
这是空白和岛屿问题的变体。您可以使用窗口函数来解决它:
select taxi, count(*)
from (select t.taxi, t.client, count(*) as num_times
from (select t.*,
row_number() over (partition by taxi order by time) as seqnum,
row_number() over (partition by taxi, client order by time) as seqnum_c
from t
) t
group by t.taxi, t.client, (seqnum - seqnum_c)
having count(*) >= 2
)
group by taxi;