以每周日期为周期对用户进行排名并列出所有排名第一的用户
Ranking users with weekly date period and listing all first ranked users
我有一个名为优惠券的 table,架构如下:
CREATE TABLE "public"."coupons" (
"id" int4 NOT NULL,
"suprise" bool NOT NULL DEFAULT false,
"user_id" int4 NOT NULL,
"start" timestamp NOT NULL,
"win_price" numeric(8,2) NOT NULL DEFAULT 0::numeric,
"fold" int4 NOT NULL DEFAULT 3,
"pay" numeric(8,2) NOT NULL DEFAULT 0::numeric,
"rate" numeric(8,2) NOT NULL DEFAULT 0::numeric,
"win" varchar(255) NOT NULL DEFAULT 'H'::character varying COLLATE "default",
"end" timestamp NOT NULL,
"win_count" int4 NOT NULL DEFAULT 0,
"match_count" int4 NOT NULL DEFAULT 0,
"played" bool NOT NULL DEFAULT false,
"created_at" timestamp NOT NULL,
"updated_at" timestamp NOT NULL
)
WITH (OIDS=FALSE);
为了让用户排名超过 win_price weekly
,我编写了下面的查询以在 2015 年 7 月 27 日和 2015 年 8 月 3 日之间获得前 5 名:
SELECT ROW_NUMBER() OVER(ORDER BY sum(win_price) DESC) AS rnk,
sum(win_price) AS win_price, user_id,
min(created_at) min_create
FROM coupons
WHERE played = true AND win = 'Y'
AND created_at BETWEEN '27-07-2015' AND '03-08-2015'
GROUP BY user_id
ORDER BY rnk ASC
LIMIT 5;
我正在寻找一个新查询,该查询每周列出给定日期段内排名第一的用户。
即:对于 01-09-2015 和 30-09-2015 之间的时间段:
rnk - win_price - user_id - min_create
1 - 1.52 - 1 - ........... (first week)
1 - 10.92 - 2 - ........... (send week)
1 - 11.23 - 1 - ........... (third week and so on)
SELECT *
FROM (
SELECT date_trunc('week', created_at) AS week
, rank() OVER (PARTITION BY date_trunc('week', created_at)
ORDER BY sum(win_price) DESC NULLS LAST) AS rnk
, sum(win_price) AS win_price
, user_id
, min(created_at) min_create
FROM coupons
WHERE played = true
AND win = 'Y' AND created_at BETWEEN '27-07-2015' AND '03-08-2015'
GROUP BY 1, 4 -- reference to 1st & 4th column
) sub
WHERE rnk = 1
ORDER BY week;
这 returns 获胜用户 每周 - 获得最大 sum(win_price)
.
的用户
请注意,我使用的是 rank()
instead of row_number()
,因为您没有为每周多个获胜者定义决胜局。
还要注意排序子句DESC NULLS LAST
:这会阻止 NULL 值首先排序(如果你应该有 NULL):
- PostgreSQL sort by datetime asc, null first?
周由开始时间戳表示,您可以使用 to_char()
任意格式。
查询的关键要素:您可以在聚合函数上使用 window 函数。详情:
- Postgres window function and group by exception
考虑 SELECT
查询中的事件序列:
- Best way to get result count before LIMIT was applied
我有一个名为优惠券的 table,架构如下:
CREATE TABLE "public"."coupons" (
"id" int4 NOT NULL,
"suprise" bool NOT NULL DEFAULT false,
"user_id" int4 NOT NULL,
"start" timestamp NOT NULL,
"win_price" numeric(8,2) NOT NULL DEFAULT 0::numeric,
"fold" int4 NOT NULL DEFAULT 3,
"pay" numeric(8,2) NOT NULL DEFAULT 0::numeric,
"rate" numeric(8,2) NOT NULL DEFAULT 0::numeric,
"win" varchar(255) NOT NULL DEFAULT 'H'::character varying COLLATE "default",
"end" timestamp NOT NULL,
"win_count" int4 NOT NULL DEFAULT 0,
"match_count" int4 NOT NULL DEFAULT 0,
"played" bool NOT NULL DEFAULT false,
"created_at" timestamp NOT NULL,
"updated_at" timestamp NOT NULL
)
WITH (OIDS=FALSE);
为了让用户排名超过 win_price weekly
,我编写了下面的查询以在 2015 年 7 月 27 日和 2015 年 8 月 3 日之间获得前 5 名:
SELECT ROW_NUMBER() OVER(ORDER BY sum(win_price) DESC) AS rnk,
sum(win_price) AS win_price, user_id,
min(created_at) min_create
FROM coupons
WHERE played = true AND win = 'Y'
AND created_at BETWEEN '27-07-2015' AND '03-08-2015'
GROUP BY user_id
ORDER BY rnk ASC
LIMIT 5;
我正在寻找一个新查询,该查询每周列出给定日期段内排名第一的用户。
即:对于 01-09-2015 和 30-09-2015 之间的时间段:
rnk - win_price - user_id - min_create 1 - 1.52 - 1 - ........... (first week) 1 - 10.92 - 2 - ........... (send week) 1 - 11.23 - 1 - ........... (third week and so on)
SELECT *
FROM (
SELECT date_trunc('week', created_at) AS week
, rank() OVER (PARTITION BY date_trunc('week', created_at)
ORDER BY sum(win_price) DESC NULLS LAST) AS rnk
, sum(win_price) AS win_price
, user_id
, min(created_at) min_create
FROM coupons
WHERE played = true
AND win = 'Y' AND created_at BETWEEN '27-07-2015' AND '03-08-2015'
GROUP BY 1, 4 -- reference to 1st & 4th column
) sub
WHERE rnk = 1
ORDER BY week;
这 returns 获胜用户 每周 - 获得最大 sum(win_price)
.
请注意,我使用的是 rank()
instead of row_number()
,因为您没有为每周多个获胜者定义决胜局。
还要注意排序子句DESC NULLS LAST
:这会阻止 NULL 值首先排序(如果你应该有 NULL):
- PostgreSQL sort by datetime asc, null first?
周由开始时间戳表示,您可以使用 to_char()
任意格式。
查询的关键要素:您可以在聚合函数上使用 window 函数。详情:
- Postgres window function and group by exception
考虑 SELECT
查询中的事件序列:
- Best way to get result count before LIMIT was applied