A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Running & Setting Up Queries
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

I require a query to count repairs by week



 
 
Thread Tools Display Modes
  #1  
Old May 4th, 2010, 02:20 AM posted to microsoft.public.access.queries
trevorC via AccessMonster.com
external usenet poster
 
Posts: 37
Default I require a query to count repairs by week

Hi,
I have a query as shown, that gives me the number of repairs per week, but
the output shows an entry for each day of the week with the total as well,
can i just get the total per week and the week number or week start date even.


requested o/p =

sample only
Week start 1/1/10 74
Week start 7/1/10 13
Week start 15/1/10 116


SELECT DatePart("ww",[out]) AS [Repairs per Week], gamrep.Out
FROM gamrep;

Any help would be great.

--
Message posted via http://www.accessmonster.com

  #2  
Old May 4th, 2010, 03:11 AM posted to microsoft.public.access.queries
trevorC via AccessMonster.com
external usenet poster
 
Posts: 37
Default I require a query to count repairs by week

I so far have this now...

SELECT Count(gamrep.[In]) AS CountOfDate, Format(DateAdd("d",-DatePart("w",
gamrep![in],1)+2,gamrep![in]),"dddd") & " " & DateAdd("d",-DatePart("w",
gamrep![in],1)+2,gamrep![in]) AS WeekStarting
FROM gamrep
GROUP BY Format(DateAdd("d",-DatePart("w",gamrep![in],1)+2,gamrep![in]),
"dddd") & " " & DateAdd("d",-DatePart("w",gamrep![in],1)+2,gamrep![in])

This gives me the data i do need, but shows it sorted by number not date
EG
230 Monday 1/01/2007 - Jan
237 Monday 1/02/2010 - Feb
003 Monday 1/03/2004 - Mar
112 Monday 1/04/2010 - Apr
...
032 Monday 12/01/2009 - Jan
and so on
can this be sorted by date
EG
first week of january
second week of january
third week of january
fourth week of january
first week in Febuary
and so on.

Is it possible to sort this query this way.
regards
TrevorC

--
Message posted via http://www.accessmonster.com

  #3  
Old May 4th, 2010, 03:31 AM posted to microsoft.public.access.queries
Marshall Barton
external usenet poster
 
Posts: 5,361
Default I require a query to count repairs by week

trevorC via AccessMonster.com wrote:
SELECT Count(gamrep.[In]) AS CountOfDate, Format(DateAdd("d",-DatePart("w",
gamrep![in],1)+2,gamrep![in]),"dddd") & " " & DateAdd("d",-DatePart("w",
gamrep![in],1)+2,gamrep![in]) AS WeekStarting
FROM gamrep
GROUP BY Format(DateAdd("d",-DatePart("w",gamrep![in],1)+2,gamrep![in]),
"dddd") & " " & DateAdd("d",-DatePart("w",gamrep![in],1)+2,gamrep![in])

This gives me the data i do need, but shows it sorted by number not date
EG
230 Monday 1/01/2007 - Jan
237 Monday 1/02/2010 - Feb
003 Monday 1/03/2004 - Mar
112 Monday 1/04/2010 - Apr
..
032 Monday 12/01/2009 - Jan
and so on
can this be sorted by date
EG
first week of january
second week of january
third week of january
fourth week of january
first week in Febuary
and so on.



Just add:
ORDER BY DateAdd("d", -DatePart("w", gamrep![in], 1)+2,
gamrep![in])

BTW, there's nothing wrong with using DatePart, but it's a
little easier to type and to read if you use the WeekDay
function:

DateAdd("d",-WeekDay(gamrep![in])+2,gamrep![in])

--
Marsh
MVP [MS Access]
  #4  
Old May 4th, 2010, 03:48 AM posted to microsoft.public.access.queries
trevorC via AccessMonster.com
external usenet poster
 
Posts: 37
Default I require a query to count repairs by week

Thanks for the update on this, but I am getting the error shown below

*****************
You tried to execute a query that does not include the specified expression
'DateAdd("d", -DatePart("w", gamrep![in], 1)+2,gamrep![in])' as part of an
aggregate function.
*****************

code from SQL with orderby added

SELECT Count(gamrep.[In]) AS CountOfDate, Format(DateAdd("d",-DatePart("w",
gamrep![in],1)+2,gamrep![in]),"dddd") & " " & DateAdd("d",-DatePart("w",
gamrep![in],1)+2,gamrep![in]) AS WeekStarting
FROM gamrep
GROUP BY Format(DateAdd("d",-DatePart("w",gamrep![in],1)+2,gamrep![in]),
"dddd") & " " & DateAdd("d",-DatePart("w",gamrep![in],1)+2,gamrep![in])
ORDER BY DateAdd("d", -DatePart("w", gamrep![in], 1)+2,gamrep![in]);

regards
TrevorC

--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...eries/201005/1

  #5  
Old May 4th, 2010, 08:29 AM posted to microsoft.public.access.queries
Bob Barrows
external usenet poster
 
Posts: 475
Default I require a query to count repairs by week

trevorC via AccessMonster.com wrote:
Thanks for the update on this, but I am getting the error shown below

*****************
You tried to execute a query that does not include the specified
expression 'DateAdd("d", -DatePart("w", gamrep![in],
1)+2,gamrep![in])' as part of an aggregate function.
*****************

code from SQL with orderby added

SELECT Count(gamrep.[In]) AS CountOfDate,
Format(DateAdd("d",-DatePart("w",
gamrep![in],1)+2,gamrep![in]),"dddd") & " " &
DateAdd("d",-DatePart("w", gamrep![in],1)+2,gamrep![in]) AS
WeekStarting
FROM gamrep
GROUP BY
Format(DateAdd("d",-DatePart("w",gamrep![in],1)+2,gamrep![in]),
"dddd") & " " &
DateAdd("d",-DatePart("w",gamrep![in],1)+2,gamrep![in]) ORDER BY
DateAdd("d", -DatePart("w", gamrep![in], 1)+2,gamrep![in]);


Four things:
1. You don't have to group by the formatted week start date. In fact, the
query will perform a little better if you minimize the number of
calculations done on the field being grouped by.
2. You don't have to include the field being grouped by in the SELECT
clause.
3. You can use the grouped-by field in an expression in the SELECT clause
without including that new expression in the GROUP BY clause.
4. You can order by a field in the GROUP BY clause that is not listed in the
SELECT clause.

The result of all those points:
SELECT
Format(DateAdd("d",-DatePart("w",gamrep![in],1)+2,gamrep![in]),"dddd") & " "
& DateAdd("d",-DatePart("w",gamrep![in],1)+2,gamrep![in]) AS WeekStarting,
Count(*) AS CountOfDate
FROM gamrep
GROUP BY DateAdd("d",-DatePart("w",[gamrep]![in],1)+2,[gamrep]![in])
ORDER BY DateAdd("d",-DatePart("w",[gamrep]![in],1)+2,[gamrep]![in]);

--
Microsoft MVP - ASP/ASP.NET - 2004-2007
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"


 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 12:58 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.