一个利用创建临时表后插入数据的查询

 
DECLARE @begin varchar(10) set @begin=GETDATE()
DECLARE @end varchar(10)   set @end=GETDATE()
if object_id('tmpPeisExamStat') is  null 
Begin
   CREATE TABLE tmpPeisExamStat(
  QueryDate nvarchar(10) COLLATE   Chinese_PRC_CI_AS,
  QueryBranchID int,
  QueryBranchName nvarchar(50) COLLATE   Chinese_PRC_CI_AS,
  PreRegQty int DEFAULT 0,
  TeamRegQty int DEFAULT 0,
  RegTotal as PreRegQty+TeamRegQty,
  PreExamQty int DEFAULT 0,
  TeamExamQty int DEFAULT 0,
  ExamTotal as PreExamQty+TeamExamQty,
  PrePrintQty int DEFAULT 0,
  TeamPrintQty int DEFAULT 0,
  PrintTotal as PrePrintQty +TeamPrintQty)
    
End
ELSE
BEGIN
   truncate table tmpPeisExamStat
END
DECLARE @tmpDate varchar(10)
 SET @tmpDate = @begin
WHILE(@tmpDate <= @end)
BEGIN
   INSERT INTO tmpPeisExamStat(QueryDate,QueryBranchID,QueryBranchName)
   SELECT Convert(varchar(10),@tmpDate,111),BranchID,BranchName FROM PLBranch
   set @tmpDate = (SELECT Convert(varchar(10),DATEADD(Day,1,@tmpDate),111))
END

UPDATE tmpPeisExamStat SET PreRegQty = (SELECT COUNT(*) from PEISRegister
WHERE CancelJudge=0 AND RegisterDate=QueryDate AND BranchID=QueryBranchID AND <<{3}>>  AND UnitRegisterID IS NULL),
TeamRegQty = (SELECT COUNT(*) from PEISRegister
WHERE CancelJudge=0 AND RegisterDate=QueryDate AND BranchID=QueryBranchID  AND <<{3}>> AND UnitRegisterID IS NOT NULL),
PreExamQty = (SELECT COUNT(*) from PEISRegister
WHERE CancelJudge=0 AND ExamDate=QueryDate AND BranchID=QueryBranchID  AND <<{3}>> AND UnitRegisterID IS NULL),
TeamExamQty = (SELECT COUNT(*) from PEISRegister
WHERE CancelJudge=0 AND ExamDate=QueryDate AND BranchID=QueryBranchID  AND <<{3}>> AND UnitRegisterID IS NOT NULL),
PrePrintQty = (SELECT COUNT(*) from PEISPatientConclusion con 
 LEFT JOIN PEISRegister reg ON con.RegisterID = reg.RegisterID 
WHERE con.PrintDate=QueryDate AND BranchID=QueryBranchID AND reg.CancelJudge=0 AND <<{3}>> AND reg.UnitRegisterID IS NULL),
TeamPrintQty = (SELECT COUNT(*) from PEISPatientConclusion con 
 LEFT JOIN PEISRegister reg ON con.RegisterID = reg.RegisterID 
WHERE con.PrintDate=QueryDate AND BranchID=QueryBranchID AND reg.CancelJudge=0  AND <<{3}>> AND reg.UnitRegisterID IS NOT NULL)
select * from tmpPeisExamStat where 1=1  
AND <<{2}>>  ORDER BY QueryDate,QueryBranchID


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值