Sql 如何创建计算计算列的adverage的查询

Sql 如何创建计算计算列的adverage的查询,sql,tsql,join,window-functions,calculated-columns,Sql,Tsql,Join,Window Functions,Calculated Columns,我需要创建一个查询来显示编辑的平均工作效率,这些编辑除了第一本以0.01页/天的精度出版的书外,还处理过多本书 我很难找到一种方法来获取自上次DateOfPublication列以来的天数,并使用它将其与NoOfPages列分开 要显示的列包括 EditorName BookName 计算列平均生产率 以下是表格及其列 AGENT AgentID (PK,varchar(11), not null) AgentName (varchar(25), not null) BOOK

我需要创建一个查询来显示编辑的平均工作效率,这些编辑除了第一本以0.01页/天的精度出版的书外,还处理过多本书

我很难找到一种方法来获取自上次
DateOfPublication
列以来的天数,并使用它将其与
NoOfPages
列分开

要显示的列包括

EditorName

BookName

计算列
平均生产率

以下是表格及其列

AGENT  AgentID (PK,varchar(11), not null)
       AgentName (varchar(25), not null)

BOOK   BookName (PK, varchar(45), not null)
       Genre (varchar(25), not null)
       DateOfPublication (date, not null)
       NoOfPages (int, not null)
       WriterID (PK, FK,, varchar(11), not null)
       EditorID (FK, varchar(11), not null)

EDITOR EditorID (PK, varchar(11), not null)
       EditorName (varchar(25), not null)
       Mentors_EditorID (FK, varchar(11), null)

WRITER WriterID (PK, varchar(11), not null)
       WriterName (varchar(25), not null)
       AgentID (FK, varchar(11), not null)

样本数据

insert into BOOK (BookName, WriterID, Genre, DateOfPublication, NoOfPages, EditorID)
values ('Valley of Heroes','10','Fiction','2010-01-12',874,'20');
insert into BOOK (BookName, WriterID, Genre, DateOfPublication, NoOfPages, EditorID)
values ('The Ruler''s Return','11','Fantasy','2012-03-14',765,'22');
insert into BOOK (BookName, WriterID, Genre, DateOfPublication, NoOfPages, EditorID)
values ('eRobot','11','Fantasy','2011-04-15',264,'20');
insert into BOOK (BookName, WriterID, Genre, DateOfPublication, NoOfPages, EditorID)
values ('An Uncle''s Letters','12','Fiction','2012-06-12',258,'20');
insert into BOOK (BookName, WriterID, Genre, DateOfPublication, NoOfPages, EditorID)
values ('Pretty flowers','13','Album','2013-01-31',148,'22');
insert into BOOK (BookName, WriterID, Genre, DateOfPublication, NoOfPages, EditorID)
values ('A Tale of Lions','12','Fantasy','2012-08-17',301,'21');
insert into BOOK (BookName, WriterID, Genre, DateOfPublication, NoOfPages, EditorID)
values ('eRobot','13','Sci Fi','2012-10-04',465,'23');
查询现在正在生成正确的列,这要归功于GMB,但计算列显示0个值

这里是查询

select * from (
    select 
    e.EditorName,
    b.BookName,
    round(
        NoOfPages/datediff(
            day, 
            lag(b.DateOfPublication) over(partition by b.EditorID order by b.DateOfPublication),
            DateOfPublication
        ),
        2
    ) AverageProductivity       
from book b
inner join editor e on e.EditorID = b.EditorID 
) x where AverageProductivity is not null
结果

Melanie eRobot  0
Melanie An Uncle's Letters  0
George  Pretty flowers  0

您可以使用窗口函数
lag()
恢复同一编辑器上一次发布的日期

然后,
datediff(day,…)
可以提供上一本书和当前一本书的出版日期之间的差值,以天为单位

最后,将当前书籍的页数除以日差,使用
round()
限制小数位数,就完成了

对于编辑器的第一本书,
lag()
返回
null
,这将在计算中传播,导致计算列也显示
null

select 
    e.EditorName,
    b.BookName,
    round(
        NoOfPages/datediff(
            day, 
            lag(b.DateOfPublication) over(partition by b.EditorID order by b.DateOfPublication),
            DateOfPublication
        ),
        2
    ) AverageProductivity       
from book b
inner join editor e on e.EditorID = b.EditorID 
如果要跳过与每个编辑器的第一本书对应的记录,则可以包装查询:

select * from (
    -- above query
) x where AverageProductivity is not null

示例数据和期望的结果将非常有用。我将添加一些示例数据和结果请在代码问题中给出一个--cut&paste&runnable代码,包括作为代码输入的最小代表性示例;期望和实际输出(包括逐字记录错误消息);标签和版本;清晰的说明和解释。尽可能少地给出代码,即显示为OK的代码,并通过显示为not OK的代码进行扩展。(调试基础。)用于包含DBMS和DDL(包括约束和索引)的SQL,并将其作为格式化为表的代码输入。暂停对总体目标的工作,将代码切掉到第一个表达式,没有给出您期望的内容,说出您期望的内容和原因。好的,我运行了它,现在我看到了正确的编辑器,但是AverageProductivity列显示了0表示他们期望的内容productivity@YHapticY:我看到的唯一原因是除法返回的值小于0.01(或者页数为0)。您可以删除周围的round()并查看结果。此外,您还可以通过将datediff()和NoOfPages放在结果集中的分隔列中来查看它们返回的内容。当我尝试删除round()时,我得到“没有为x的第3列指定列名”