Chinaunix首页 | 论坛 | 博客
  • 博客访问: 75116
  • 博文数量: 15
  • 博客积分: 305
  • 博客等级: 二等列兵
  • 技术积分: 140
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-10 11:42
文章分类

全部博文(15)

文章存档

2013年(2)

2012年(7)

2011年(1)

2010年(1)

2009年(4)

我的朋友

分类: Mysql/postgreSQL

2009-11-10 11:56:28

MYSQL 销售数据按日期行转换为列 语句:

CREATE  PROCEDURE `PROC_GET_SUM_Report`(
tag char(1),
btime varchar(10),
etime varchar(10))
BEGIN
      declare $stm varchar(8000);
      declare $rowcnt int;
      declare $mycnt int;
      declare $i int;
      set $mycnt = 0;
      set $i = 1;
       set $stm='select storeno as 编号,storename as 店名,estorename as 店英文名';
if tag='0' then
select count(distinct workdate) into $rowcnt from xa_dg_temp_emp_sum_cross where DATE_SUB(CURDATE(), INTERVAL 1 MONTH) <= date(workdate);
      WHILE $i<=$rowcnt DO
         set @i :=0;
          select col from (select (@i := @i +1 ) as iden,CONCAT($stm,',SUM(case workdate when ''',workdate,''' then slmny else 0 end) as ''',workdate,'''') as col from (select distinct workdate from xa_dg_temp_emp_sum_cross where DATE_SUB(CURDATE(), INTERVAL 1 MONTH) <= date(workdate)) as tb) as tb1 where iden=$i into $stm;
      Set $i:=$i+1;
      END WHILE;
end if;
if tag='1' then
select count(distinct workdate) into $rowcnt from xa_dg_temp_emp_sum_cross where workdate>=btime and workdate<=etime;
      WHILE $i<=$rowcnt DO
         set @i :=0;
          select col from (select (@i := @i +1 ) as iden,CONCAT($stm,',SUM(case workdate when ''',workdate,''' then slmny else 0 end) as ''',workdate,'''') as col from (select distinct workdate from xa_dg_temp_emp_sum_cross where workdate>=btime and workdate<=etime) as tb) as tb1 where iden=$i into $stm;
      Set $i:=$i+1;
      END WHILE;
end if;
     Set @stm=concat($stm,' from xa_dg_temp_emp_sum_cross where paycode=''01'' group by storeno,storename,estorename order by storeno;');
     prepare s from @stm;
    execute s;
    deallocate prepare s;
        END

原始数据图:

行转换列后效果图:


 

阅读(1543) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:海量数据分页示例

给主人留下些什么吧!~~