By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,818 Members | 1,381 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,818 IT Pros & Developers. It's quick & easy.

Prints Months Name in Brackets with Comma

P: 71
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.
Expand|Select|Wrap|Line Numbers
  2. SELECT qBG.Company, qBG.Region, qBG.Territory, Sum(qBG.SumOfBG) AS [Total Of SumOfBG]
  3. FROM qBG
  4. GROUP BY qBG.Company, qBG.Region, qBG.Territory
  5. 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.

Attached Images
File Type: jpg MyRequirement.jpg (35.4 KB, 138 views)
2 Weeks Ago #1
Share this Question
Share on Google+
7 Replies

Expert Mod 2.5K+
P: 3,278

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.


Hope we can hepp!
2 Weeks Ago #2

P: 71
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.

Attached Images
File Type: jpg Query.jpg (137.7 KB, 130 views)
2 Weeks Ago #3

Expert Mod 2.5K+
P: 3,278

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:

Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.     qBG.Company, 
  3.     qBG.Region, 
  4.     qBG.Territory, 
  5.     Sum(qBG.SumOfBG) AS [Total Of SumOfBG], 
  6.     GetMonths(qBG.Company, qBG.Region, qBG.Territory) as Months 
  7. FROM 
  8.     qBG 
  9. GROUP BY 
  10.     qBG.Company, 
  11.     qBG.Region, 
  12.     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.
2 Weeks Ago #4

Expert Mod 10K+
P: 12,364
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.
2 Weeks Ago #5

P: 71
Dear Rabbit

What is concatenates 12 Nz() functions. ? I have no idea about this... can you please explain. ?
2 Weeks Ago #6

Expert Mod 15k+
P: 31,470
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.
1 Week Ago #7

Expert Mod 15k+
P: 31,470
To give more help on Twinny's suggestion you may find Combining Rows-Opposite of Union helpful.
1 Week Ago #8

Post your reply

Sign in to post your reply or Sign up for a free account.