Re: Top 5 records per group
- From: Michael Gramelspacher <gramelsp@xxxxxxxx>
- Date: Fri, 9 Mar 2007 19:00:04 -0600
In article <0D2B1F1A-3F75-433A-B101-B27F46AD43C5
@microsoft.com>, rmarie127@xxxxxxxxxxxxxxxxxxxxxxxxx says...
Ok, I am trying to create a query that will bring me back the top 5 Shrink %maybe something like this, but I must say your column names
Retails for each store in a table. I've been tooling around with this but I
can seem to get it to work correctly. Here's the base query:
SELECT [Weekly Detail].Area, [Weekly Detail].Region, [Weekly
Detail].District, [Weekly Detail].store_nbr, [Weekly Detail].Inv_Date,
[Weekly Detail].Prior_Inv_Date, [Weekly Detail].Type, [Weekly Detail].[FS
Shrink %], [Weekly Detail].[FS Prior %], [Weekly Detail].[FS Shrink%
Erosion], CatShrinkStatCode9.[Cat Number], category.cat_dsc,
CatShrinkStatCode9.[Shrink % Retail], CatShrinkStatCode9.[Shrink % Cost]
FROM ([Weekly Detail] INNER JOIN CatShrinkStatCode9 ON [Weekly
Detail].store_nbr = CatShrinkStatCode9.Store) LEFT JOIN category ON
CatShrinkStatCode9.[Cat Number] = category.cat_nbr
GROUP BY [Weekly Detail].Area, [Weekly Detail].Region, [Weekly
Detail].District, [Weekly Detail].store_nbr, [Weekly Detail].Inv_Date,
[Weekly Detail].Prior_Inv_Date, [Weekly Detail].Type, [Weekly Detail].[FS
Shrink %], [Weekly Detail].[FS Prior %], [Weekly Detail].[FS Shrink%
Erosion], CatShrinkStatCode9.[Cat Number], category.cat_dsc,
CatShrinkStatCode9.[Shrink % Retail], CatShrinkStatCode9.[Shrink % Cost],
CatShrinkStatCode9.[Shrink % Retail]
ORDER BY [Weekly Detail].store_nbr, CatShrinkStatCode9.[Shrink % Retail];
What I'd like it to look like is this:
Store Cat Shrink % Retail
1234 1 5
1234 2 4
1234 3 3
1234 4 2
1234 5 1
2345 1 8
2345 2 6
2345 3 4
2345 4 3
2345 5 1
etc., etc.
seem strange. There does not seem to be a consistent system.
SELECT b.store_nbr AS Store,
COUNT(* ) AS Cat,
b.[Shrink% Retail]
FROM BaseQuery AS b
INNER JOIN BaseQuery AS b1
ON ((b.[Shrink% Retail] < b1.[Shrink% Retail])
OR (b.[Shrink% Retail] = b1.[Shrink% Retail]
AND b.Inv_Date <= b1.Inv_date))
AND (b.store_nbr = b1.store_nbr)
GROUP BY b.store_nbr,b.[Shrink% Retail], b.Inv_Date
HAVING 5 >= COUNT(* )
ORDER BY b.store_nbr,
COUNT(* );
.
- Prev by Date: Re: Novice getting error on query - please critique
- Next by Date: Re: Removing Formatting Symbols from Phone numbers
- Previous by thread: Re: Date Timestamp Format
- Next by thread: Re: Top 5 records per group
- Index(es):
Relevant Pages
|