하위 쿼리에서 order by
을 수행하고 싶습니다. 대신 주 쿼리에서 distinct on
을 수행하지만 순서가 제거되거나 다시 설정된 이유는 무엇입니까? 실제로는 동일한 레벨에서 distinct on
과 order by
이라는 문을 하나만 쓰면 원하는대로 내림차순으로 내려야합니다. 나는 왜 이것을 이해하고 싶습니다 distinct on
는 아래의 쿼리가 좋아서 주문을 삭제할 것입니다.하위 쿼리에서 "DISTINCT"가 제거되고 "ORDER BY"도 느림
쿼리
select distinct on (id) * from (
select * from products
left outer join product_descriptions on product_descriptions.product_id = products.id
order by id desc) as A
결과 :
id product_name price
1 Navy Floral Embroidery Bomber Jacket 1390
2 Black Floral Embroidery Bomber Jacket 1390
3 Blue Wandy Blouse 750
4 White Adele Pants 1790
5 White Wandy Blouse 750
6 Black Wandy Blouse 750
7 Navy Adele Pants 1790
난 내가 시도했지만 순서가 될 것으로 내가 더 빨리을하기 때문에 order by
의 차이 수준으로 distinct on
이동을 시도하고 있기 때문에 질문이 있습니다 제거되었습니다. 이 쿼리에서 매우 느린 쿼리 문제를 해결하고 있습니다.
with recursive categ_all as
(
select *, id as root_category,array_append(null,id) as category_path
from categories
where parent_id is null
union all
select c.*, p.root_category,array_append(p.category_path,c.id)
from categories c
join categ_all p on c.parent_id[1] = p.id
)
select * from products as p
left outer join categ_all on categ_all.id = ANY (p.category_ids)
order by p.created_at limit 20
난 볼 내가 order by
을 제거하거나 배열 left outer join
제거 때. 더 빠를 것이지만 배열을 사용해야합니다. 감사합니다. 당신은 당신이 원하는 것을 달성하기 위해 서브 쿼리의 주문에 의존해서는 안 일반적으로
select distinct on (p.id) *
from products p join
product_descriptions pd
on p.id = pd.product_id
order by p.id;
:
내부 쿼리 대신 외부 쿼리를 사용해보십시오. –
샘플 테이블 데이터와 예상 결과를 형식이 지정된 텍스트 (이미지 또는 이미지 링크가 아님)로 추가하십시오. – jarlh
현재 쿼리 시도도 표시하십시오. – jarlh