我需要编写一个SQL查询来将数据迁移到Excel文件中,我能够使用ORA_EXCEL从其他表传输数据但我无法对此特定部分执行相同操作,因为它需要我将它们添加到此格式中。所以我计划创建一个新表并从该新表中调用它。我目前正在使用Oracle 10g。下面显示了我所拥有的表的一个示例
STUDENT_BOOK_RECORD(---表名---)
+-------+----------+------------+-------------+---------------+------------+
|BOOK_ID|CHECKED_BY|DATE_OF_LOAN|HOURS_OF_LOAN| COMMENTS | Student |
+-------+----------+------------+-------------+---------------+------------+
| 1 | Mr.Alex | 1/1/17 | 1 |Its a good read| Bob |
+-------+----------+------------+-------------+---------------+------------+
| 2 | Miss Li | 1/2/16 | 0.5 | Its alright | Tom |
+-------+----------+------------+-------------+---------------+------------+
| 2 | Miss Li | 1/1/17 | 3 | Its a bad book| Tim |
+-------+----------+------------+-------------+---------------+------------+
| 1 | Mr.Alex | 3/1/15 | 2 | Its boring | Jane |
+-------+----------+------------+-------------+---------------+------------+
| 1 | Mr.Alex | 4/1/12 | 5 | Fun to read | Sally |
+-------+----------+------------+-------------+---------------+------------+
现在我正在尝试使用基于类似BOOK_ID的连接数据创建一个表,并按日期对BOOK_DESCRIPTION中的信息进行排序。
LIBRARY_BOOK_RECORD(---表名---)
+-----------+-----------------------------------------------+
| BOOK_ID | BOOK_DESCRIPTION |
+-----------+-----------------------------------------------+
| | Total Hours of Loan: 8 |
| | ----------------------------------------- |
| | Written by: Mr.Alex |
| | Date of Loan : 4/1/12 |
| | Hour(s) of Loan: 5 |
| | Comments: Fun to read |
| | Student: Sally |
| | ----------------------------------------- |
| | Written by: Mr.Alex |
| 1 | Date of Loan : 3/1/15 |
| | Hour(s) of Loan: 2 |
| | Comments: Its boring |
| | Student: Jane |
| | ----------------------------------------- |
| | Written by: Mr.Alex |
| | Date of Loan : 1/1/17 |
| | Hour(s) of Loan: 1 |
| | Comments: Its a good read |
| | Student: Bob |
| | ----------------------------------------- |
+-----------+-----------------------------------------------+
| | Total Hours of Loan: 3.5 |
| | ----------------------------------------- |
| | Written by: Miss Li |
| | Date of Loan : 1/2/16 |
| | Hour(s) of Loan: 0.5 |
| 2 | Comments: Its alright |
| | Student: Tom |
| | ----------------------------------------- |
| | Written by: Miss Li |
| | Date of Loan : 1/1/17 |
| | Hour(s) of Loan: 3 |
| | Comments: Its a bad book |
| | Student: Tim |
+-----------+-----------------------------------------------+
请帮忙!!!并提前谢谢你。
答案 0 :(得分:0)
这样的事可能有用。当然,没有办法在其列中精确对齐book_id
(也许您在报告软件中至少可以做到这一点)。我也跟随你的领导,并使用了两位数的年份,虽然这是一个非常糟糕的做法,最好避免。由于这不是你问题的主题,所以我不管它。
在下面的查询中,首先我创建一个“factored subquery”(顶部的WITH
子句)用于测试目的。在你的工作中,你不需要它;您只需要从注释行下方开始的查询。使用您的实际表名和列名。
with
student_book ( book_id, checked_by, date_of_loan, hours_of_loan, comments, student )
as (
select 1, 'Mr.Alex', to_date('1/1/17', 'mm/dd/rr'), 1 , 'Its a good read', 'Bob'
from dual union all
select 2, 'Miss Li', to_date('1/2/16', 'mm/dd/rr'), 0.5, 'Its alright' , 'Tom'
from dual union all
select 2, 'Miss Li', to_date('1/1/17', 'mm/dd/rr'), 3 , 'Its a bad book' , 'Tim'
from dual union all
select 1, 'Mr.Alex', to_date('3/1/15', 'mm/dd/rr'), 2 , 'Its boring' , 'Jane'
from dual union all
select 1, 'Mr.Alex', to_date('4/1/12', 'mm/dd/rr'), 5 , 'Fun to read' , 'Sally'
from dual
)
-- end of test data; SQL query begins BELOW THIS LINE.
select book_id,
'Total Hours of Loan: ' || to_char(sum(hours_of_loan)) || chr(10) ||
lpad('-', 30, '-') || chr(10) ||
listagg(descr, chr(10) || lpad('-', 30, '-') || chr(10))
within group (order by date_of_loan) as book_description
from (
select book_id, date_of_loan, hours_of_loan,
'Written by: ' || checked_by || chr(10) ||
'Date of Loan : ' || to_char(date_of_loan, 'mm/dd/yy') || chr(10) ||
'Hour(s) of Loan: ' || to_char(hours_of_loan) || chr(10) ||
'Comments: ' || comments || chr(10) ||
'Student: ' || student as descr
from student_book
)
group by book_id
order by book_id
;
<强>输出强>:
BOOK_ID BOOK_DESCRIPTION
------- --------------------------------
1 Total Hours of Loan: 8
------------------------------
Written by: Mr.Alex
Date of Loan : 04/01/12
Hour(s) of Loan: 5
Comments: Fun to read
Student: Sally
------------------------------
Written by: Mr.Alex
Date of Loan : 03/01/15
Hour(s) of Loan: 2
Comments: Its boring
Student: Jane
------------------------------
Written by: Mr.Alex
Date of Loan : 01/01/17
Hour(s) of Loan: 1
Comments: Its a good read
Student: Bob
2 Total Hours of Loan: 3.5
------------------------------
Written by: Miss Li
Date of Loan : 01/02/16
Hour(s) of Loan: .5
Comments: Its alright
Student: Tom
------------------------------
Written by: Miss Li
Date of Loan : 01/01/17
Hour(s) of Loan: 3
Comments: Its a bad book
Student: Tim
修改:对于11之前的Oracle版本,listagg()
将无效。 xmlagg()
早先存在(我相信)。以下修改应该有效。 (它在每本书的描述底部产生一个额外的“破折号线”,如果需要可以删除它 - 我没有打扰它。)
更换
listagg(descr, chr(10) || lpad('-', 30, '-') || chr(10))
within group (order by date_of_loan) as book_description
使用
xmlagg(xmlelement(x, descr, chr(10) || lpad('-', 30, '-') || chr(10)).extract('//text()')
order by date_of_loan) as book_description