在论坛中出现的比较难的sql问题:39(动态行转列 动态日期列问题)

原文:在论坛中出现的比较难的sql问题:39(动态行转列 动态日期列问题)


最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了。

所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路。

求一SQL语句。

http://bbs.csdn.net/topics/390496661


  1. create table #tab
  2. (
  3. col1 char(10),
  4. col2 char(10),
  5. item char(10),
  6. num int,
  7. [Date] varchar(10))
  8. insert #tab values('AAA','BBB','A',50,'2013-06-10')
  9. insert #tab values('ABB','BGG','B',30,'2013-06-10')
  10. insert #tab values('AAA','BBB','C',80,'2013-06-13')

在论坛中出现的比较难的sql问题:39(动态行转列 动态日期列问题)

我的解法:


  1. create table tab
  2. (
  3. col1 char(10),
  4. col2 char(10),
  5. item char(10),
  6. num int,
  7. [Date] varchar(10)
  8. )
  9. insert tab values('AAA','BBB','A',50,'2013-06-10')
  10. insert tab values('ABB','BGG','B',30,'2013-06-10')
  11. insert tab values('AAA','BBB','C',80,'2013-06-13')
  12. --动态生成sql语句
  13. declare @start_date varchar(10) = '2013-06-01',
  14. @end_date varchar(10) = '2013-06-30';
  15. declare @date varchar(10),
  16. @sql varchar(max) = '',
  17. @sql1 varchar(8000),
  18. @sql2 varchar(8000);
  19. set @date = @start_date;
  20. set @sql1 = 'select case when rownum = 1 then col1 else '''' end as col1,
  21. case when rownum = 1 then col2 else '''' end as col2,
  22. item'
  23. set @sql2 = 'select col1,col2,item,row_number() over(partition by col1,col2
  24. order by item) as rownum'
  25. while @date <= @end_date
  26. begin
  27. set @sql1 = @sql1 + ',v_' + REPLACE( right(@date,5),'-','') +
  28. ' as ''' + CAST(DATEPART(month,@date) as varchar) + '/' +
  29. CAST(DATEPART(day,@date) as varchar) +'''';
  30. set @sql2 = @sql2 + ',SUM(case when date =''' + @date +
  31. ''' then num else 0 end) as v_' +
  32. REPLACE( right(@date,5),'-','')
  33. set @date = CONVERT(varchar(10),dateadd(day,1,@date),120)
  34. end
  35. set @sql = @sql1 + ' from (' +
  36. @sql2 + ' from tab
  37. group by col1,col2,item' +
  38. ') v'
  39. --生产的动态sql语句
  40. select @sql
  41. exec(@sql)

上面由于是动态生成语句,所以不能用局部的临时表,所以建了一个表。

下面是动态生成的sql语句,经过了格式化:


  1. select case when rownum = 1 then col1 else '' end as col1,
  2. case when rownum = 1 then col2 else '' end as col2,
  3. item,
  4. v_0601 as '6/1',v_0602 as '6/2',v_0603 as '6/3',
  5. v_0604 as '6/4',v_0605 as '6/5',
  6. v_0606 as '6/6',v_0607 as '6/7',
  7. v_0608 as '6/8',v_0609 as '6/9',
  8. v_0610 as '6/10',v_0611 as '6/11',
  9. v_0612 as '6/12',v_0613 as '6/13',
  10. v_0614 as '6/14',v_0615 as '6/15',
  11. v_0616 as '6/16',v_0617 as '6/17',
  12. v_0618 as '6/18',v_0619 as '6/19',
  13. v_0620 as '6/20',v_0621 as '6/21',
  14. v_0622 as '6/22',v_0623 as '6/23',
  15. v_0624 as '6/24',v_0625 as '6/25',
  16. v_0626 as '6/26',v_0627 as '6/27',
  17. v_0628 as '6/28',v_0629 as '6/29',
  18. v_0630 as '6/30'
  19. from
  20. (
  21. select col1,col2,item,
  22. row_number() over(partition by col1,col2 order by item) as rownum,
  23. SUM(case when date ='2013-06-01' then num else 0 end) as v_0601,
  24. SUM(case when date ='2013-06-02' then num else 0 end) as v_0602,
  25. SUM(case when date ='2013-06-03' then num else 0 end) as v_0603,
  26. SUM(case when date ='2013-06-04' then num else 0 end) as v_0604,
  27. SUM(case when date ='2013-06-05' then num else 0 end) as v_0605,
  28. SUM(case when date ='2013-06-06' then num else 0 end) as v_0606,
  29. SUM(case when date ='2013-06-07' then num else 0 end) as v_0607,
  30. SUM(case when date ='2013-06-08' then num else 0 end) as v_0608,
  31. SUM(case when date ='2013-06-09' then num else 0 end) as v_0609,
  32. SUM(case when date ='2013-06-10' then num else 0 end) as v_0610,
  33. SUM(case when date ='2013-06-11' then num else 0 end) as v_0611,
  34. SUM(case when date ='2013-06-12' then num else 0 end) as v_0612,
  35. SUM(case when date ='2013-06-13' then num else 0 end) as v_0613,
  36. SUM(case when date ='2013-06-14' then num else 0 end) as v_0614,
  37. SUM(case when date ='2013-06-15' then num else 0 end) as v_0615,
  38. SUM(case when date ='2013-06-16' then num else 0 end) as v_0616,
  39. SUM(case when date ='2013-06-17' then num else 0 end) as v_0617,
  40. SUM(case when date ='2013-06-18' then num else 0 end) as v_0618,
  41. SUM(case when date ='2013-06-19' then num else 0 end) as v_0619,
  42. SUM(case when date ='2013-06-20' then num else 0 end) as v_0620,
  43. SUM(case when date ='2013-06-21' then num else 0 end) as v_0621,
  44. SUM(case when date ='2013-06-22' then num else 0 end) as v_0622,
  45. SUM(case when date ='2013-06-23' then num else 0 end) as v_0623,
  46. SUM(case when date ='2013-06-24' then num else 0 end) as v_0624,
  47. SUM(case when date ='2013-06-25' then num else 0 end) as v_0625,
  48. SUM(case when date ='2013-06-26' then num else 0 end) as v_0626,
  49. SUM(case when date ='2013-06-27' then num else 0 end) as v_0627,
  50. SUM(case when date ='2013-06-28' then num else 0 end) as v_0628,
  51. SUM(case when date ='2013-06-29' then num else 0 end) as v_0629,
  52. SUM(case when date ='2013-06-30' then num else 0 end) as v_0630
  53. from tab
  54. group by col1,col2,item
  55. ) v

在论坛中出现的比较难的sql问题:39(动态行转列 动态日期列问题)
在论坛中出现的比较难的sql问题:39(动态行转列 动态日期列问题)
发布了416 篇原创文章 · 获赞 135 · 访问量 95万+
上一篇:怎样SQL存储过程中执行动态SQL语句


下一篇:java动态代理框架