简体   繁体   中英

INSERT of data from a query into an existing table

This query works…

SELECT Round(( (SELECT Count(*) 
                FROM   nps_surveys 
                WHERE  score >= 9 
                       AND social = 0 
                       AND Date(completedon) >= Last_day(CURRENT_DATE) + 
                                                INTERVAL 1 day 
                                                - INTERVAL 1 month) / 
                              (SELECT Count(score) 
                               FROM   nps_surveys 
                               WHERE  score IS NOT NULL 
                                      AND social = 0 
                                      AND Date(completedon) >= Last_day( 
                                          CURRENT_DATE) + 
                                                               INTERVAL 1 day 
                                                               - 
                                          INTERVAL 1 month) * 
                      100 ) - ( (SELECT Count(*) 
                          FROM   nps_surveys 
                          WHERE  score >= 0 
                                 AND score <= 6 
                                 AND social = 0 
                                 AND Date(completedon) >= Last_day(CURRENT_DATE) 
                                                          + 
                                                          INTERVAL 1 
                                                          day 
                                                          - INTERVAL 1 month) / 
                                      (SELECT Count(score) 
                                       FROM   nps_surveys 
                                       WHERE  score IS NOT NULL 
                                              AND social = 0 
                                              AND Date(completedon) >= Last_day( 
                                                  CURRENT_DATE) 
                                                                       + 
                                                                       INTERVAL 
                                                                       1 day 
                                                                       - 
                                                  INTERVAL 1 month) * 
                          100 )) nps; 

But this one doesn't…

SELECT cast(round(( 
                    ( 
                    SELECT Count(*) 
                    FROM   nps_surveys 
                    WHERE  score >= 9 
                    AND    social = 0 
                    AND    Date(completedon) >= Last_day(CURRENT_DATE) + interval 1 day - interval 1 month) /
                   ( 
                          SELECT count(score) 
                          FROM   nps_surveys 
                          WHERE  score IS NOT NULL 
                          AND    social = 0 
                          AND    date(completedon) >= last_day(CURRENT_DATE) + interval 1 day - interval 1 month)*100) - (
                  ( 
                         SELECT count(*) 
                         FROM   nps_surveys 
                         WHERE  score >=0 
                         AND    score <= 6 
                         AND    social = 0 
                         AND    date(completedon) >= last_day(CURRENT_DATE) + interval 1 day - interval 1 month) /
                  ( 
                         SELECT count(score) 
                         FROM   nps_surveys 
                         WHERE  score IS NOT NULL 
                         AND    social = 0 
                         AND    date(completedon) >= last_day(CURRENT_DATE) + interval 1 day - interval 1 month)*100)) nps;

I keep getting an error when running of You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'nps' at line 1 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'nps' at line 1 , but it doesn't make sense because the only difference is the cast function. I tried inserting the known working query above into my python script without the cast and it barks at me too.

You should try simplify your query to something like this:

SELECT COUNT(CASE WHEN score >= 9 THEN 1 END) as bigger_9,           
       COUNT(CASE WHEN score >= 0 and score <= 6  THEN 1 END) as between_0_6,
       COUNT(CASE WHEN score = 0 THEN 1 END) as equal_0,
       COUNT(score) total_count // Dont need filter NULL, COUNT doesnt count NULL
FROM table
WHERE social = 0 
  AND date(completedon) >= last_day(CURRENT_DATE) + 
                           interval 1 day - interval 1 month*100

not sure about the date part but hope you get the idea

您从未在强制转换函数上使用相应的右括号。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM