sqlite查询可以加法但不能除法

时间:2019-07-17 16:22:12

标签: sql sqlite

为什么此SQLite语句可用于加法运算但不能用于除法运算?我试图将两个查询的总和除以第三个:(Q1 + Q2)/ Q3。

在这里,第1季度和第2季度已合并到一个查询中,我试图将其除以第三个查询。

这是表格的代码:

CREATE TABLE subset_all(
  "SegmentID" TEXT,
  "RatingDateTime" TEXT,
  "Rating" TEXT,
  "value" TEXT
);

这是我使用加法时有效的代码。我通过尝试调试除法情况发现了它的工作原理:

select(
(select sum(LM)
FROM(
SELECT sum(value) LM
FROM (SELECT segmentid, rating, RatingDateTime, value
           , row_number() OVER (PARTITION BY segmentid ORDER BY RatingDateTime DESC) AS rn
      FROM subset_all
      WHERE RatingDateTime < date('2014-10-20'))
WHERE rating >= 7 AND rn = 1
UNION ALL
SELECT sum(value) LM
FROM (SELECT segmentid, rating, RatingDateTime, value
           , row_number() OVER (PARTITION BY segmentid ORDER BY RatingDateTime DESC) AS rn
      FROM subset_all
      WHERE RatingDateTime < date('2014-10-20'))
WHERE rating >= 8 AND rn = 1
))
+
(select sum(LM) from (
SELECT sum(value) LM
FROM (SELECT segmentid, RatingDateTime, value
           , row_number() OVER (PARTITION BY segmentid ORDER BY RatingDateTime DESC) AS rn
      FROM subset_all
      WHERE RatingDateTime < date('2014-10-20'))
WHERE rn = 1 
)
)
);

这是我要执行的代码,当我使用除法功能时不起作用!

select(
    (select sum(LM)
     FROM(
        SELECT sum(value) LM
        FROM (
            SELECT segmentid, rating, RatingDateTime, value
               , row_number() OVER (PARTITION BY segmentid ORDER BY RatingDateTime DESC) AS rn
            FROM subset_all
            WHERE RatingDateTime < date('2014-10-20')
        )
        WHERE rating >= 7 AND rn = 1
        UNION ALL
        SELECT sum(value) LM
        FROM (
            SELECT segmentid, rating, RatingDateTime, value
               , row_number() OVER (PARTITION BY segmentid ORDER BY RatingDateTime DESC) AS rn
            FROM subset_all
            WHERE RatingDateTime < date('2014-10-20')
        )
        WHERE rating >= 8 AND rn = 1
    )
    )
    /
    (select sum(LM) 
     from (
        SELECT sum(value) LM
        FROM (
            SELECT segmentid, RatingDateTime, value
                   , row_number() OVER (PARTITION BY segmentid ORDER BY RatingDateTime DESC) AS rn
            FROM subset_all
            WHERE RatingDateTime < date('2014-10-20')
        )
        WHERE rn = 1 
    )
    )
);

这是运行非工作代码时出现的错误:

Error: near "(": syntax error

有人可以解释我在做什么错吗?

0 个答案:

没有答案