SQL 加上嵌套 table 列的格式
SQL Plus formatting a nested table column
我在显示嵌套的 table 列数据时遇到问题,因为某些记录正在拖尾,有没有办法整理出 order_items
彼此下方的格式?
SQL> SELECT order_id, billing_name, items FROM orders;
ORDER_ID BILLING_NAME ITEMS(ORDER_ID, PRODUCT_ID, SELLER_ID, SUB_ORDER_NUMBER, QUANTITY, CONDITION, UNIT_PRICE, COST_CHARGE, TOTAL)
---------- --------------- ---------------------------------------------------------------------------------------------------------------------------------------
1 John Smith ORDER_ITEMS(ORDER_ITEM(1, 1, 1, '3026143053', 1, 'Brand new ', 53.49, 0, 53.49), ORDER_ITEM(1, 2, 2, '3029608429', 1, 'Brand new ', 1.9
2 Sarah Jones ORDER_ITEMS(ORDER_ITEM(2, 3, 3, '3054134547', 2, 'Brand New ', 53.49, 0, 106.98), ORDER_ITEM(2, 4, 4, '3053273551', 1, 'Brand New ', 29
3 Tom Sharpe ORDER_ITEMS(ORDER_ITEM(3, 2, 2, '3073748221', 1, 'Brand New ', 7.97, 2, 9.97), ORDER_ITEM(3, 6, 5, '3146744589', 1, 'Brand New ', 779.9
4 Derek Miller ORDER_ITEMS(ORDER_ITEM(4, 4, 4, '3124685316', 1, 'Brand New ', 299, 0, 299), ORDER_ITEM(4, 5, 5, '3157302741', 1, 'Brand New ', 639.95,
5 Mark Dwight ORDER_ITEMS(ORDER_ITEM(5, 1, 1, '3246315960', 1, 'Brand New ', 53.49, 0, 53.49), ORDER_ITEM(5, 2, 2, '3354174322', 1, 'Brand New ', 1.9
6 Lucy Nolan ORDER_ITEMS(ORDER_ITEM(6, 4, 4, '3821362630', 1, 'Brand New ', 299, 0, 299), ORDER_ITEM(6, 3, 3, '3902471881', 1, 'Brand New ', 53.49,
6 rows selected.
SQL> SPOOL OFF
您正在寻找这样的结果吗?
ORDER_ID BILLING_NAME PRODUCT_ID SELLER_ID SUB_ORDER_ QUANTITY CONDITION UNIT_PRICE COST_CHARGE TOTAL
-------- --------------- ---------- --------- ---------- -------- --------- ---------- ----------- --------
1 John Smith 1 1 3026143053 1 Brand new 53.49 0 53.49
2 2 3029608429 1 Brand new 1.90 1 2.90
2 Sarah Jones 3 3 3054134547 2 Brand New 53.49 0 106.98
4 4 3053273551 1 Brand New 29.22 0 29.22
3 Tom Sharpe 2 2 3073748221 1 Brand New 7.97 2 9.97
6 5 3146744589 1 Brand New 779.95 0 779.95
4 Derek Miller 4 4 3124685316 1 Brand New 299.00 0 299.00
5 5 3157302741 1 Brand New 639.95 0 639.95
5 Mark Dwight 1 1 3246315960 1 Brand New 53.49 0 53.49
2 2 3354174322 1 Brand New 1.90 1 1.90
6 Lucy Nolan 4 4 3821362630 1 Brand New 299.00 0 299.00
3 3 3902471881 1 Brand New 53.49 1 54.49
您可以通过结合两件事来做到这一点:扁平化数据的查询(取消嵌套嵌套的 table 列 - 每个 order_id
生成多行)和 SQL*加上格式化命令 - 在这种情况下,break
命令为每个组显示每个 (order_id, billing_name)
一次。您还可以 need/want 格式化各个列,例如,单价 1.9 显示为 1.90,但这是一个不相关(并且更容易)的问题;我没有展示如何做到这一点。
所以,这里先是 SQL*Plus 命令,然后是查询。请注意,嵌套 table 中的记录具有 order_id
属性;那应该是 order_id
你已经在 table 中了,所以我把它从查询(及其输出)中去掉了。实际上,如果“tables”嵌套在已经具有 order_id
的父 table 中,那么它为何成为记录的一部分并不清楚;我会让你对此进行哲学思考。
SQL> break on order_id on billing_name
SQL> select o.order_id, o.billing_name, product_id, seller_id, sub_order_number,
2 quantity, condition, unit_price, cost_charge, total
3 from orders o left outer join lateral (select * from table(o.items))
4 on null is null
5 /
outer(横向)连接对于 items
是原子的 null
的情况是必要的,以及 items
是空嵌套 table 的情况。两种情况都有可能;我假设你知道这两者不一样。
我在显示嵌套的 table 列数据时遇到问题,因为某些记录正在拖尾,有没有办法整理出 order_items
彼此下方的格式?
SQL> SELECT order_id, billing_name, items FROM orders;
ORDER_ID BILLING_NAME ITEMS(ORDER_ID, PRODUCT_ID, SELLER_ID, SUB_ORDER_NUMBER, QUANTITY, CONDITION, UNIT_PRICE, COST_CHARGE, TOTAL)
---------- --------------- ---------------------------------------------------------------------------------------------------------------------------------------
1 John Smith ORDER_ITEMS(ORDER_ITEM(1, 1, 1, '3026143053', 1, 'Brand new ', 53.49, 0, 53.49), ORDER_ITEM(1, 2, 2, '3029608429', 1, 'Brand new ', 1.9
2 Sarah Jones ORDER_ITEMS(ORDER_ITEM(2, 3, 3, '3054134547', 2, 'Brand New ', 53.49, 0, 106.98), ORDER_ITEM(2, 4, 4, '3053273551', 1, 'Brand New ', 29
3 Tom Sharpe ORDER_ITEMS(ORDER_ITEM(3, 2, 2, '3073748221', 1, 'Brand New ', 7.97, 2, 9.97), ORDER_ITEM(3, 6, 5, '3146744589', 1, 'Brand New ', 779.9
4 Derek Miller ORDER_ITEMS(ORDER_ITEM(4, 4, 4, '3124685316', 1, 'Brand New ', 299, 0, 299), ORDER_ITEM(4, 5, 5, '3157302741', 1, 'Brand New ', 639.95,
5 Mark Dwight ORDER_ITEMS(ORDER_ITEM(5, 1, 1, '3246315960', 1, 'Brand New ', 53.49, 0, 53.49), ORDER_ITEM(5, 2, 2, '3354174322', 1, 'Brand New ', 1.9
6 Lucy Nolan ORDER_ITEMS(ORDER_ITEM(6, 4, 4, '3821362630', 1, 'Brand New ', 299, 0, 299), ORDER_ITEM(6, 3, 3, '3902471881', 1, 'Brand New ', 53.49,
6 rows selected.
SQL> SPOOL OFF
您正在寻找这样的结果吗?
ORDER_ID BILLING_NAME PRODUCT_ID SELLER_ID SUB_ORDER_ QUANTITY CONDITION UNIT_PRICE COST_CHARGE TOTAL
-------- --------------- ---------- --------- ---------- -------- --------- ---------- ----------- --------
1 John Smith 1 1 3026143053 1 Brand new 53.49 0 53.49
2 2 3029608429 1 Brand new 1.90 1 2.90
2 Sarah Jones 3 3 3054134547 2 Brand New 53.49 0 106.98
4 4 3053273551 1 Brand New 29.22 0 29.22
3 Tom Sharpe 2 2 3073748221 1 Brand New 7.97 2 9.97
6 5 3146744589 1 Brand New 779.95 0 779.95
4 Derek Miller 4 4 3124685316 1 Brand New 299.00 0 299.00
5 5 3157302741 1 Brand New 639.95 0 639.95
5 Mark Dwight 1 1 3246315960 1 Brand New 53.49 0 53.49
2 2 3354174322 1 Brand New 1.90 1 1.90
6 Lucy Nolan 4 4 3821362630 1 Brand New 299.00 0 299.00
3 3 3902471881 1 Brand New 53.49 1 54.49
您可以通过结合两件事来做到这一点:扁平化数据的查询(取消嵌套嵌套的 table 列 - 每个 order_id
生成多行)和 SQL*加上格式化命令 - 在这种情况下,break
命令为每个组显示每个 (order_id, billing_name)
一次。您还可以 need/want 格式化各个列,例如,单价 1.9 显示为 1.90,但这是一个不相关(并且更容易)的问题;我没有展示如何做到这一点。
所以,这里先是 SQL*Plus 命令,然后是查询。请注意,嵌套 table 中的记录具有 order_id
属性;那应该是 order_id
你已经在 table 中了,所以我把它从查询(及其输出)中去掉了。实际上,如果“tables”嵌套在已经具有 order_id
的父 table 中,那么它为何成为记录的一部分并不清楚;我会让你对此进行哲学思考。
SQL> break on order_id on billing_name
SQL> select o.order_id, o.billing_name, product_id, seller_id, sub_order_number,
2 quantity, condition, unit_price, cost_charge, total
3 from orders o left outer join lateral (select * from table(o.items))
4 on null is null
5 /
outer(横向)连接对于 items
是原子的 null
的情况是必要的,以及 items
是空嵌套 table 的情况。两种情况都有可能;我假设你知道这两者不一样。