SQL: 分组后过滤

分组后过滤使用having

SELECT
	user_id,
	phone,
	COUNT ( user_id ) AS  user_cnt
FROM
	"ticket" 
WHERE
	(
	  
		is_deleted = FALSE 
		AND ( "ticket"."create_time" AT TIME ZONE"Asia/Shanghai" ) :: DATE >= "2019-11-30" :: DATE 
		AND ( "ticket"."create_time" AT TIME ZONE"Asia/Shanghai" ) :: DATE <= "2019-12-04" :: DATE 

	) 
GROUP BY
	"ticket"."user_id",
	"ticket"."phone"
HAVING COUNT ( "ticket"."user_id" )  >= 2
ORDER BY user_cnt DESC;

django having

分组后.filter即可

    queryset = queryset.values("user")
        .annotate(count=Count("user"))
        .values("count", "user")
        .filter(count__gt=once)