如何在SQLite中使用ROW_NUMBER的T-SQL和逐个查询?

时间:2013-10-18 08:38:23

标签: sql sqlite

我已经检查了这个: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)

SQL Fiddle

1 个答案:

答案 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;