在不同的 postgres 的多列上按 id desc 排序
Sort by id desc on multiple columns distinct postrges
SELECT impressions.*
FROM impressions
WHERE impressions.user_id = 2
AND impressions.action_name = 'show'
AND (impressions.message IS NOT NULL)
GROUP BY impressionable_id, impressionable_type
我想 select 从 table 所有在 impressionable_id 和 impresssionable_type 上唯一的最后展示中按 ID 降序排序并获得最后 10
进一步解释
id, impressionabale_type, impressionable_id, action_name
50012, assignment, 2, show
50011, assignment, 1, show
50010, person, 1, show
50009, assignment, 1, show
50008, person, 5, show
50007, person, 4, show
50006, person, 3, show
50005, person, 1, show
50004, person, 1, show
50003, person, 2, show
50002, person, 2, show
50001, person, 1, show
50000, person, 1, show
理想情况下我想要这个
50012, assignment, 2, show
50011, assignment, 1, show
50010, person, 1, show
50008, person, 5, show
50007, person, 4, show
50006, person, 3, show
50003, person, 2, show
我已经尝试过 distinct 和 group by,但我的 sql 知识充其量是公平的。
我明白了
PG::GroupingError: ERROR: column "impressions.id" must appear in the GROUP BY clause or be used in an aggregate function
有人可以解释一下吗
也许这会满足您的需求:
SELECT t2.*
FROM (
SELECT DISTINCT impressionable_id, impressionabale_type
FROM impressions
WHERE impressions.action_name = 'show'
) t1, LATERAL (
SELECT *
FROM impressions
WHERE (t1.impressionable_id,t1.impressionabale_type) = (impressionable_id,impressionabale_type)
ORDER BY id DESC
LIMIT 1
) t2
ORDER BY id DESC
LIMIT 10
这将找到 impressionable_id
和 impressionable_type
的所有唯一组合,并且对于每个组合,将在 LATERAL
子查询中找到具有最大 id
的行。
select *
from (
select *,
row_number() over (
partition by impressionable_id, impressionable_type
order by id desc
) as rn
from impressions
where
user_id = 2
and action_name = 'show'
and message is not null
) s
where rn = 1
SELECT impressions.*
FROM impressions
WHERE impressions.user_id = 2
AND impressions.action_name = 'show'
AND (impressions.message IS NOT NULL)
GROUP BY impressionable_id, impressionable_type
我想 select 从 table 所有在 impressionable_id 和 impresssionable_type 上唯一的最后展示中按 ID 降序排序并获得最后 10
进一步解释
id, impressionabale_type, impressionable_id, action_name
50012, assignment, 2, show
50011, assignment, 1, show
50010, person, 1, show
50009, assignment, 1, show
50008, person, 5, show
50007, person, 4, show
50006, person, 3, show
50005, person, 1, show
50004, person, 1, show
50003, person, 2, show
50002, person, 2, show
50001, person, 1, show
50000, person, 1, show
理想情况下我想要这个
50012, assignment, 2, show
50011, assignment, 1, show
50010, person, 1, show
50008, person, 5, show
50007, person, 4, show
50006, person, 3, show
50003, person, 2, show
我已经尝试过 distinct 和 group by,但我的 sql 知识充其量是公平的。
我明白了
PG::GroupingError: ERROR: column "impressions.id" must appear in the GROUP BY clause or be used in an aggregate function
有人可以解释一下吗
也许这会满足您的需求:
SELECT t2.*
FROM (
SELECT DISTINCT impressionable_id, impressionabale_type
FROM impressions
WHERE impressions.action_name = 'show'
) t1, LATERAL (
SELECT *
FROM impressions
WHERE (t1.impressionable_id,t1.impressionabale_type) = (impressionable_id,impressionabale_type)
ORDER BY id DESC
LIMIT 1
) t2
ORDER BY id DESC
LIMIT 10
这将找到 impressionable_id
和 impressionable_type
的所有唯一组合,并且对于每个组合,将在 LATERAL
子查询中找到具有最大 id
的行。
select *
from (
select *,
row_number() over (
partition by impressionable_id, impressionable_type
order by id desc
) as rn
from impressions
where
user_id = 2
and action_name = 'show'
and message is not null
) s
where rn = 1