如何创建一个查询来计算计算列的平均值
How do I create a Query that calculates the adverage of a computed column
我需要创建一个查询,以显示除第一本书以 0.01 的精度出版外,编辑了不止一本书的编辑的平均生产力 pages/day。
我无法找到一种方法来获取自上一 DateOfPublication
列以来的天数,并将其与 NoOfPages
列相除。
要显示的列是
EditorName
BookName
计算列AverageProductivity
这是表格及其列
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
您可以使用window函数lag()
恢复同一编辑器上次发布的日期。
然后,datediff(day, ...)
可以为您提供上一本书和当前一本书的出版日期之间的差异,以天为单位。
最后将当前书页数除以天差,用round()
限制小数位数,大功告成
对于编辑的第一本书,lag()
returns 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
我需要创建一个查询,以显示除第一本书以 0.01 的精度出版外,编辑了不止一本书的编辑的平均生产力 pages/day。
我无法找到一种方法来获取自上一 DateOfPublication
列以来的天数,并将其与 NoOfPages
列相除。
要显示的列是
EditorName
BookName
计算列AverageProductivity
这是表格及其列
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
您可以使用window函数lag()
恢复同一编辑器上次发布的日期。
然后,datediff(day, ...)
可以为您提供上一本书和当前一本书的出版日期之间的差异,以天为单位。
最后将当前书页数除以天差,用round()
限制小数位数,大功告成
对于编辑的第一本书,lag()
returns 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