--´´½¨´æ´¢¹ý³Ì
CREATE PROCEDURE spStatist2 
    @ModalityType varchar(18), 
    @StudySource varchar(18),
    @StatStartDttm datetime,
	@StatEndDttm datetime 
AS
BEGIN
 
DECLARE @sDiagnositician varchar(18)
DECLARE @iStudy_key int
DECLARE @sBodyPart varchar(128)
DECLARE @fCharge money
DECLARE @iPos int
DECLARE @sBodyChild varchar(128)
DECLARE @iLen int

DECLARE crStudy CURSOR FOR
SELECT report_diagnostician, study_key, study_bodypart, study_charge 
FROM study
WHERE study_modalitytype=@ModalityType AND study_source=@StudySource AND report_state > 0 AND
	study_dttm >= @StatStartDttm AND study_dttm <= @StatEndDttm

OPEN crStudy

FETCH NEXT FROM crStudy 
INTO @sDiagnositician,@iStudy_key,@sBodyPart,@fCharge

WHILE @@FETCH_STATUS = 0
BEGIN
  SET @sBodyPart=RTRIM(LTRIM(@sBodyPart))  
  IF LEN(@sBodyPart) > 0
  BEGIN
	  SET @iPos=CHARINDEX(' ',@sBodyPart)          
	  WHILE @iPos>=1       
	  BEGIN    
		SET @sBodyChild=SUBSTRING(@sBodyPart,1,@iPos-1)  
		INSERT INTO CC_Statist (tmp_diagnostician,tmp_BodyPart,BodyPart_Weight)  
			VALUES(@sDiagnositician,@sBodyChild,1)  
		SET @iLen=LEN(@sBodyPart)  
		SET @sBodyPart=SUBSTRING(@sBodyPart,@iPos+1,@iLen)  
		SET @sBodyPart=RTRIM(LTRIM(@sBodyPart))  
		SET @iPos=CHARINDEX(' ',@sBodyPart)  
	  END

	  INSERT INTO CC_Statist (tmp_diagnostician,study_key,tmp_BodyPart,BodyPart_Weight,tmp_charge)    
		VALUES(@sDiagnositician,@iStudy_key,@sBodyPart,1,@fCharge)    
  END
  	
  FETCH NEXT FROM crStudy 
  INTO @sDiagnositician,@iStudy_key,@sBodyPart,@fCharge   
END       

CLOSE crStudy     
DEALLOCATE crStudy

DECLARE crBodyPart CURSOR FOR       
SELECT bodypart_name,CASE WHEN bodypart_charge1>0.0 THEN bodypart_charge1 ELSE 0.0 END       
FROM dd_bodypart WHERE modality_type=@ModalityType AND (bodypart_charge1<1.0 OR bodypart_charge1>1.0)

OPEN crBodyPart      

FETCH NEXT FROM crBodyPart  
INTO @sBodyChild,@fCharge 

WHILE @@FETCH_STATUS=0 
BEGIN 
  UPDATE cc_statist SET BodyPart_Weight=@fCharge WHERE tmp_bodypart=@sBodyChild  
  
  FETCH NEXT FROM crBodyPart  
  INTO @sBodyChild,@fCharge 
END 

CLOSE crBodyPart
DEALLOCATE crBodyPart   

END
