Hi Mario,
Maybe you have "moved on" but just in case...
I forgot to explicitly define the column headings
so those fields will *always exist* for your report:
PARAMETERS [Forms]!frmEndDate![txtEndDate] DateTime;
TRANSFORM NZ(Count(*),0) AS Cnt
SELECT
Orders.CustomerID,
DateAdd("m",1,[Forms]!frmEndDate![txtEndDate]) AS LastMnth
FROM Orders
WHERE
(((Orders.OrderDate)>=DateAdd("m",-20,[Forms]![frmEndDate]![txtEndDate])
And
(Orders.OrderDate)<DateAdd("m",1,[Forms]![frmEndDate]![txtEndDate])))
GROUP BY
Orders.CustomerID,
DateAdd("m",1,[Forms]!frmEndDate![txtEndDate]),
"Mth" &
Format(DateDiff("m",[OrderDate],DateAdd("m",1,[Forms]!frmEndDate![txtEndDate])),"00")
PIVOT
"Mth" &
Format(DateDiff("m",[OrderDate],DateAdd("m",1,[Forms]!frmEndDate![txtEndDate])),"00")
In
("Mth01","Mth02","Mth03","Mth04","Mth05","Mth06","Mth07","Mth08","Mth09","Mth10",
"Mth11","Mth12","Mth13","Mth14","Mth15","Mth16","Mth17","Mth18","Mth19","Mth20");
Note that previously w/o column headings
the PIVOT expression was just an "Expression"
but when add column headings,
the PIVOT expression must be included in GROUP BY clause.
good luck,
gary
Gary Walter said:
If you had a form "frmEndDate"
with a textbox "txtEndDate"
and you entered 2/1998 in textbox,
following should give same 20-month span results:
PARAMETERS [Forms]!frmEndDate![txtEndDate] DateTime;
TRANSFORM NZ(Count(*),0) AS Cnt
SELECT
Orders.CustomerID,
DateAdd("m", 1, [Forms]!frmEndDate![txtEndDate]) AS LastMnth
FROM Orders
WHERE
Orders.OrderDate>=DateAdd("m",-20,[Forms]![frmEndDate]![txtEndDate])
And
Orders.OrderDate<DateAdd("m", 1, [Forms]!frmEndDate![txtEndDate])
GROUP BY
Orders.CustomerID,
DateAdd("m", 1, [Forms]!frmEndDate![txtEndDate])
PIVOT
"Mth" & Format(DateDiff("m",[OrderDate],DateAdd("m", 1,
[Forms]!frmEndDate![txtEndDate])),"00");
:
"Marios" wrote:
I Have a crosstab query, which provides sales per month. I have two
questions on that:
1) Is it possible to present the data from Jul - Jun (or any other
combination) instead on Jan - Dec?
2) Is it possible, if I want the sales for a period larger than one
calendar
year to have more month columns (e.g. from 06/05 to 02/07, 20
columns)?
Hi Mario,
You can add a WHERE clause
(using # delimiters and US date format)
to get however many months you want.
TRANSFORM aggregate function
SELECT
some field(s)
FROM
some table(s)
WHERE
[datefield] >=#6/1/2005#
AND
[datefield] < #3/1/2007#
GROUP BY
some fields
PIVOT Format([datefield], "yyyy\/mm")
Typically though queries do not exist
as "an entity in and of themselves."
You usually show your users results
of queries only in a form or report.
That is why one usually uses "relative" months
as column headings, for example
in NorthWind.mdb try:
TRANSFORM NZ(Count(*),0) AS Cnt
SELECT
Orders.CustomerID,
#3/1/1998# AS LastMnth
FROM Orders
WHERE
Orders.OrderDate >= #6/1/1996#
And
Orders.OrderDate < #3/1/1998#
GROUP BY
Orders.CustomerID,
#3/1/1998#
PIVOT
"Mth" & Format(DateDiff("m",[OrderDate],#3/1/1998#),"00");
this means that you can create a 20-column report
based on this crosstab using relative fields
"Mth01", "Mth02" etc
and report labels for these fields can be computed
in a textbox (not label), for example
=Format(DateAdd("m",-1,[LastMnth]), "yyyy\/mm")
=Format(DateAdd("m",-2,[LastMnth]), "yyyy\/mm")
Dates for the 20-month span can change in your xtab,
but your report does not need to be changed because
the field names are relative.
(all thanks go to Duane for this technique...
I'm just parotting)
good luck,
gary