Sql 用相同的ID连接不同的数据
我需要编写SQL查询以将数据迁移到Excel文件中,我可以使用ORA_Excel从其他表中传输数据,但我无法对该特定部分执行相同的操作,因为它需要我将数据添加到此格式中。所以我计划创建一个新表并从新表调用它。我目前正在使用Oracle 10g。下面是我的表格示例 学生用书记录(---表名--) 现在,我正在尝试创建一个表,其中包含基于类似BOOK_ID的连接数据,并按日期对BOOK_描述中的信息进行排序 库\书\记录(---表名--)Sql 用相同的ID连接不同的数据,sql,oracle,Sql,Oracle,我需要编写SQL查询以将数据迁移到Excel文件中,我可以使用ORA_Excel从其他表中传输数据,但我无法对该特定部分执行相同的操作,因为它需要我将数据添加到此格式中。所以我计划创建一个新表并从新表调用它。我目前正在使用Oracle 10g。下面是我的表格示例 学生用书记录(---表名--) 现在,我正在尝试创建一个表,其中包含基于类似BOOK_ID的连接数据,并按日期对BOOK_描述中的信息进行排序 库\书\记录(---表名--) 请帮忙!!!提前谢谢你。像这样的东西可能有用。当然,没有办法
请帮忙!!!提前谢谢你。像这样的东西可能有用。当然,没有办法将
图书id
在其列中精确对齐(也许您可以在报告软件中至少做这么多)。我也跟着你的建议,使用了今年的两位数,尽管这是一个非常糟糕的做法,最好避免。既然这不是你问题的主题,我就不提了
在下面的查询中,首先我创建了一个“分解子查询”(顶部带有子句的),用于测试目的。在你的工作中你不需要它;您只需要从注释行下方开始的查询。使用实际的表名和列名
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版本,listag()
将不起作用xmlagg()
早就存在了(我相信)。下面的修改应该有效。(它在每本书描述的底部都会产生一条额外的“虚线”,如果需要,可以删除它——我不介意)
替换
listagg(descr, chr(10) || lpad('-', 30, '-') || chr(10))
within group (order by date_of_loan) as book_description
与
所需的输出不是SQL查询的传统输出。你应该在应用程序级别进行重新排列。listag()
可能会让你走到一半,但正如@Gordon所说,如果你真的想要这种格式,应该在你的应用程序代码中完成。Hello@GordonLinoff,感谢您的建议,但我需要为此作业编写SQL查询,以便将数据迁移到其他格式。嘿@TimBiegeleisen,谢谢您的帮助,我将研究Listag(),是的,我确实需要这种格式。我必须编写SQL查询并将数据迁移到Excel文件中,我可以使用ORA_EXCEL从其他表中传输数据,但我无法对这个特定部分执行相同的操作,因为它需要我将数据添加到此格式Hey@mathguy感谢您的帮助,并对延迟回复表示抱歉=D我从未想过以这种方式编写查询,但我无法使用“Listag”由于my Oracle的版本是10g,并且Listag只能在Oracle 12c、Oracle 11g第2版中使用,很抱歉在此之前没有提及。还有别的办法吗?@Laran-我会补充一些答案;我相信Oracle 10中提供了xmlagg
。
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
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