Database 有没有办法显示某一业务类型的前10名平均评级?
我的数据库方案中有以下表格Database 有没有办法显示某一业务类型的前10名平均评级?,database,postgresql,Database,Postgresql,我的数据库方案中有以下表格 CREATE TABLE public.organization_rating ( rating integer NOT NULL, user_id integer NOT NULL, organization_id integer NOT NULL, CONSTRAINT organization_rating_pkey PRIMARY KEY (user_id, organization_id), CONSTRAINT us
CREATE TABLE public.organization_rating
(
rating integer NOT NULL,
user_id integer NOT NULL,
organization_id integer NOT NULL,
CONSTRAINT organization_rating_pkey PRIMARY KEY (user_id, organization_id),
CONSTRAINT user_id FOREIGN KEY (user_id)
REFERENCES public.users (user_id) MATCH SIMPLE
ON UPDATE CASCADE
ON DELETE CASCADE,
CONSTRAINT stars CHECK (rating >= 1 AND rating < 5)
)
我想实现一个查询,该查询提供以下信息:
每种业务类型的前10名组织评级
每个业务阶段前10名组织
评级最差的前三名组织
由于查询看起来很相似,我只需要订购DESC或ASC,这取决于需求,我只需要一个查询就可以工作,我将使用另外两个查询。我尝试实现此查询:
CREATE TABLE public.organization
(
org_id integer NOT NULL DEFAULT nextval('organization_org_id_seq'::regclass),
name character varying(90) COLLATE pg_catalog."default" NOT NULL,
description character varying(90) COLLATE pg_catalog."default" NOT NULL,
email text COLLATE pg_catalog."default" NOT NULL,
phone_number character varying COLLATE pg_catalog."default" NOT NULL,
bt_id integer NOT NULL,
bs_id integer NOT NULL,
CONSTRAINT organization_pkey PRIMARY KEY (org_id),
CONSTRAINT bs_id FOREIGN KEY (bs_id)
REFERENCES public.business_step (bs_id) MATCH SIMPLE
ON UPDATE CASCADE
ON DELETE CASCADE
NOT VALID,
CONSTRAINT bt_id FOREIGN KEY (bt_id)
REFERENCES public.business_type (bt_id) MATCH SIMPLE
ON UPDATE CASCADE
ON DELETE CASCADE
NOT VALID
)
with average_rating as (
SELECT organization_id as Organization_ID, ROUND(AVG(rating)) as Rating
FROM organization_rating
GROUP BY organization_id
),
ordered_data as (
select org_id, bt_id, rating, row_number() over (partition by bt_id order by rating desc) rank
from average_rating r
join organization o on o.org_id = r.organization_id
where bt_id = 1
order by bt_id, rating desc
)
select org_id, bt_id, rating
from ordered_data
where rank <= 10
以下是我的选择声明:
SELECT O.org_id, O.bt_id, R.rating
FROM public.organization as O
INNER JOIN public.organization_rating as R ON O.org_id = R.organization_id
WHERE bt_id=1
GROUP by org_id, bt_id, rating
ORDER BY ROUND(AVG(rating)) DESC LIMIT 10
但结果如下:
似乎有一个错误,不同的组织被复制。这些是被复制组织的实际平均值:
及
为什么要复制组织id
提前感谢。您看到重复记录的原因是因为组织中的评级是按用户进行的。可以有多个用户对一个组织进行评级。您应该首先计算平均评级,然后加入organization表 对于bt_id=1,您可以执行以下操作:
with average_rating as (
select organization_id as org_id, avg(rating) as avg_rating
from organization_rating r
group by org_id
)
select org_id, bt_id, avg_rating
from average_rating r
join organization o on o.org_id = r.org_id
where r.bt_id = 1
order by avg_rating desc limit 10;
如果要在单个查询中获取所有数据,可以使用窗口函数:
with average_rating as (
select organization_id as org_id, avg(rating) as avg_rating
from organization_rating r
group by org_id
),
ordered_data as (
select org_id, bt_id, avg_rating, row_number() over (partition by bt_id order by avg_rating desc) rank
from average_rating r
join organization o on o.org_id = r.org_id
order by bt_id, avg_rating desc
)
select org_id, bt_id, avg_rating
from ordered_data
where rank <= 10
多亏了mihai_f87,我才能够构建这个查询:
CREATE TABLE public.organization
(
org_id integer NOT NULL DEFAULT nextval('organization_org_id_seq'::regclass),
name character varying(90) COLLATE pg_catalog."default" NOT NULL,
description character varying(90) COLLATE pg_catalog."default" NOT NULL,
email text COLLATE pg_catalog."default" NOT NULL,
phone_number character varying COLLATE pg_catalog."default" NOT NULL,
bt_id integer NOT NULL,
bs_id integer NOT NULL,
CONSTRAINT organization_pkey PRIMARY KEY (org_id),
CONSTRAINT bs_id FOREIGN KEY (bs_id)
REFERENCES public.business_step (bs_id) MATCH SIMPLE
ON UPDATE CASCADE
ON DELETE CASCADE
NOT VALID,
CONSTRAINT bt_id FOREIGN KEY (bt_id)
REFERENCES public.business_type (bt_id) MATCH SIMPLE
ON UPDATE CASCADE
ON DELETE CASCADE
NOT VALID
)
with average_rating as (
SELECT organization_id as Organization_ID, ROUND(AVG(rating)) as Rating
FROM organization_rating
GROUP BY organization_id
),
ordered_data as (
select org_id, bt_id, rating, row_number() over (partition by bt_id order by rating desc) rank
from average_rating r
join organization o on o.org_id = r.organization_id
where bt_id = 1
order by bt_id, rating desc
)
select org_id, bt_id, rating
from ordered_data
where rank <= 10
通过这个查询,我可以搜索每种业务类型的前10个组织
输出
当我运行查询时,会出现以下消息:错误:列引用组织id不明确第7行:选择组织id、bt id、平均评级、部分上的行号。。。