将查询中的数据插入现有表中

时间:2017-04-17 21:56:07

标签: python sql

此查询有效......

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; 

但这个没有...

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;

我在运行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时遇到错误,但它没有意义,因为唯一的区别是cast功能。我尝试将上面已知的工作查询插入到我的python脚本中,而不是 cast,它也咆哮着我。

2 个答案:

答案 0 :(得分:1)

您应该尝试将查询简化为以下内容:

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

不确定日期部分,但希望您明白

答案 1 :(得分:0)

你从未在演员表中使用过相应的右括号。