473,403 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Concatenate values and group by sectionID

tuxalot
200 100+
New to Access so I hope you can help point me in the right direction. Trying to get my head around this.

I have data like so:

Question level1 | Question level2 | SectionID | Question text | Response
1 |a | 1 | what color is the sky? | Blue
1 |b | 1 | what do you like about school? | Lunch
2 | 1 | what is your favorite sitcom? | Seinfeld
4 | 2 | Who makes the best hamburgers? | McDonalds
5 | 2 | What is your favorite type of car? | Chevrolet

Recordsource for the report is a crosstab query. Report is grouped on SectionID and I have an unbound textbox "txtResponseAggregate" at the SectionID footer. I would like to show this in txtResponseAggregate:

In the section 1 footer:
1a) Blue; 1b) Lunch; 2 Seinfeld

In the Section 2 footer:
4 McDonalds; 5 Chevrolet

Note: sometimes the SectionID's are not contiguous (depends on data) i.e. might have section 1,2,3,5,10, etc.

I found this http://allenbrowne.com/func-concat.html but I do not see how I can pass the SectionID as a filter. Do I use a sub-report?

Thanks in advance.
Aug 23 '11 #1
14 6484
NeoPa
32,556 Expert Mod 16PB
You may find Listing Items Within GROUP BY Set helpful. It's not an approach I'd generally recommend (as explained within the linked threads) but if you must do it then they explain how.
Aug 23 '11 #2
tuxalot
200 100+
Thanks NeoPa. I will study those solutions and post back if(when) I get stuck.

Cheers.
Aug 23 '11 #3
tuxalot
200 100+
NeoPa - thanks for pointing me to the code referenced. It works fine however it generates a data type mismatch in criteria expression if "Response" if left blank (which sometimes it will be). Any solutions for this?
Aug 23 '11 #4
patjones
931 Expert 512MB
Hi tuxalot,

You're basically looking to show the answer key for each section's question in the footer for that particular section yes?

Pat
Aug 23 '11 #5
tuxalot
200 100+
zepphead80 - precisely :) Best is if I could have it formatted as per my original post.
Aug 23 '11 #6
patjones
931 Expert 512MB
Hi,

I'm not entirely sure that using a GROUP BY clause is going to get you anywhere here, because SQL doesn't contain aggregate functions for strings. I did find an article for doing this in T-SQL:T-SQL String Aggregate in SQL Server, which doesn't really help you.

I'm going to think about this a little. I feel somehow that it's possible.

Pat
Aug 23 '11 #7
patjones
931 Expert 512MB
To clarify my statement in the last post, consider a really simple table with some integer values versus alphanumerical codes:

code value
A1 3
A1 8
B3 2
B3 9
B3 5

You can get the total value for each alphanumerical code by doing this:

Expand|Select|Wrap|Line Numbers
  1. SELECT code, SUM(value) AS total_value
  2. FROM tbl
  3. GROUP BY code;

which would produce:

code total_value
A1 11
B3 16

However, as far as I can tell, there is no function such as SUM for strings. Perhaps one of the other contributors can correct me if I'm wrong.

Pat
Aug 23 '11 #8
tuxalot
200 100+
The code is almost there. I added QstnLvl1 & QstnLvl2 as an expression in the underlying query.

One adjustment I would like to figure out is how to order the resulting sql data.

Take this simple example:

SectionID data
01 1
01 2
01 3
03 1
03 2

The SQL datasheet might show:

SectionID Data
01 3,2,1
03 1,2

Is there a way to sort this like
01 1,2,3
03 1,2

I've sorted the underlying query data...does not seem to matter. I think it has something to do with how the function loops.
Aug 23 '11 #9
patjones
931 Expert 512MB
Hi,

I wasn't sure whether you wanted to delve into VBA or not so I tried to determine a SQL-only solution. I'm glad the VBA function works for you.

If you call the SQL in your previous post "qryConcat", you can reference it in a DLookup call as follows:

Expand|Select|Wrap|Line Numbers
  1. txtFooter = DLookup("RspnsComments", "qryConcat", "[SectionID] = '" & Me![txtResponseAggregate] & "'")

RspnsComments is the thing that you want DLookup to pull, so that goes in the first argument. Notice that I wrapped [txtResponseAggregate] in '' signs, because SectionID appears to be a string-valued quantity.

Pat
Aug 24 '11 #10
NeoPa
32,556 Expert Mod 16PB
A fair bit to cover here.

@Pat
You are correct as far as there being no such SQL function available to accomplish string aggregation within a GROUP BY. The threads linked to in post #2 though, show how a function can be designed and used to accomplish this.

@Tux
You comment about the function being unable to handle blank (Null) values. This is easy enough to code around once you decide how you want them to be handled. The Nz() function enables you to convert the Null into a string value and the routine can be coded either to ignore these or treat them normally as a value, thus producing a pair of commas (,) in the result.

