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