计算获取最小值和最大值
程序员文章站
2022-08-10 08:34:32
比如,在下面的销售业绩中,统计业务员的销售业绩中最大值和最小值。 下面是业务数据: CREATE TABLE [dbo].[SalesPerformance]( [ID] [int] IDENTITY(1,1) NOT NULL, [Salesman] NVARCHAR(30) NOT NULL, ......
比如,在下面的销售业绩中,统计业务员的销售业绩中最大值和最小值。
下面是业务数据:
create table [dbo].[salesperformance]( [id] [int] identity(1,1) not null, [salesman] nvarchar(30) not null, [orderdate] [date] null, [sell] decimal(18,2) null ) go
填充数据:
insert into [dbo].[salesperformance] ([salesman],[orderdate],[sell]) values ('s0003','2019-05-12',23800.00), ('s0008','2019-05-19',66528.00), ('s0001','2019-05-05',35455.00), ('s0001','2019-05-18',75220.00), ('s0003','2019-05-17',33658.00), ('s0041','2019-05-10',56300.00), ('s0041','2019-05-11',41811.00), ('s0003','2019-05-20',26309.00) go
使用first_value和last_value函数进行分组查询:
select [id],[salesman],[orderdate],[sell], first_value([sell]) over (partition by [salesman] order by [sell]) [最低销售额], last_value([sell]) over (partition by [salesman] order by [sell]) [最高销售额] from [dbo].[salesperformance]
下一篇: js中this对象用法分析