Re: Comparing weeks with highest and lowest sales Query--HELP
- From: John Spencer <spencer@xxxxxxxxxx>
- Date: Sat, 30 May 2009 10:28:51 -0400
I think you will have to use nested queries to get the result you want.
Three queries.
First query saved as QueryOne:
SELECT TOP 3 DatePart("ww", saledate) As WeekNum
, Sum(saleamount) As WeekTotal
FROM table
GROUP BY DatePart("ww", saledate)
ORDER BY Sum(saleamount) DESC
Second query saved as QueryTwo:
SELECT TOP 3 DatePart("ww", saledate) As WeekNum
, Sum(saleamount) As WeekTotal
FROM table
GROUP BY DatePart("ww", saledate)
ORDER BY Sum(saleamount) ASC
Third query using UNION to combine the results of the first two queries:
SELECT * FROM QueryOne
UNION
SELECT * FROM QueryTwo
The problem is that UNION queries ignore any order by clauses in the component queries. You can have only one ORDER By clause at the END of the Union query and that is based on the names of the field in the first component query
'====================================================
John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
'====================================================
Neo wrote:
When I entered the code into a blank query I got the following error message:.
The ORDER BY expression (Sum(QuantitySold)) includes fields that are not selected by the query. Only those fields requested in the first query can be included in an ORDER BY expression.
P.S.- QuantitySold is the name of my field in my database. and also, are the two tables in the FROM statements suppose to be the same table (sales)?
"Marshall Barton" wrote:
Neo wrote:i would like to create a query to retrieve the total number of sales for the week and output the 3 highest and lowest weeks for the year (in other words finding the weeks with the most and least number of sales).
Could someone inform me how to go about creating this query, specifically the formulars to use or if i need to write SQL for it?
This way requires SQL view:
SELECT TOP 3 DatePart("ww", saledate) As WeekNum,
Sum(saleamount) As WeekTotal
FROM table
GROUP BY DatePart("ww", saledate)
ORDER BY Sum(saleamount) DESC
UNION ALL
SELECT TOP 3 DatePart("ww", saledate) As WeekNum,
Sum(saleamount) As WeekTotal
FROM table
GROUP BY DatePart("ww", saledate)
ORDER BY Sum(saleamount)
--
Marsh
MVP [MS Access]
- References:
- Prev by Date: Re: Criteria linked to form combo 'Yes/No or All' expression help
- Next by Date: Re: appending new records to a table with a compound primary key
- Previous by thread: Re: Comparing weeks with highest and lowest sales Query--HELP
- Next by thread: Re: Comparing weeks with highest and lowest sales Query--HELP
- Index(es):
Relevant Pages
|