我有一个详细的表格,其中我想计算具有2个或更多不同action_taken值的user_ids。
详情:
| user_id | action_taken | action_date |
|---------|-----------------------|-------------|
| 1234 | clicked on a link | 1/1/2017 |
| 1234 | went to the home page | 1/5/2017 |
| 1234 | clicked on a link | 1/7/2017 |
| 1234 | clicked on a link | 1/9/2017 |
| 1234 | changed password | 1/11/2017 |
| 1234 | clicked on a link | 1/13/2017 |
| 9876 | went to the home page | 2/1/2017 |
| 9876 | went to the home page | 2/5/2017 |
| 9876 | went to the home page | 2/7/2017 |
| 9876 | went to the home page | 2/9/2017 |
| 5566 | clicked on a link | 1/1/2017 |
| 5566 | clicked on a link | 1/5/2017 |
| 5566 | changed password | 1/7/2017 |
| 5566 | clicked on a link | 1/9/2017 |
| 4433 | went to the home page | 1/5/2017 |
期望的输出:
输出1:返回actions_taken的不同值的数量。
| user_id | number_dift_action_taken_values |
|---------|---------------------------------|
| 1234 | 3 |
| 4433 | 1 |
| 5566 | 2 |
| 9876 | 1 |
输出2:仅返回具有> = 2个不同action_taken值的user_ids。
| user_id |
|---------|
| 1234 |
| 5566 |
到目前为止,这是我无法正常工作的内容:http://rextester.com/TUL87833。 HAVING子句计算属于每个组的详细信息中的行数,不 GROUP BY user_id,action_taken子句...
指定的组数select
user_id
,action_taken
,count(*)
from
tbl
group by
user_id
,action_taken
having count(*) >=2;
| user_id | action_taken | count |
|---------|-----------------------|-------|
| 1234 | clicked on a link | 4 |
| 5566 | clicked on a link | 3 |
| 9876 | went to the home page | 4 |
答案 0 :(得分:2)
输出1
SELECT user_id,
COUNT(DISTINCT action_taken) number_dift_action_taken_values
FROM t_tab
GROUP BY user_id
结果
user_id number_dift_action_taken_values
1234 3
4433 1
5566 2
9876 1
输出2
SELECT user_id
FROM t_tab
GROUP BY user_id
HAVING COUNT(DISTINCT action_taken) >= 2
结果
user_id
1234
5566
答案 1 :(得分:0)
对于每个输出#:
,我会从以下内容开始输出1:
select
user_id
,count(distinct action_taken) as number_dift_action_taken_values
into #Output1_tbl
from details_tbl
group by user_id
输出2:
select *
from #Output1_tbl
where number_dift_action_taken_values >= 2