在论坛中出现的比较难的sql问题:19(row_number函数 行转列、sql语句记流水)

原文: 在论坛中出现的比较难的sql问题:19(row_number函数 行转列、sql语句记流水)

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

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



1、SQLServer 把表的挨着的四条数据合并到一起 

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



数据:
a          a         
 b          b         
 c          c         
 d          d         
 e          e         
 f          f         
 g          g         
 h          h         
 i              i  


我的方法:


   
   
  1. --drop table t
  2. create table t( name varchar( 10),pass varchar( 10))
  3. insert into t
  4. select 'a', 'a' union all
  5. select 'b', 'b' union all
  6. select 'c', 'c' union all
  7. select 'd', 'd' union all
  8. select 'e', 'e' union all
  9. select 'f', 'f' union all
  10. select 'g', 'g' union all
  11. select 'h', 'h' union all
  12. select 'i', 'i'
  13. go
  14. select MAX( case when rn = 1 then name else null end) name,
  15. MAX( case when rn = 1 then pass else null end) pass,
  16. MAX( case when rn = 2 then name else null end) name,
  17. MAX( case when rn = 2 then pass else null end) pass,
  18. MAX( case when rn = 3 then name else null end) name,
  19. MAX( case when rn = 3 then pass else null end) pass,
  20. MAX( case when rn = 4 then name else null end) name,
  21. MAX( case when rn = 4 then pass else null end) pass
  22. from
  23. (
  24. select *,
  25. ROW_NUMBER() over( partition by rownum order by getdate()) rn
  26. from
  27. (
  28. select *,
  29. (ROW_NUMBER() over( order by getdate()) -1) / 4 rownum
  30. from t
  31. )t
  32. )t
  33. group by rownum
  34. /*
  35. name pass name pass name pass name pass
  36. a a b b c c d d
  37. e e f f g g h h
  38. i i NULL NULL NULL NULL NULL NULL
  39. */


2、怎么用简单的sql语句记流水?

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


有一个流水表TF,字段有ID,FirstQuantity,ChangeQuantity,FinalQuantity。ID表示物品,后面几个表示期初数量,变化数量,最终数量。
假设表TF现在是空
有一张变动表TC,字段有ID,Quantity。表示某物品的数量。里面会有重复。内容如下:
ID Quantity
1  10
1  20
1  30
那么当我把TC的数据加入到TF后,TF的内容应该如下:


   
   
  1. ID FirstQuantity ChangeQuantity FinalQuantity
  2. 1 0 10 10
  3. 1 10 20 30
  4. 1 30 30 60
这个功能,用编程的方法很好解决,就是一个一个循环写入,但是效率太慢了。
那么能不能用一条sql语句就搞定呢?

我的方法:


   
   
  1. create table tc( ID int, Quantity int)
  2. insert into tc
  3. select 1 , 10 union all
  4. select 1 , 20 union all
  5. select 1 , 30
  6. go
  7. ;with t
  8. as
  9. (
  10. select *,
  11. ROW_NUMBER() over( partition by id order by @@servername) rownum
  12. from tc
  13. )
  14. select ID,
  15. FirstQuantity,
  16. ChangeQuantity,
  17. FirstQuantity+ChangeQuantity as inalQuantity
  18. from
  19. (
  20. select ID,
  21. case when rownum = 1 then 0
  22. else ( select SUM(Quantity) from t t2
  23. where t2.ID = t1.id and t2.rownum < t1.rownum)
  24. end as FirstQuantity,
  25. Quantity as ChangeQuantity
  26. from t t1
  27. )tt
  28. /*
  29. ID FirstQuantity ChangeQuantity inalQuantity
  30. 1 0 10 10
  31. 1 10 20 30
  32. 1 30 30 60
  33. */




发布了416 篇原创文章 · 获赞 135 · 访问量 94万+

猜你喜欢

转载自www.cnblogs.com/lonelyxmas/p/12020020.html
今日推荐