查询以检索过去24个月内租用最多的图书

时间:2013-12-12 14:59:05

标签: sql oracle

如何查找过去24个月内租用最多的图书?

 rentedBooks (rentid, bookid, dateout, datein)  

我尝试了以下内容:

SELECT * FROM RENTEDBOOKS, COUNT(BOOKID) WHERE DATEIN >= DATEADD(MONTH, -24, GETDATE()); 

但是继续收到提及无效标识符的错误:S

注意:我没有使用mysql!

3 个答案:

答案 0 :(得分:2)

SELECT BOOKID, COUNT(*) cnt
FROM RENTEDBOOKS
WHERE DATEIN >= sysdate - interval '24' month
group by BOOKID
order by cnt desc; 

修改

SELECT BOOKID, COUNT(*) cnt
FROM RENTEDBOOKS
WHERE DATEIN >= add_months(sysdate, -24)
group by BOOKID
order by cnt desc; 

答案 1 :(得分:0)

我不确定您的WHERE子句(DATEADD,getdate)发生了什么,但其余部分看起来应该是这样......

SELECT BOOKID, COUNT(BOOKID) 
FROM RENTEDBOOKS
WHERE ...
GROUP BY BOOKID; 

答案 2 :(得分:0)

WHERE DATEIN >= DATEADD('m',-24, Date$());