[英]Query a single table as master and detail and sort result where master row will be on top of each detail
can this be done? 可以做到吗? please help me.
请帮我。
I tried union, only it is not sorted like the way i want it - master row on top of detail row. 我尝试了联合,只是它没有按照我想要的方式排序-主行位于明细行的顶部。 Thank you..
谢谢..
SELECT
mover.v_id,
mover.v_registration,
mover.attached_with
from ops_vehicle mover
LEFT JOIN v_l_type ON (mover.type = v_l_type.idv_type)
where v_l_type.trailer = FALSE
union all
SELECT
trailer.v_id,
trailer.v_registration,
trailer.attached_with
from ops_vehicle trailer
LEFT JOIN v_l_type ON (trailer.type = v_l_type.idv_type)
where v_l_type.trailer = TRUE
result: query result 结果: 查询结果
The result i want is something like this: 我想要的结果是这样的:
v_id v_registration attached_with
-------------------------------------------
1 ABC 123 4
4 T/B 123
2 CBA 876 5
5 T/B 763
3 OPQ 631
The result row with v_id=4 should be under the v_id=1 because attached_with is 4. v_id = 4的结果行应位于v_id = 1以下,因为Attach_with为4。
Yes, you can do: 是的,您可以执行以下操作:
select t.*
from (<your query here>) t
order by coalesce(attached_with, v_id),
(attached_with is not null) desc,
v_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.