You also talk about sorting the result set. I think you're missing the point that sorting is done after the SELECTing. I expect if you produce a subquery that is sorted then process that data using the Concat() function, you may get what you require.
Aug 24 '11 #11
tuxalot
200 100+
@Pat
Thanks for the DLOOKUP. This idea worked fine. My final code was:
Expand|Select|Wrap|Line Numbers
  1. =DLookUp("[qtestConcatComments]![RspnsComments]","qtestConcatComments","[qtestConcatComments]![SectionID]=" & [SectionID])
@ NeoPa
Adding the Nz() function throws an error - wrong number of arguments. Here's the code:
Expand|Select|Wrap|Line Numbers
  1. SELECT qselIndRespWithSurveyName.SectionID, Max(Concat([SectionID],[RspnsComment2], Nz([RspnsComment2], 'Not Found'))) AS RspnsComments
  2. FROM qselIndRespWithSurveyName
  3. GROUP BY qselIndRespWithSurveyName.SectionID;
.

For the sorting, the referenced input query above produces the data sorted as I would hope to see. During the Concat() it seems to be getting a bit mixed up. I've re-built the table referenced by the query above and made sure the data is organized in my preferred order along with the primary key (autonumber). I guess SQL processes data without following any sort order within Access.
Aug 24 '11 #12
NeoPa
32,556 Expert Mod 16PB
@Tux
That only happens if you don't follow the instructions correctly.

I'll post the SQL you should have got :
Expand|Select|Wrap|Line Numbers
  1. SELECT   [SectionID]
  2.        , Max(Concat([SectionID], Nz([RspnsComment2], 'Not Found'))) AS RspnsComments
  3. FROM     [qselIndRespWithSurveyName]
  4. GROUP BY [SectionID]
On the plus side, using 'Not Found' in place of an empty string is fine.

I'll also delete the post in the other thread as it's not really an addition to the thread and posting the same comment in two threads isn't allowed ;-)
Aug 24 '11 #13
NeoPa
32,556 Expert Mod 16PB
Tuxalot:
For the sorting, the referenced input query above produces the data sorted as I would hope to see. During the Concat() it seems to be getting a bit mixed up. I've re-built the table referenced by the query above and made sure the data is organized in my preferred order along with the primary key (autonumber). I guess SQL processes data without following any sort order within Access.
Concat() has nothing to do with it. It processes each record as the data is passed. The mucking-up is done by Access before calling the procedure.

Access sometimes orders incoming data to suit itself. Sorting only really has any effect on data as it's output (Sorry for the bum info earlier). If you're interested in sorting the resultant string you could convert the string into array values (Using Split()) and implement something like the procedure described in Sorting Arrays With QSort.
Aug 24 '11 #14
tuxalot
200 100+
Yes, good I was watching both threads. Sorry about that. And again, thanks NeoPa for the assistance.
Aug 24 '11 #15

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

Similar topics

5
by: Sunil Gosavi | last post by:
I have to write a master detail query, in which the detail record should be stored in a variable as Comma Seperated Values. Can anyone help me...... Sun *** Sent via Developersdex...
0
by: Michelle | last post by:
Hi all I have a form with two subforms The main form has a combo box which lists ShiftPatternID. When user selects from combo first subform shows shift pattern for that ID, ie user selects,...
1
by: David Horowitz | last post by:
Hi folks. I need to create a report that has a Group Header that pulls certain data from the Detail section. It's something like this: +--Report---------------------------------------- |...
6
by: Sheldon | last post by:
Hi, I am trying to build a large array using concatenate function in python. So as I loop over the number of arrays, of which there are 12 (4 down and 3 across), I create 3 long arrays by...
1
by: pobnospam | last post by:
I need to loop thru a table and concatenate values from a field called "items" and place in a new table with the concatenated values displaying in one record (grouped by order#). Any ideas ? ...
12
by: parth | last post by:
Hi I want to achieve the following transformation of data using a stored procedure. Source col1 col2(varchar) -------------------------
2
by: Garimella | last post by:
Hi Experts. Need a help in writing a query. Here is the scenario. table has three columns : Emp Id, Book Name, Book Number sample data is...... 1 ten 1 1 two 2 1 Thr 3
1
by: avanthika | last post by:
hi, i want to concatenate values of three column in one row for example my data is: Binoy> select * from fpfm_nominies where emp_no='1283L'; EMP_NO NOMINEE -------...
10
by: Aaron Hoffman | last post by:
Hello, I'm hoping someone might be able to offer some guidance to my problem. I have one query in MS Access which consists of 2 tables joined by a SEQUENCE_ID. By joining the two tables I am...
2
by: MoroccoIT | last post by:
I am saving a value ID using sessions on this feedback Form, but at times, if the visitor takes too long to type a a feedback, the session times out and the user received an error message. Please...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
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,...
0
jinu1996
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.