Sql server 如何截断计算列中的小数?

Sql server 如何截断计算列中的小数?,sql-server,precision,calculated-columns,Sql Server,Precision,Calculated Columns,我需要创建一个查询来显示编辑的平均工作效率,这些编辑除了第一本以0.01页/天的精度出版的书外,还处理过多本书 我现在显示了正确的列,但我需要减少AverageProductivity列中显示的零的数量 要显示的列包括 EditorName BookName 计算列平均生产率 以下是表格及其列 AGENT AgentID (PK,varchar(11), not null) AgentName (varchar(25), not null) BOOK BookName (P

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

我现在显示了正确的列,但我需要减少
AverageProductivity
列中显示的零的数量

要显示的列包括

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');
我曾尝试使用格式语法来更改精度,但我不确定它的语法是否正确

这里是查询

use SignumLibri_01
select * from (
    select e.EditorName, b.BookName,
       round(NoOfPages * 1.0
                 / 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
有没有办法截断所有多余的零,使我的精度达到0.00

结果

Melanie eRobot  0.580000000000
Melanie An Uncle's Letters  0.610000000000
George  Pretty flowers  0.460000000000

您需要将数据类型更改为
DECIMAL(X,2)
ROUND
函数实际上并不更改数据类型,只是从特定位置开始截断或舍入值

CONVERT(
    DECIMAL(10, 2),
    round(NoOfPages * 1.0 /datediff(
            day, 
            lag(b.DateOfPublication) over(partition by b.EditorID order by b.DateOfPublication),
            DateOfPublication),
        2))
还请注意,在转换为
十进制时,该值会自动舍入,因此实际上不必调用
舍入
函数(除非要截断小数)

结果:

Original    RoundedAt2  RoundedAt4  ConvertedToDecimal2 ConvertedToDecimal4
2.631578    2.630000    2.631600    2.63                2.6316

您可以看到舍入值和十进制值匹配(不是在比例和精度上,而是在内容上)。

您需要将数据类型更改为
decimal(X,2)
ROUND
函数实际上并不更改数据类型,只是从特定位置开始截断或舍入值

CONVERT(
    DECIMAL(10, 2),
    round(NoOfPages * 1.0 /datediff(
            day, 
            lag(b.DateOfPublication) over(partition by b.EditorID order by b.DateOfPublication),
            DateOfPublication),
        2))
还请注意,在转换为
十进制时,该值会自动舍入,因此实际上不必调用
舍入
函数(除非要截断小数)

结果:

Original    RoundedAt2  RoundedAt4  ConvertedToDecimal2 ConvertedToDecimal4
2.631578    2.630000    2.631600    2.63                2.6316

您可以看到舍入值和十进制值匹配(不是在比例和精度上,而是在内容上)。

您可以使用CONVERT(十进制(10,2),您的列)。此外,通常在表示层上进行格式化,以在整个过程中保持数据完整性(舍入过早)。例如,您希望.5876和.5855的排序顺序是什么?您可以使用CONVERT(十进制(10,2),您的列)。此外,通常在表示层上进行格式化,以在整个过程中保持数据完整性(过早舍入)。例如,您希望.5876和.5855的排序顺序是什么