我在解释这个问题时遇到了问题,所以如果有人可以对标题或问题进行调整,请执行此操作。
我有一个简单的SQL查询,我正在运行
SELECT orders.customer_no, orders.order_no FROM orders WHERE orders.creation = '01-JAN-14';
导致
customer_no order_no
----------- ----------
0 8051729
2 2809137
2 3794827
3 1934678
3 9237192
6 3462890
6 3131378
6 6267190
6 2864952
6 1325645
但我想要的是
customer_no order_no
----------- ----------
0 8051729
2 2809137 3794827
3 1934678 9237192
6 3462890 3131378 6267190 2864952 1325645
是否有可能在SQL中直接执行此类操作?
编辑:使用Oracle8i企业版8.1.7.4.0版 - 生产。
答案 0 :(得分:0)
我相信你想要:
select orders.customer_no, listagg(orders.order_no, ' ') within group (order by orders.order_no) orders.order_no
from orders
WHERE orders.creation = '01-JAN-14'
group by orders.customer_no;
在MySQL中你需要GROUP_CONCAT函数,它在Oracle中大致是LISTAGG,根据:
Is there any function in oracle similar to group_concat in mysql?
答案 1 :(得分:0)
根据Oracle: Way to aggregate concatenate an ungrouped column in grouped results,您可以尝试以下内容:
WITH j
AS (SELECT customer_no, order_no
FROM orders
WHERE creation = '01-JAN-14')
SELECT RTRIM (
EXTRACT (SYS_XMLAGG (XMLELEMENT ("X", order_no || ' ')), '/ROWSET/X/text()').getstringval (),
', ')
FROM j
GROUP BY customer_no;