计算获取最小值和最大值

比如,在下面的销售业绩中,统计业务员的销售业绩中最大值和最小值。

下面是业务数据:

 

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
Source Code


填充数据:

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
Source Code

 

使用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]
Source Code

 

posted @ 2019-05-28 14:56 Insus.NET 阅读(...) 评论(...) 编辑 收藏