1

我有一个 SQL Server 2005 表,其中存储了两个学生之间发生的书籍交换。

ExchangeID   BookID   ExchangeDate   FromPersName   ToPersName
1            23       23.12.2011     John           Matt
2            22       15.02.2012     Billy          Ken
3            23       27.12.2011     Matt           Riddley
5            23       05.03.2012     Riddley        Josh
6            22       08.03.2012     Ken            Rachel
7            23       19.03.2012     Josh           Laura
8            23       15.01.2013     Laura          Mike
9            22       17.01.2013     Rachel         Stephanie

我想生成一个指定年份的报告,如下所示:

Year:2012

BookID    PersonName      ReceivingDate   DeliveryDate
23        Matt            01.01.2012      27.02.2012
23        Riddley         27.02.2012      05.03.2012
23        Josh            05.03.2012      19.03.2012
23        Laura           19.03.2012      31.12.2012
22        Ken             01.01.2012      08.03.2012
22        Rachel          08.03.2012      31.12.2012
4

2 回答 2

0

表可以自己加入

select ex1.BookID,ex1.toPersName,ex1.ExchangeDate as DeliveryDate,Exchanges.ExchangeDate    
as ReceivingDate from Exchanges as ex1 inner join Exchanges on      
ex1.toPersName=Exchanges.FromPersName
于 2013-01-16T08:47:25.663 回答
0

这是一半的解决方案。你需要稍微改变一下才能工作。我不会对你的作业做完整的询问。用点力吧!

http://sqlfiddle.com/#!3/39d73/17

于 2013-01-16T09:29:13.070 回答