Sql 怎样将某个列转为行,实…
来源:互联网 发布:蜂蜜和四叶草 知乎 编辑:程序博客网 时间:2024/06/06 07:28
--如下图,代码的任务就是将图一转为图二
--此功能在PB中的交叉报表也可实现
--编写此段代码是因为今天遇到客户有这个需求,自己觉得可以挑战一下,就尝试做下来了
代码如下:
- create
table #patient_result_temp ( - patient_code
nvarchar(100), - date_test
datetime, - item_code
nvarchar(100), - test_value2
nvarchar(100) - )
- create
table #patient_result_target ( - patient_code
nvarchar(100), - date_test
datetime, - RBC
nvarchar(50), - WBC
nvarchar(50), - PLT
nvarchar(50) - )
- insert
into #patient_result_temp selecta .patient_code,a.date_test,b.item_code,b.test_value2from ut_check_patient ,ut_check_resulta b wherea .date_test= b .test_dateand a .ybh= b .ybhand item_code in('RBC','WBC','PLT') and a .date_test>'2013-10-21' and a .patient_code>'' - declare
my_cursor cursor - for
select * from #patient_result_temp - open
my_cursor - declare
@p_code nvarchar(100),@d_testdatetime,@i_code nvarchar(100),@t_value2 nvarchar(100) - fetch
next frommy_cursor into@p_code ,@d_test,@i_code,@t_value2 - while
(@@fetch_status = 0 ) @i_code ='RBC' (select count(*) from #patient_result_target wherepatient_code =@p_code anddate_test =@d_test )<= 0 into #patient_result_target values(@p_code,@d_test,@t_value2,'','')#patient_result_target RBC =@t_value2 patient_code =@p_code anddate_test =@d_test @i_code ='WBC' (select count(*) from #patient_result_target wherepatient_code =@p_code anddate_test =@d_test )<= 0 into #patient_result_target values(@p_code,@d_test,@t_value2,'','')#patient_result_target WBC =@t_value2 patient_code =@p_code anddate_test =@d_test @i_code ='PLT' (select count(*) from #patient_result_target wherepatient_code =@p_code anddate_test =@d_test )<= 0 into #patient_result_target values(@p_code,@d_test,'','',@t_value2)#patient_result_target PLT =@t_value2 patient_code =@p_code anddate_test =@d_test next frommy_cursor into@p_code ,@d_test,@i_code,@t_value2end - close
my_cursor - deallocate
my_cursor - select
* from #patient_result_target - drop
table #patient_result_target - drop
table #patient_result_temp
0 0
- Sql 怎样将某个列转为行,实…
- listagg函数将查询列转为行
- SQL Server怎样获得某个表的列名
- 行转为列
- MySql列转为行
- 将sql表中的数据多行动态的转为多列
- 【symbian】怎样将程序转为后台程序
- 怎样能完整将PDF转为PPT
- 怎样使用PS将彩色图片转为黑白图片
- Java – 怎样将 Array 转为 Stream
- SQL SERVER将某一列字段中的某个值替换为其他的值
- SQL Server将列值转为列名输出
- sql如何将列转化为行
- SQL 查询怎么将行变成列
- sql 将行显示为列
- sql将列显示为行
- SQL SERVER 将行转成列值
- 怎样查询SQL数据库中某一个表中的某个列的一个数值的所有行数据
- 逢三退一 算法
- Java 按规律将字符串分割为do…
- Java 容器 collection hashCode
- Java 集合排序方法 集合…
- Java 泛型 Generic
- Sql 怎样将某个列转为行,实…
- Java IO 流 stream
- Java 线程 Thread Runnable
- DOM解析xml
- SAX解析xml
- Java解析虾米音乐的真实下载地址
- 关于Linux accpet()的错误处理
- 非主流数据库Firebird
- protel布局布线