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

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
Angel02Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jeffrey CoachmanMIS LiasonCommented:
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
Jeffrey CoachmanMIS LiasonCommented:
0
Jeffrey CoachmanMIS LiasonCommented:
Sorry, that did no answer your question correctly,...
Try this sample:
Database28.mdb
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Jeffrey CoachmanMIS LiasonCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Angel02Author Commented:
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
Gustav BrockCIOCommented:
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
Angel02Author Commented:
Thank you. Can you please also tell me how can I write the same query using SQL?
0
Gustav BrockCIOCommented:
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
Angel02Author Commented:
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
Gustav BrockCIOCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.