from语句中的Sqlite和嵌套查询

时间:2013-01-14 10:04:51

标签: sql sqlite

我目前正在使用SQLite,我对这个查询感到害怕

    SELECT Cur.Name, SUM(Cur.Added+Cur.Removed) AS Lines, COUNT(DISTINCT(Cur.Author)) AS authCurrent,
       ( SELECT COUNT(DISTINCT(Author)) 
         FROM Change
         WHERE Extencion = "java" AND
             Date >= date('2012-04-08') AND
             Date < date('2012-04-15')
         ) AS totCurrent
    FROM 
       Change AS Cur,
       ( SELECT a.Name, COUNT(DISTICT(a.Author)) AS authLast
         FROM Change AS a
         WHERE a.Extencion = "java" AND
           a.Date >= date('2012-04-08') AND
           a.Date < date('2012-04-15')
       GROUP BY a.Name ) AS Previous
    WHERE
       Previous.Name = Cur.Name
       Cur.Extencion = "java" AND
       Cur.Date >= date('2012-04-15') AND
       Cur.Date < date('2012-04-22')
   GROUP BY Cur.Name

它说“查询错误:靠近”Cur“:语法错误无法执行语句” 我不知道,是不是因为SQLite不支持嵌套查询,所以查询不可执行?

无论如何,表格变更具有以下形式

      Author | Path | Extencion | Name | Date | Added | Removed

1 个答案:

答案 0 :(得分:1)

您似乎在此行拼写DISTINCT错误:

COUNT(DISTICT(a.Author)) AS authLast  -- change to DISTINCT

你也尝试过这样的查询:

SELECT Cur.Name, 
  SUM(Cur.Added+Cur.Removed) AS Lines, 
  COUNT(DISTINCT(Cur.Author)) AS authCurrent,
  Previous.authLast AS totCurrent
FROM Change AS Cur
LEFT JOIN 
( 
  SELECT a.Name, COUNT(DISTINCT(a.Author)) AS authLast
  FROM Change AS a
  WHERE a.Extencion = "java" AND
    a.Date >= date('2012-04-08') AND
    a.Date < date('2012-04-15')
  GROUP BY a.Name
) AS Previous
  ON Previous.Name = Cur.Name
WHERE 
       Cur.Extencion = "java" AND
       Cur.Date >= date('2012-04-15') AND
       Cur.Date < date('2012-04-22')
GROUP BY Cur.Name, Previous.authLast