0

我已经检查过了:如何在 sqlite 中使用 ROW_NUMBER

但这对我没有帮助。我已经这样查询了,它返回了错误的行索引。查询有什么问题?

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)

SQL小提琴

4

1 回答 1

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;
于 2013-10-18T12:57:43.820 回答