Sql 怎样将某个列转为行,实…

 
--如下图,代码的任务就是将图一转为图二
--此功能在PB中的交叉报表也可实现
--编写此段代码是因为今天遇到客户有这个需求,自己觉得可以挑战一下,就尝试做下来了
 
 
Sql <wbr>怎样将某个列转为行,实现交叉报表的功能
Sql <wbr>怎样将某个列转为行,实现交叉报表的功能

代码如下:
 
  1. create   table  #patient_result_temp (
  2. patient_code  nvarchar ( 100 ),
  3. date_test  datetime ,
  4. item_code  nvarchar ( 100 ),
  5. test_value2  nvarchar ( 100 )
  6. )
  7. create   table  #patient_result_target (
  8. patient_code  nvarchar ( 100 ),
  9. date_test  datetime ,
  10. RBC  nvarchar ( 50 ),
  11. WBC  nvarchar ( 50 ),
  12. PLT  nvarchar ( 50 )
  13. )
  14.  
  15.  
  16. insert   into  #patient_result_temp  select  a . patient_code , a . date_test , b . item_code , b . test_value2  from  ut_check_patient a , ut_check_result  where  a . date_test  =  b . test_date  and  a . ybh  =  b . ybh  and  item_code  in   ( 'RBC' , 'WBC' , 'PLT' )   and  a . date_test  > '2013-10-21'   and  a . patient_code  > ''
  17.  
  18. declare  my_cursor  cursor  
  19. for   select   *   from  #patient_result_temp
  20. open  my_cursor
  21. declare  @p_code  nvarchar ( 100 ), @d_test  datetime , @i_code  nvarchar ( 100 ), @t_value2  nvarchar ( 100 )
  22. fetch  next  from  my_cursor  into  @p_code , @d_test , @i_code , @t_value2
  23. while   ( @@fetch_status   =  0 )
  24.     begin
  25.  
  26.      if  @i_code  =   'RBC'  
  27.         begin
  28.         
  29.            if   ( select   count (*)   from  #patient_result_target  where  patient_code  =  @p_code  and  date_test  =  @d_test )   <=  
  30.               begin
  31.                insert   into  #patient_result_target  values ( @p_code , @d_test , @t_value2 , '' , '' )
  32.               end
  33.            else
  34.               begin
  35.                  update  #patient_result_target
  36.                  set  RBC  =  @t_value2
  37.                  where  patient_code  =  @p_code  and  date_test  =  @d_test
  38.               end
  39.           end
  40.  
  41.      if  @i_code  =   'WBC'  
  42.            begin
  43.          
  44.            if   ( select   count (*)   from  #patient_result_target  where  patient_code  =  @p_code  and  date_test  =  @d_test )   <=  
  45.               begin
  46.                insert   into  #patient_result_target  values ( @p_code , @d_test , @t_value2 , '' , '' )
  47.               end
  48.            else
  49.               begin
  50.                  update  #patient_result_target
  51.                  set  WBC  =  @t_value2
  52.                  where  patient_code  =  @p_code  and  date_test  =  @d_test
  53.               end
  54.           end
  55.    if  @i_code  =   'PLT'  
  56.          begin
  57.          
  58.            if   ( select   count (*)   from  #patient_result_target  where  patient_code  =  @p_code  and  date_test  =  @d_test )   <=  
  59.               begin
  60.                insert   into  #patient_result_target  values ( @p_code , @d_test , '' , '' , @t_value2 )
  61.               end
  62.            else
  63.               begin
  64.                  update  #patient_result_target
  65.                  set  PLT  =  @t_value2
  66.                  where  patient_code  =  @p_code  and  date_test  =  @d_test
  67.               end
  68.           end
  69.  
  70.  
  71.  
  72.     fetch  next  from  my_cursor  into  @p_code , @d_test , @i_code , @t_value2
  73.   end
  74.   
  75.  
  76. close  my_cursor
  77. deallocate  my_cursor
  78.  
  79. select   *   from  #patient_result_target 
  80. drop   table  #patient_result_target
  81. drop   table  #patient_result_temp
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值