请帮助以下Oracle SQL查询:
尝试了几种方法,但未获得预期的结果。
数据集:
ORDER_NO ITEM CODE
1234 999999 777
1234 999999 111
1234 999999 777
1234 999999 111
预期结果:
ORDER_NO ITEM CODE
1234 999999 777
111
答案 0 :(得分:1)
您可以尝试通过dcast(aggregate_comments_data, event.post.id ~ event.is_comment_to_post, fun = length)
中的ROW_NUMBER
窗口函数来创建行号,然后使用CTE
来创建行号。
CASE WHEN
查询1 :
CREATE TABLE T(
ORDER_NO INT,
ITEM INT,
CODE INT
);
INSERT INTO T VALUES (1234,999999,777);
INSERT INTO T VALUES (1234,999999,111);
INSERT INTO T VALUES (1234,999999,777);
INSERT INTO T VALUES (1234,999999,111);
Results :
with cteRn as (
SELECT t1.*,ROW_NUMBER() OVER (PARTITION BY ORDER_NO,ITEM,CODE order by ORDER_NO) rn
FROM T t1
), cteGrp as (
SELECT
t1.*,ROW_NUMBER() OVER (PARTITION BY ORDER_NO,ITEM ORDER BY CODE desc) grp
FROM cteRn t1
WHERE rn = 1
)
SELECT
(CASE WHEN grp = 1 then ORDER_NO end) ORDER_NO,
(CASE WHEN grp = 1 then ITEM end) ITEM,
CODE
FROM cteGrp
答案 1 :(得分:1)
当您为SQL Developer标记了此标记(在这种情况下可以被认为是应用程序/表示层)时,您可以使用break ... nodup
:
clear breaks
break on order_no nodup on item nodup
with your_table (order_no, item, code) as (
select 1234, 999999, 777 from dual
union all select 1234, 999999, 111 from dual
union all select 1234, 999999, 777 from dual
union all select 1234, 999999, 111 from dual
)
select * from your_table;
ORDER_NO ITEM CODE
---------- ---------- ----------
1234 999999 777
111
777
111
具有多个订单和商品:
with your_table (order_no, item, code) as (
select 1234, 999999, 777 from dual
union all select 1234, 999999, 111 from dual
union all select 1234, 999999, 777 from dual
union all select 1234, 999999, 111 from dual
union all select 1235, 999999, 111 from dual
union all select 1236, 999999, 111 from dual
union all select 1236, 999998, 111 from dual
union all select 1236, 999998, 111 from dual
)
select * from your_table;
ORDER_NO ITEM CODE
---------- ---------- ----------
1234 999999 777
111
777
111
1235 999999 111
1236 999999 111
1236 999998 111
111