Hi Rich!
I didn’t risk to show the query before, because there is would be a big chance that nobody would read it. Anyway, I’ll try to explain what’s going on:
There are following tables:
tbl_Books
BookID (primary)
CategoryID
DiscPrice
Quantity
tbl_Categories
CategoryID (primary)
CategoryName
tbl_AgentWeeklyStockSale – (stores weekly sale of every agent excluding details)
AgentID (primary)
WeekID (primary)
WeeklyReportDetailsID
Totals
tbl_AgentWeeklySSDetails – (stores details of weekly sale of every agent)
WeeklyReportDetailsID (primary)
BookID
StockCurrent
New
Sold
Returned
The task is to select new collection of book titles for an agent, which he hasn’t had on his sale (“tblAgentWeeklyStock.Sold”) for the last 30 weeks. Quantity (tbl_Books.Quanitity) has to be > 0, Sale > 1 (“tblAgentWeeklyStock.Sold”), also agent has not taken (“tblAgentWeeklyStock.New” field) any new quantities of the book for the last 10 weeks; In the current example for simplicity AgentID=7, and current WeekID=200435.
SELECT DISTINCT [tbl_Categories].[CategoryID], [tbl_Categories].[CategoryName], [tbl_Books].[BookName], [tbl_Books].[Quantity], [tbl_Books].[DiscPrice], [tbl_Books].[BookID]
FROM tbl_Categories INNER JOIN (tbl_Books LEFT JOIN tbl_AgentWeeklySSDetails ON [tbl_Books].[BookID]=[tbl_AgentWeeklySSDetails].[BookID]) ON
[tbl_Categories].[CategoryID]=[tbl_Books].[CategoryID]
WHERE tbl_Books.Quantity>0 AND tbl_Books.BookID NOT IN
(SELECT BookID FROM tbl_AgentWeeklySSDetails
WHERE (New > 1 AND WeeklyReportDetailsID IN
(SELECT WeeklyReportDetailsID FROM tbl_AgentWeeklyStockSale WHERE AgentID = 7 AND WeekID > 200435 - 10))
OR (Sold > 1 AND WeeklyReportDetailsID IN
(SELECT WeeklyReportDetailsID FROM tbl_AgentWeeklyStockSale WHERE AgentID = 7 AND WeekID > 200435 -30 )))
Any comments welcome. Thank you