sql 简单 ---------行转列

来源:互联网 发布:淘宝卖家版怎么注册 编辑:程序博客网 时间:2024/06/07 01:42
USE [dgxt]
GO
/****** Object:  StoredProcedure [dbo].[pro_ReportDate]    Script Date: 06/19/2012 08:32:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[pro_ReportDate]
    @beTime datetime,
    @aftime datetime
AS
BEGIN
    SET NOCOUNT ON;
    -------创建临时表
select * into #ReportAsDate from
(select datepart(yy,ordertime) as Yeard ,
datepart(mm,ordertime) as Monthd,COUNT(*) as OrderNum,SUM(SumMoney) as zjMoney
from En_Orders a,dbo.En_OrderStatus b
where a.Osid=b.Osid
 and DATEDIFF(MONTH,OrderTime,@beTime)<=0 and
 DATEDIFF(MONTH,OrderTime,@aftime)>=0
group by datepart(yy,ordertime),datepart(mm,ordertime))as b
-- Insert statements for procedure here
select * from #ReportAsDate
--行转列
 /*  select
  max(case Monthd  when '1' then OrderNum else 0 end) 一月,
  max(case Monthd when '2' then OrderNum else 0 end) 二月,
  max(case Monthd when '3' then OrderNum else 0 end) 三月,
  max(case Monthd when '4' then OrderNum else 0 end) 四月,
  max(case Monthd when '5' then OrderNum else 0 end) 五月,
  max(case Monthd when '6' then OrderNum else 0 end) 六月,
  max(case Monthd when '7' then OrderNum else 0 end) 七月,
  max(case Monthd when '8' then OrderNum else 0 end) 八月,
  max(case Monthd when '9' then OrderNum else 0 end) 九月,
  max(case Monthd when '10' then OrderNum else 0 end) 十月,
  max(case Monthd when '11' then OrderNum else 0 end) 十一月,
  max(case Monthd when '12' then OrderNum else 0 end) 十二月
  from #ReportAsDate
  group by Yeard*/
END