我们说我已经获得了以下数据(注意日期差异的ID):
customer_id created_at
1000 2017-12-29 20:48:54+00
1000 2017-12-30 12:48:56+00
1000 2017-12-30 12:49:26+00
1002 2017-12-30 12:52:36+00
1001 2017-12-30 12:54:15+00
1002 2017-12-30 13:54:15+00
1001 2017-12-30 13:56:58+00
1000 2018-01-02 13:01:13+00
1001 2018-01-02 20:29:19+00
1002 2018-01-02 20:29:31+00
1000 2018-01-03 20:30:28+00
1001 2018-01-03 20:38:40+00
我希望获得客户创建记录的天数。如果客户已经创建了一天的多个记录,那么它仍然计为1.因此,上述数据的输出应为:
customer_id count
1000 4
1001 3
1002 2
我尝试使用to_char(created_at, 'YYYY-mm-dd')
和DISTINCT ON(created_at)
以及count
时尝试了不同的查询,但我没有得到我想要的汇总结果。 F.e:
SELECT distinct on (to_char(created_at, 'YYYY-mm-dd')) count(customer_id), customer_id
FROM registration
WHERE created_at >= '2017-12-29' and created_at <= '2018-01-03' and customer_id in (1000,1001,1002)
group by customer_id, created_at;
答案 0 :(得分:1)
在派生表中使用distinct
(from
子句中的子查询):
select customer_id, count(created_at)
from (
select distinct customer_id, created_at::date
from registration
) s
group by 1
order by 1;
customer_id | count
-------------+-------
1000 | 4
1001 | 3
1002 | 2
(3 rows)
实际上,用户1001活跃了3天,而不是4天。
答案 1 :(得分:1)
您可以在package main
import (
"fmt"
"github.com/hashicorp/hcl"
)
type T struct {
LogDir string `hcl:"log_dir"`
}
func main() {
var t T
err := hcl.Decode(&t, `log_dir = "/var/log"`)
fmt.Println(t.LogDir, err)
}
内使用DISTINCT
:
COUNT