如果这些确实是值,你可以做这样的事情id
:
select group_id,
max(case when id = 1 then item_code end) item_code1,
max(case when id = 1 then item_label end) item_label1,
max(case when id = 1 then item_detail end) iitem_detail1,
max(case when id = 1 then item_score end) item_score1,
max(case when id = 2 then item_code end) item_code2,
max(case when id = 2 then item_label end) item_label2,
max(case when id = 2 then item_detail end) iitem_detail2,
max(case when id = 2 then item_score end) item_score2,
max(case when id = 3 then item_code end) item_code3,
max(case when id = 3 then item_label end) item_label3,
max(case when id = 3 then item_detail end) iitem_detail3,
max(case when id = 3 then item_score end) item_score3
from yourtable
group by group_id
请参阅带有演示的 SQL Fiddle
结果:
| GROUP_ID | ITEM_CODE1 | ITEM_LABEL1 | IITEM_DETAIL1 | ITEM_SCORE1 | ITEM_CODE2 | ITEM_LABEL2 | IITEM_DETAIL2 | ITEM_SCORE2 | ITEM_CODE3 | ITEM_LABEL3 | IITEM_DETAIL3 | ITEM_SCORE3 |
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 10 | BLU123 | Blue 123 | Blah blah 123 | 3 | BLU124 | Blue 124 | Blah blah 124 | 6 | BLU125 | Blue 125 | Blah blah 125 | 2 |
如果您不能依赖id
表中的 ,那么您可以在要返回的记录中添加一个行号:
select group_id,
max(case when rownum = 1 then item_code end) item_code1,
max(case when rownum = 1 then item_label end) item_label1,
max(case when rownum = 1 then item_detail end) iitem_detail1,
max(case when rownum = 1 then item_score end) item_score1,
max(case when rownum = 2 then item_code end) item_code2,
max(case when rownum = 2 then item_label end) item_label2,
max(case when rownum = 2 then item_detail end) iitem_detail2,
max(case when rownum = 2 then item_score end) item_score2,
max(case when rownum = 3 then item_code end) item_code3,
max(case when rownum = 3 then item_label end) item_label3,
max(case when rownum = 3 then item_detail end) iitem_detail3,
max(case when rownum = 3 then item_score end) item_score3
from
(
select group_id, item_code, item_detail,
item_label, item_score,
@rn:=@rn+1 rownum
from yourtable, (SELECT @rn:=0) r
where group_id = 10
order by id
) src
group by group_id
请参阅带有演示的 SQL Fiddle