Hello Friends,
One more problem to solve and I need your help. I have a crosstab query is below that runs and give me my results. - TRANSFORM Sum(qBG.SumOfBG) AS SumOfSumOfBG
-
SELECT qBG.Company, qBG.Region, qBG.Territory, Sum(qBG.SumOfBG) AS [Total Of SumOfBG]
-
FROM qBG
-
GROUP BY qBG.Company, qBG.Region, qBG.Territory
-
PIVOT qBG.HeldOn In ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
query runs and give me accurate results. But I do not want full months. I want only that months which contain data in brackets like (Jan,Feb,Mar) etc. Plz see picture attached. 9 1776
Irsmalik,
Based upon your image, are you saying you only want the totals from those [articular months? Or, are you saying that the totals for those records is based upon values that happen to fall in those particular months, and you are annotating which months those values are from?
I’m not sure if my question made sense, but what I am getting at is, for the first record listed, what is the source of “Jan, Feb, Apr”? Because this is not a Calendar “quarter”, but rather a list of months, in order, but missing one month.
Thanks.
Hope we can hepp!
Thank you sir
Actually when I run query, it gives me all the months name (Jan ~ Dec) so it is long size to print in a report. Further, my boss required only the month which contain the data. Suppose if the data is in Jan, Feb & Apr... I want to print it in Bracket like this (Jan, Feb, Apr)... that same for other regions.
It is not quarterly base... it is just a query for all year. Query picture is attached.... but my requirement is the previous image.
Thanks
irsmalik
irsmalik,
My initial thought is that you will need some form of either a subquery or a User-Defined function that returns just the months which apply to each record. Then, you no longer need a CrossTab Query, but can simply use an Aggregate Query (for the Total), and one of your Fields will be your function: - SELECT
-
qBG.Company,
-
qBG.Region,
-
qBG.Territory,
-
Sum(qBG.SumOfBG) AS [Total Of SumOfBG],
-
GetMonths(qBG.Company, qBG.Region, qBG.Territory) as Months
-
FROM
-
qBG
-
GROUP BY
-
qBG.Company,
-
qBG.Region,
-
qBG.Territory;
This code assumes you have a User-Defined Function GetMonths(), which receives three arguments and returns a String value of the months which apply.
Since I don't know any more about your data, going beyond this general recommendation is impossible.
Knowing more, we might could move forward.
Hope this hepps point you in a better direction.
Alternatively, if you want to avoid VBA, use the crosstab as the source for another query and derive a field that concatenates 12 Nz() functions.
Dear Rabbit
What is concatenates 12 Nz() functions. ? I have no idea about this... can you please explain. ?
thanks
irsmalik
NeoPa 32,556
Expert Mod 16PB
That means you take each month column from the CrossTab Query and use it to create a Field which includes all the twelve columns - BUT handles those columns having no data in such a way that the resulting data makes sense.
You could create a query that lists the months: - SELECT
-
qBG.Company, qBG.Region, qBG.Territory, qBG.HeldOn
-
FROM
-
qBG
-
GROUP BY
-
qBG.Company, qBG.Region, qBG.Territory, qBG.HeldOn
Save this as qBGmonths.
Then use my DJoin function found in my article here: Link removed
and here: VBA.DJoin
and create a query to list the joined months: - SELECT
-
qBG.Company, qBG.Region, qBG.Territory, Sum(qBG.SumOfBG) AS [BG],
-
DJoin("[HeldOn]", "[qBGmonth]", "[Company]=" & [Company] & " And [Region]=" & [Region] & " And [Territory]=" & [Territory] & "", ", ") As Months
-
FROM
-
qBG
-
GROUP BY
-
qBG.Company, qBG.Region, qBG.Territory
Of course, if a group field holds text, apply quotes in the filter string.
Disclaimer: Air code
- I sort of took a hybrid approach for generating the Months with Data.
- I used qGB_Crosstab (simulated CrossTab) as the source for another Query which I named qGB_Crosstab_2. qGB_Crosstab_2 displays the [Company], [Region], [Territory], and [Total] Fields from qGB_Crosstab as well as a Calculated Field name [Period].
- SQL for qGB_Crosstab:
- SELECT qBG_Crosstab.Company, qBG_Crosstab.Region, qBG_Crosstab.Territory, qBG_Crosstab.Total,
-
fCalcMonths([Region],[Territory]) AS Period FROM qBG_Crosstab;
- The [Period] Field passes the [Region] and [Territory] Fields to a Public Function named fCalcMonths(). Within this Function, a Recordset is created based on the Unique Combination of [Region] and [Territory]. For each Record, Fields 4 thru 15 (Jan thru Dec) are checked for NULL Values. If a Field contains Data, then the Field Name is concatenated to a Build String.
- Query OUTPUT (Sample Data):
- Company Region Territory Total Period
-
GREENLET A North 5 Feb, Jul
-
GREENLET B South 3 Jan, Feb, Apr
-
GREENLET C East 7 Mar, Jun, Aug, Nov
-
GREENLET D West 8 Jan, May, Sep, Oct, Dec
- Function Definition:
- Public Function fCalcMonths(strRegion As String, strTerritory As String) As String
-
Dim MyDB As DAO.Database
-
Dim rst As DAO.Recordset
-
Dim strSQL As String
-
Dim intFld As Integer
-
Dim strBuild As String
-
-
strSQL = "SELECT * FROM qBG_Crosstab WHERE [Region] = '" & strRegion & _
-
"' AND [Territory] = '" & strTerritory & "'"
-
-
Set MyDB = CurrentDb
-
Set rst = MyDB.OpenRecordset(strSQL, dbOpenSnapshot)
-
-
For intFld = 4 To 15 'Jan to Dec inclusive
-
If Not IsNull(rst.Fields(intFld)) Then
-
strBuild = strBuild & rst.Fields(intFld).Name & ", "
-
End If
-
Next
-
-
fCalcMonths = Left$(strBuild, Len(strBuild) - 2) 'Remove Trailing ', '
-
-
rst.Close
-
Set rst = Nothing
-
End Function
- Finally, fCalcMonths() returns this Concatenated String to the [Period] Field in the Query. Each Field ends with a Comma and Space which are stripped prior to returning the completed String.
- Realizing that this all sounds very confusing, I uploaded the Demo for your Review.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Derek |
last post by:
I came upon the idea of writting a logging class that
uses a Python-ish syntax that's easy on the eyes (IMO):
int x = 1;
double y = 2.5;
std::string z = "result";
debug = "Results:", x, y,...
|
by: Pmb |
last post by:
I'm trying to learn the syntax for initializing objects in a comma separated
list. Below is an example program I wrote to learn how to do this (among
other things). While I understand how to...
|
by: SteelDetailer |
last post by:
Thnaks in advance for considering this post. It's probably very simple,
but.....
I have an old VB6 application that allows me to create, save and edit a
"project information file" that is a...
|
by: Edwin Knoppert |
last post by:
I can't find a way to obtain the months name from a date.
Like 'August'
|
by: Kirk Strauser |
last post by:
Given a class:
how can I find its name, such as:
'foo'
I'm writing a trace() decorator for the sake of practice, and am trying to
print the name of the class that a traced method belongs...
|
by: =?Utf-8?B?QUEyZTcyRQ==?= |
last post by:
Why does System.Globalization.DateTimeFormatInfo.CurrentInfo.MonthNames
return 13 elements? Why does it not use the cuurent culture?
|
by: Kandiman |
last post by:
Hello,
Im hoping you can help, for now im trying to learn how to do something so an easy example may help me to gain the concept.
I wish to create a hyperlink that when i click it, it prints my...
|
by: Jim in Arizona |
last post by:
I built a webpage using vb.net (.net 2.0) that creates a form letter. This
letter pulls data from a database. Although I populate the address with the
person's full name, which comes from the name...
|
by: Kasu |
last post by:
function month_name($monthNumber)
{
$months = array(
1 =>'January',
2 =>'February',
3 =>'March',
4 =>'April',
5 =>'May',
6 =>'June',
7 =>'July',
|
by: Biplab Tikader |
last post by:
#ifndef _ASA_USER_ASA_H_
#define _ASA_USER_ASA_H_
#ifdef __cplusplus
extern "C" {
#endif
/***********************************************************************
* Adaptive Simulated...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |