sqlserver 数据横向查询和游标使用
2013-04-26 14:32
357 查看
USE [tempdb]
GO
/****** 对象: StoredProcedure [dbo].[sp_sum] 脚本日期: 04/26/2013 14:36:53 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[sp_sum]
-- Add the parameters for the stored procedure here
AS
declare @iii int
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
-- SET NOCOUNT ON
-- Insert statements for procedure here
--第一步:把数据纳入到表tb_jhsy 手工从excle插入到表
--第二步:把数据插入到表tb_sum
delete from tb_sum
declare @result VARCHAR(30)
Declare curSF Cursor for
select distinct xs from tb_jhsy;---查询语句(查询所有用户存储过程名称)
Open curSF
--循环并提取记录
Fetch Next From curSF Into @result--取第一条记录存入@result中
While ( @@Fetch_Status=0 )
begin
insert into tb_sum(dz,cb,qy,yf,yhyf)
select @result,b.cb,sum(b.qy),sum(b.yf),sum(b.yfy) from (
select a.cb,count(a.cb) as qy,0 as yf,0 as yfy from tb_jhsy a
where a.xs = @result group by a.cb
union all
select a.cb,0 as qy,count(a.cb) as yf,0 as yfy from tb_jhsy a
where a.xs = @result and a.lb like '全员育妇%' group by a.cb
union all
select a.cb,0 as qy,0 as yf,count(a.cb) as yfy from tb_jhsy a
where a.xs = @result and a.lb like '全员育妇已%' group by a.cb
) as b
group by b.cb
Fetch Next From curSF into @result ----下一条
end
--关闭游标
Close curSF
--释放游标
Deallocate curSF
--第三步:统计出需要的数据
declare @s varchar(8000)
set @s='select cb'
select @s=@s+',['+ltrim(rtrim(dz))+'qy]=sum(case ltrim(rtrim(dz)) when '''+ltrim(rtrim(dz))+''' then qy else 0 end)'+',['+ltrim(rtrim(dz))+'yf]=sum(case ltrim(rtrim(dz)) when '''+ltrim(rtrim(dz))+''' then yf else 0 end)'+',['+ltrim(rtrim(dz))+'yhyf]=sum(case
ltrim(rtrim(dz)) when '''+ltrim(rtrim(dz))+''' then yhyf else 0 end )'
from tb_sum
group by dz
set @s=@s+' from tb_sum group by cb '
exec(@s)
END
GO
/****** 对象: StoredProcedure [dbo].[sp_sum] 脚本日期: 04/26/2013 14:36:53 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[sp_sum]
-- Add the parameters for the stored procedure here
AS
declare @iii int
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
-- SET NOCOUNT ON
-- Insert statements for procedure here
--第一步:把数据纳入到表tb_jhsy 手工从excle插入到表
--第二步:把数据插入到表tb_sum
delete from tb_sum
declare @result VARCHAR(30)
Declare curSF Cursor for
select distinct xs from tb_jhsy;---查询语句(查询所有用户存储过程名称)
Open curSF
--循环并提取记录
Fetch Next From curSF Into @result--取第一条记录存入@result中
While ( @@Fetch_Status=0 )
begin
insert into tb_sum(dz,cb,qy,yf,yhyf)
select @result,b.cb,sum(b.qy),sum(b.yf),sum(b.yfy) from (
select a.cb,count(a.cb) as qy,0 as yf,0 as yfy from tb_jhsy a
where a.xs = @result group by a.cb
union all
select a.cb,0 as qy,count(a.cb) as yf,0 as yfy from tb_jhsy a
where a.xs = @result and a.lb like '全员育妇%' group by a.cb
union all
select a.cb,0 as qy,0 as yf,count(a.cb) as yfy from tb_jhsy a
where a.xs = @result and a.lb like '全员育妇已%' group by a.cb
) as b
group by b.cb
Fetch Next From curSF into @result ----下一条
end
--关闭游标
Close curSF
--释放游标
Deallocate curSF
--第三步:统计出需要的数据
declare @s varchar(8000)
set @s='select cb'
select @s=@s+',['+ltrim(rtrim(dz))+'qy]=sum(case ltrim(rtrim(dz)) when '''+ltrim(rtrim(dz))+''' then qy else 0 end)'+',['+ltrim(rtrim(dz))+'yf]=sum(case ltrim(rtrim(dz)) when '''+ltrim(rtrim(dz))+''' then yf else 0 end)'+',['+ltrim(rtrim(dz))+'yhyf]=sum(case
ltrim(rtrim(dz)) when '''+ltrim(rtrim(dz))+''' then yhyf else 0 end )'
from tb_sum
group by dz
set @s=@s+' from tb_sum group by cb '
exec(@s)
END
相关文章推荐
- Oracle 使用函数 function查询数据返回游标
- Sqlserver使用排名函数Row_Number()函数查询指定范围的数据
- 使用SQL语句查询MySQL,SQLServer,Oracle所有数据库名和表名,字段名
- SQLSERVER通过游标查询两个数据表共有字段名组合成的字符串
- SQLSERVER通过游标查询两个数据表共有字段名组合成的字符串
- sqlserver查询表空间大小,使用大小,数据条数
- sqlserver查询表空间大小,使用大小,数据条数
- SQLSERVER通过游标查询两个数据表共有字段名组合成的字符串
- 答:SQLServer DBA 三十问之一: char、varchar、nvarchar之间的区别(包括用途和空间占用);xml类型查找某个节点的数据有哪些方法,哪个效率高;使用存储 过程和使用T-SQL查询数据有啥不一样;
- 关于myBatis与SQLserver 配合使用,查询类型为Date的数据时,日期会减少两天的问题
- 使用sqlserver的游标功能来导数据的常见写法
- sqlserver 使用excle导入数据后查询出来成了org.hibernate.lob.serializablecolb@ff87db的问题。
- SqlServer数据批量模糊查询及使用String.Join串联字符串
- 通过python使用游标查询Elasticsearch数据并写入文件
- Oracle使用游标查询指定数据表的所有字段名称组合而成的字符串
- Oracle使用游标查询指定数据表的所有字段名称组合而成的字符串
- SQLSERVER 多表查询使用/显示多列数据
- 使用游标批量获取数据提高查询性能
- 使用游标查询子部门下的数据
- 注意Sqlserver中使用with(nolock)后实际上还是会加架构锁,只是不对要查询的数据加S索而已(转载)