sql 实现一张表中查询雇员的各个季度的销售总量

创建Sales表:

CREATE TABLE [dbo].[Sales](
    [OrderId] [char](10) PRIMARY KEY NOT NULL ,
    [Quantity] [int] NULL,
    [OrderDate] [datetime] NULL,
    [Employee] [char](10) NULL
    )
GO

 


查询第一季度的销售总量:

select Employee as '雇员',
       '第一季度' as '季度',
       Sum(Case when month(OrderDate) in (1, 2, 3) then Quantity else 0 end) as '销售量'
  from dbo.Sales
 group by Employee

 

查询结果:

sql 实现一张表中查询雇员的各个季度的销售总量_第1张图片

 

 

查询四个季度的各个雇员的销售总量

Select Sum(Case when month(OrderDate) in (1,2,3) then Quantity end) as '第一季度',
      Sum(Case when month(OrderDate) in (4,5,6) then Quantity end) as '第二季度',
      Sum(Case when month(OrderDate) in (7,8,9) then Quantity end) as'第三季度',
      Sum(Case when month(OrderDate) in (10,11,12) then Quantity end) as '第四季度',
      Employee as '雇员'
     from Sales
     group by Employee


查询结果:

sql 实现一张表中查询雇员的各个季度的销售总量_第2张图片

仅供交流使用,如果大家还有更好的查询方法,一起分享!

 

转载于:https://www.cnblogs.com/huangjing/archive/2013/04/08/3008728.html

你可能感兴趣的:(sql 实现一张表中查询雇员的各个季度的销售总量)