我已经检查了这个:How to use ROW_NUMBER in sqlite
但这对我没有帮助。我这样查询&它返回我的假行索引。查询出了什么问题?
SELECT (select count(*) from medicalrecords b where a.id >= b.id ) as cnt,avg(bmi)
FROM MedicalRecords a
WHERE Date BETWEEN datetime('now', '-1 month') AND datetime('now', 'localtime')
group by strftime('%W', Date)
答案 0 :(得分:0)
这可以在一个查询中完成,但使用VIEW
将变得更加容易:
-- Groups (without sequencial):
CREATE VIEW Grp0 AS SELECT DISTINCT STRFTIME('%W', Date) AS Week FROM MedicalRecords WHERE Date BETWEEN DATETIME('NOW', '-1 month') AND DATETIME('now', 'localtime');
-- Groups (with sequencial):
CREATE VIEW Grp AS SELECT (SELECT COUNT() FROM Grp0 AS _ WHERE Week<=Grp0.Week), Week FROM Grp0;
-- Query:
SELECT Week, (SELECT AVG(bmi) FROM MedicalRecords WHERE STRFTIME('%W', Date) = Week)) FROM Grp;