C
Clif McIrvin
Looking for help with a select query.
I have a table containing a column: WeekEnding (Date/Time field).
Each week will have a different number of rows.
I'm trying to write a query that will return the rows from the most
recent two weeks. I discovered that TOP 1 returns all the rows from the
most recent week, but to my dismay discovered that TOP 2 also returns
the same rows. On reflection, I believe that I understand why, which
leads to my dilemma: how do I return the rows from the last two weeks,
regardless of how many rows that might be?
Here's what I have right now:
SELECT TOP 8 [Mix Designs].[Mix ID], AssignedLots.LotNo,
AssignedLots.WeekEnding
FROM [Mix Designs] INNER JOIN (Plants INNER JOIN AssignedLots ON
Plants.PlantID=AssignedLots.PlantID) ON [Mix
Designs].MixID=AssignedLots.MixID
ORDER BY AssignedLots.WeekEnding DESC;
which returns the last three weeks:
Mix ID, LotNo, WeekEnding
6PF, 10-040, 03-Apr-10
SMH, 10-042, 03-Apr-10
7A, 10-039, 03-Apr-10
3BF, 10-041, 03-Apr-10
6PF, 10-037, 27-Mar-10
7A, 10-036, 27-Mar-10
3BF, 10-038, 27-Mar-10
6PF, 10-035, 20-Mar-10
3BF, 10-034, 20-Mar-10
1PF, 10-033, 20-Mar-10
7A, 10-032, 20-Mar-10
I'm going to use TOP 7, on the premise that any given week could have
six rows, but seven rows is not likely. The downside is when there are
only 2 or 3 rows in each week, causing the query to return three (or 4)
weeks instead of two.
Suggestions?
Thanks in advance!
Clif
I have a table containing a column: WeekEnding (Date/Time field).
Each week will have a different number of rows.
I'm trying to write a query that will return the rows from the most
recent two weeks. I discovered that TOP 1 returns all the rows from the
most recent week, but to my dismay discovered that TOP 2 also returns
the same rows. On reflection, I believe that I understand why, which
leads to my dilemma: how do I return the rows from the last two weeks,
regardless of how many rows that might be?
Here's what I have right now:
SELECT TOP 8 [Mix Designs].[Mix ID], AssignedLots.LotNo,
AssignedLots.WeekEnding
FROM [Mix Designs] INNER JOIN (Plants INNER JOIN AssignedLots ON
Plants.PlantID=AssignedLots.PlantID) ON [Mix
Designs].MixID=AssignedLots.MixID
ORDER BY AssignedLots.WeekEnding DESC;
which returns the last three weeks:
Mix ID, LotNo, WeekEnding
6PF, 10-040, 03-Apr-10
SMH, 10-042, 03-Apr-10
7A, 10-039, 03-Apr-10
3BF, 10-041, 03-Apr-10
6PF, 10-037, 27-Mar-10
7A, 10-036, 27-Mar-10
3BF, 10-038, 27-Mar-10
6PF, 10-035, 20-Mar-10
3BF, 10-034, 20-Mar-10
1PF, 10-033, 20-Mar-10
7A, 10-032, 20-Mar-10
I'm going to use TOP 7, on the premise that any given week could have
six rows, but seven rows is not likely. The downside is when there are
only 2 or 3 rows in each week, causing the query to return three (or 4)
weeks instead of two.
Suggestions?
Thanks in advance!
Clif