存储过程--虚拟表

 一般用于报表,前提:报表中部分是从一些表中查询而得,而另一部分是通过各种计算而得,这种情况下,可用此存储过程。纯属个人记录,不供大家学习。

如下:

USE [changyuan]

GO

/****** Object:  StoredProcedure [dbo].[sp_adv_qy_jpdata_Select_byDates]    Script Date: 05/19/2013 21:52:49 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

 

ALTER PROC [dbo].[sp_adv_qy_jpdata_Select_byDates] --[sp_adv_qy_jpdata_Select_byDates] "2006-11-6", "2006-11-13"  ,5         

(             

@time1 datetime,             

@time2 datetime=null,             

@qy int=null                    

)              

as

--天数     

declare @i int   

set @i=0 

--select @i= DATEDIFF(day,@time1,@time2)     

--set @i=@i+1   

--print @i   

 

--表1 显示最终结果    

CREATE TABLE #TABLE            

(argname nvarchar(100) , lshds int DEFAULT ((0)), renshu int DEFAULT ((0)),ytjs int DEFAULT ((0)), ywcs int DEFAULT ((0)), wwcs int DEFAULT ((0)), wcl nvarchar(100) DEFAULT ((0)),id int )           

 

--插入所有分公司 

insert into #TABLE (id,argname) 

select id,argname from tb_area where fatherid='1' 

 

--分公司所有店的提交数 

CREATE TABLE #TABLE1            

(eid int, qy int)  

 

--时间段内的所有周一 

set @time1=(select(convert(datetime,@time1)-datepart(dw,convert(datetime,@time1))+2))  

  

while(@time1 <=@time2)         

begin    

 

if((select datepart(dw,@time1))=2)   

begin   

 

insert into #TABLE1 

select distinct eid, (select fatherid from tb_area where id=(select fatherid from tb_area where id=(select lshfgs from tb_rt where lshid=shop))) as qy from jpdata left join employee on jpdata.eid=employee.employeeid where employee.available=1 and salesdate = @time1 

 

--select distinct salesdate, (select fatherid from tb_area where id=(select fatherid from tb_area where id=(select lshfgs from tb_rt where lshid=shop))) as qy from jpdata where salesdate = @time1 

 

set @time1=DateADD(day,1,@time1) 

set @i=@i+1      

end    

 

 

 

 

set @time1=@time1+1   

end       

print @i     

      

--select * from #TABLE1 

--岗位人数   

update #TABLE set renshu=isnull((select count(*) from employee where (available=1 or (available=0 and lzhshj > ''+cast(''+@time2 + ' 23:59' as varchar)+'')) and  employee.qy=#TABLE.id ),0) from employee where  available=1 and  employee.qy=#TABLE.id 

 

--零售店数   

update #TABLE set lshds=isnull((select count(*) from tb_rt where  lshq=#TABLE.id ),0) from tb_rt where lshq=#TABLE.id   

 

--应提交数     

update #TABLE set ytjs=renshu*@i  

     

--已提交数     

update #table set ywcs=(select count(*) from #table1 where #TABLE1.qy=#table.id) from #TABLE1 where #table1.qy=#table.id 

     

--未提交数     

update #TABLE set wwcs=(ytjs-ywcs)     

     

--提交完成率     

update #TABLE set wcl=round(convert(float,ywcs)*100/(CASE WHEN ytjs=0 THEN 1 ELSE ytjs END) ,2)     

      

if @qy is not null and @qy <>''   

begin    

select * from #table where id=@qy   

end   

else   

begin   

select * from #table  

end     

--drop table #table 

--drop table #table1

posted on   秋天  阅读(1192)  评论(0编辑  收藏  举报

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2009-05-19 如何定时重起操作系统.

导航

< 2013年5月 >
28 29 30 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示