Reputation: 501
I'm trying to build an online bookstore for my homework.
I want to list the most sold books by time (last week, last month, all time).
I wrote the all time query but couldn't figure the last week query.
Can someone please help me?
Here is the table:
TbSoldBooks (saleID, BookID, SaleDate)
(I searched but can't find the answer)
Update: This is for all time most sold books.
SELECT b.bookID, COUNT(b.bookID) AS All-Time
FROM TbSoldBooks b
GROUP BY b.bookID
Upvotes: 0
Views: 2525
Reputation: 1464
SELECT top 10 b.bookID, COUNT(b.bookID)
FROM TbSoldBooks b
GROUP BY b.bookID
Where SaleDate >= DateAdd(day, -7, getDate()) -- last week
Order by 2 desc
SELECT top 10 b.bookID, COUNT(b.bookID)
FROM TbSoldBooks b
GROUP BY b.bookID
Where SaleDate >= DateAdd(month, -1, getDate()) -- last month
Order by 2 desc
Upvotes: 1