2017-04-17 21 views
0

此查詢的工作......INSERT查詢中的數據到現有的表

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,它在我太樹皮。

回答

0

你從來沒用過就投功能的相應的右括號。

+0

當我打開用'SELECT CAST(括號。 ..'並且不要關閉它,我得到一個錯誤:'你的SQL語法有錯誤;查看與你的MySQL服務器版本相對應的手冊,以便在'1'附近'nps'使用正確的語法。 –

+0

@JohnF這就是我的觀點,你已經表明你正在執行一個函數調用''cast(...'但你永遠不會關閉它 - 這是消息指出的語法錯誤。 – EyuelDK

+0

@EyueIDK當我添加諸如'nps);'這樣的結束'''時,它仍然返回一個錯誤。 –

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 

不知道的日期部分,但希望你的想法

相關問題