Click here to Skip to main content
15,994,549 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is the stored procedure i have

SQL
DELIMITER $$

USE `hadoopdb_other`$$

DROP PROCEDURE IF EXISTS `StatisticalData`$$

CREATE DEFINER=`root`@`%` PROCEDURE `StatisticalData`(
IN keywords VARCHAR(100),
IN tablename VARCHAR(100)
)
BEGIN
 SET @keyword= keywords;
 SET @tablename=tablename;
 SET @v1 := REPLACE(@keyword , ' ','');
 SET @v2 := REPLACE(@tablename , ' ','');
 SET @v3 := CONCAT(@v1,'_info');
 SET @v4 := CONCAT(@v2,'_Populatefilteroptions');
 
 SET @sdate= CONCAT('select Min(DATE_FORMAT(date_time,''%Y-%m-%d'')) into @var1 from ',tablename);
 SET @edate= CONCAT('select max(DATE_FORMAT(date_time,''%Y-%m-%d'')) into @var2 from ',tablename);
 SET @startdate= @var1;
 SET @enddate= @var2;
 SET @drop = CONCAT ('drop table if exists ',@v3);
 SET @createtable = CONCAT('create table ',@v3,' (CreatedDate varchar(200),KeyCount bigint,Source varchar(200),
   Keyword VARCHAR(200),Positive bigint,Negative BIGINT,Neutral BIGINT,PositiveScore double,
   NegativeScore DOUBLE, NeutralScore DOUBLE,PosStrength DOUBLE,NegStrength DOUBLE,NeuStrength DOUBLE,Total bigint)');
   
        SET @drop1 = CONCAT ('drop table if exists ',@v4);
 SET @createtable1 = CONCAT('create table ',@v4,'(gender varchar(200),country  varchar(200),product_name varchar(200),
             source VARCHAR(200),date_time VARCHAR(200))');
 
 SET @isKeywordsExists="";
 SET @keyCnt=0;
 SET @keyAt="";
 SET @tempKeyStr="";
 
 
        
 IF keywords <> "" THEN
          SET @tempKeyStr=keywords;
          SET @keyCnt=ROUND ( ( LENGTH(@tempKeyStr) - LENGTH( REPLACE ( @tempKeyStr, ",", "") ) ) / LENGTH(","))+1;
        
         WHILE @keyCnt>0 DO
           
           
            SET @keyAt = REPLACE(SUBSTRING(SUBSTRING_INDEX(keywords, ',', @keyCnt),
       LENGTH(SUBSTRING_INDEX(keywords, ',', @keyCnt-1)) + 1),
       ',', '');
            
            SET @isKeywordsExists=CONCAT(@isKeywordsExists,",SUM(IF (product_name='",@keyAt,"',1,0)) AS `",@keyAt,"`");
            SET @keyCnt=@keyCnt-1;
          END WHILE;
        END IF; 
       SET@tr=CONCAT('TRUNCATE ', @v3);
       
  SET @t1 = CONCAT("insert into ",@v3," (CreatedDate,KeyCount,Source,Keyword,Positive,Negative,Neutral,
                   PositiveScore,NegativeScore,NeutralScore,PosStrength,NegStrength,NeuStrength,Total)
 select * from (
        SELECT DATE_FORMAT( STR_TO_DATE(date_time,'%Y-%m-%d %T'),'%Y-%m-%d') AS createdDate
        ",@isKeywordsExists,",source,product_name AS keyword,
        SUM(IF (sentiment_score>0,1,0)) AS Positive,
 SUM(IF (sentiment_score<0,1,0)) AS Negative,
 SUM(IF (sentiment_score=0,1,0)) AS Neutral,
 ROUND(SUM(IF (sentiment_score>0,positive,0)),2) as PositiveScore,
 ROUND(SUM(IF (sentiment_score<0,negative,0)),2) as NegativeScore,
 ROUND(SUM(IF (sentiment_score=0,neutral,0)),2) as NeutralScore,
 COALESCE(ROUND(SUM(IF (sentiment_score>0,positive,0))*100/SUM(IF (sentiment_score>0,1,0)),2),0) AS PosStrength, 
 COALESCE(ROUND(SUM(IF (sentiment_score<0,negative,0))*100/SUM(IF (sentiment_score<0,1,0)),2),0) AS NegStrength, 
 COALESCE(ROUND(SUM(IF (sentiment_score=0,neutral,0))*100/SUM(IF (sentiment_score=0,1,0)),2),0) AS NeuStrength, 
 COUNT(*) AS total
        FROM ",tablename," 
        WHERE (DATE_FORMAT( STR_TO_DATE(date_time,'%Y-%m-%d %T'),'%Y-%m-%d')
         between '",@startdate,"' AND '",@enddate,"')     
        GROUP BY createdDate,source 
        order by createdDate desc )sub order by createdDate asc");
        
          SET@tr1=CONCAT('TRUNCATE ', @v4);
        
        SET @t2= CONCAT( ' INSERT INTO ',@v4,'(gender,country,product_name,source,date_time)
      SELECT DISTINCT gender,country,product_name,source,DATE_FORMAT(date_time,\'%Y-%m-%d\') 
                     as date_time FROM ',tablename);
        
        
  
 PREPARE stmt FROM @sdate;
        EXECUTE stmt;
        DEALLOCATE PREPARE stmt; 
         
 PREPARE stmts FROM @edate;
        EXECUTE stmts;
        DEALLOCATE PREPARE stmts; 
         
 PREPARE stmt0 FROM @drop;
        EXECUTE stmt0;
        DEALLOCATE PREPARE stmt0; 
           
 PREPARE stmt1 FROM @createtable;
        EXECUTE stmt1;
        DEALLOCATE PREPARE stmt1; 
        
        PREPARE stmt01 FROM @drop1;
        EXECUTE stmt01;
        DEALLOCATE PREPARE stmt01; 
           
 PREPARE stmt11 FROM @createtable1;
        EXECUTE stmt11;
        DEALLOCATE PREPARE stmt11;       
 
 PREPARE stmttr FROM @tr;
        EXECUTE stmttr;
        DEALLOCATE PREPARE stmttr;       
       
        PREPARE stmt2 FROM @t1;
        EXECUTE stmt2;
        DEALLOCATE PREPARE stmt2;
        
        PREPARE stmttr1 FROM @tr1;
        EXECUTE stmttr1;
        DEALLOCATE PREPARE stmttr1;  
        
        PREPARE stmt3 FROM @t2;
        EXECUTE stmt3;
 DEALLOCATE PREPARE stmt3;
        
      
    END$$

DELIMITER ;


What I have tried:

throwing null at line 1 error 1064
Posted
Updated 24-Jun-16 11:37am
v2
Comments
CHill60 24-Jun-16 4:50am    
If it works sometimes and not others then it must be data related - for what input values does it fail?

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900