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

 

查询结果:

 

 

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

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


查询结果:

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

 

posted @ 2013-04-08 19:56  程序员--靖  阅读(999)  评论(0编辑  收藏  举报