[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

MS Access query to display sum of a column in each week

Posted on 2014-08-14
10
Medium Priority
?
905 Views
Last Modified: 2014-08-25
I have a table like below

Type  Product  OrdDate          Qty

A        edr         08/11/2014       2
A        ehg         07/30/2014       3
A        hfg         07/15/2014       5
A        edr         07/15/2014       3
B        juy         08/11/2014       2
B        fyh         07/30/2014       4
B        juy         07/23/2014       2
B        fyh         07/23/2014       1

I need to get a result by week numbers and sum of the quantity in the corresponding week number of the order date. Please help.

Type    Week29    Week30   Week31   Week33
A          8                0              3             2
B          0                3              4             2
0
Comment
Question by:Angel02
  • 4
  • 3
  • 3
10 Comments
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 40261869
Create a query like this:

SELECT YourTable.ID, YourTable.YourDate, YourTable.Type, Format([YourDate],"ww") AS TheWeek
FROM YourTable;

Then save it as something like: YourQuery


then create a crostab query like this:

TRANSFORM Count(YourQuery.[ID]) AS CountOfID
SELECT YourQuery.[Type]
FROM YourQuery
GROUP BY YourQuery.[Type]
PIVOT YourQuery.[TheWeek];
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 40261873
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 40261887
Sorry, that did no answer your question correctly,...
Try this sample:
Database28.mdb
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 74

Accepted Solution

by:
Jeffrey Coachman earned 1400 total points
ID: 40261890
Query:
SELECT YourTable.ID, YourTable.YourDate, YourTable.Type, Format([YourDate],"ww") AS TheWeek, YourTable.Qty
FROM YourTable;


Crosstab Query:
TRANSFORM Sum(YourQuery.Qty) AS SumOfQty
SELECT YourQuery.Type
FROM YourQuery
GROUP BY YourQuery.Type
PIVOT YourQuery.TheWeek;


JeffCoachman
0
 

Author Comment

by:Angel02
ID: 40262198
Thank you Jeffrey Coachman.
This is awesome. I did not know I could do it this way.

If I want to indicate the week range in dates for the given week number, is it possible in this query?

29 (07/13/2014 - 07/19/2014)
0
 
LVL 52

Assisted Solution

by:Gustav Brock
Gustav Brock earned 600 total points
ID: 40262725
You can do it with one query:

TRANSFORM
    Sum(YourTable.Qty) AS Total
SELECT
    YourTable.Type
FROM
    YourTable
GROUP BY
    YourTable.Type
PIVOT
    Format([YourDate],"ww",2,2) & " (" & Format(DateWeekFirst([YourDate],2),"mm\/dd\/yyyy") & " - " & Format(DateWeekLast([YourDate],2),"mm\/dd\/yyyy") & ")";

using these two functions:
Public Function DateWeekFirst( _
  ByVal datDate As Date, _
  Optional ByVal lngFirstDayOfWeek As VbDayOfWeek = vbUseSystemDayOfWeek) _
  As Date

' Returns the first date of the week of datDate.
' lngFirstDayOfWeek defines the first weekday of the week.
' 2000-09-07. Cactus Data ApS.
' 2003-05-01. System settings used as default.
' 2012-10-44. Data type of lngFirstDayOfWeek changed to VbDayOfWeek.
    
  DateWeekFirst = DateAdd("d", vbSunday - Weekday(datDate, lngFirstDayOfWeek), datDate)
    
End Function

Public Function DateWeekLast( _
  ByVal datDate As Date, _
  Optional ByVal lngFirstDayOfWeek As Long = vbUseSystemDayOfWeek) _
  As Date

' Returns the last date of the week of datDate.
' lngFirstDayOfWeek defines the first weekday of the week.
' 2000-09-07. Cactus Data ApS.
' 2003-05-01. System settings used as default.
' 2012-10-44. Data type of lngFirstDayOfWeek changed to VbDayOfWeek.
    
  DateWeekLast = DateAdd("d", vbSaturday - Weekday(datDate, lngFirstDayOfWeek), datDate)
    
End Function

Open in new window

The parameters shown (2) are for ISO week numbering. Adjust to your needs.
/gustav
0
 

Author Comment

by:Angel02
ID: 40268137
Thank you. Can you please also tell me how can I write the same query using SQL?
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 40268188
You wouldn't need that in Access.

If you want to put it all into T-SQL, you would replace the Format and the two weekday functions (using DateAdd) with the corresponding functions of SQL Server. Nothing fancy.

/gustav
0
 

Author Comment

by:Angel02
ID: 40268241
If I just use this, it doesn't look like SQL recognizes it. Does SQL recognize transform?

TRANSFORM
    Sum(YourTable.Qty) AS Total
SELECT
    YourTable.Type
FROM
    YourTable
GROUP BY
    YourTable.Type
PIVOT
    Datepart(wk,[YourDate])
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 40268260
That's right. It's an Access SQL speciality.

There are ways to resolve this but I don't use them, so you'll have to bing/goolge on it or see if someone else has a method on hand.

/gustav
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

865 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question