473,394 Members | 1,738 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,394 software developers and data experts.

Group By Combination on Multiple Rows

Hi,

I have a scenario like so:

TblOne
--------------------------------------------
ElementID | Audit_Nbr
---------------------------------------------
F5000000570 73459
F5000000568 73459
F5000000567 73471
F5000000568 305995
F5000000573 305995
F5000000575 305995
F5000000569 305995
F5000000569 313027
F5000000567 313027
I need to group by Audit_Nbr, then for each distinct audit number
combine the Element ID's in a SORTED comma separated text field. NOTE:
One Audit Number can have 1-many ElementID

Output should be something like,

----------------------------------------------------------------------
Audit_Nbr | SORTED_ElementID_Combination
----------------------------------------------------------------------
73459 | F5000000568, F5000000570
73471 | F5000000567
305995 | F5000000568, F5000000569, F5000000573, F5000000575
313027 | F5000000567, F5000000569
Thanks in advance

Oct 28 '06 #1
3 2010
ja********@gmail.com wrote:
Hi,

I have a scenario like so:

TblOne
--------------------------------------------
ElementID | Audit_Nbr
---------------------------------------------
F5000000570 73459
F5000000568 73459
F5000000567 73471
F5000000568 305995
F5000000573 305995
F5000000575 305995
F5000000569 305995
F5000000569 313027
F5000000567 313027
I need to group by Audit_Nbr, then for each distinct audit number
combine the Element ID's in a SORTED comma separated text field. NOTE:
One Audit Number can have 1-many ElementID

Output should be something like,

----------------------------------------------------------------------
Audit_Nbr | SORTED_ElementID_Combination
----------------------------------------------------------------------
73459 | F5000000568, F5000000570
73471 | F5000000567
305995 | F5000000568, F5000000569, F5000000573, F5000000575
313027 | F5000000567, F5000000569
Thanks in advance
There is no standard query that can produce that output from your data. You
would need a custom VBA function that will create the comma separated list using
an Audit_Nbr as an input. Then you could build a query using that function.

SELECT DISTINCT Audit_Nbr, FunctionName(Audit_Nbr) AS Elements
FROM TblOne

--
Rick Brandt, Microsoft Access MVP
Email (as appropriate) to...
RBrandt at Hunter dot com
Oct 28 '06 #2
"Rick Brandt" <ri*********@hotmail.comwrote in
news:tm***************@newssvr11.news.prodigy.com:
ja********@gmail.com wrote:
>Hi,

I have a scenario like so:

TblOne
--------------------------------------------
ElementID | Audit_Nbr
---------------------------------------------
F5000000570 73459
F5000000568 73459
F5000000567 73471
F5000000568 305995
F5000000573 305995
F5000000575 305995
F5000000569 305995
F5000000569 313027
F5000000567 313027
I need to group by Audit_Nbr, then for each distinct audit
number combine the Element ID's in a SORTED comma separated
text field. NOTE: One Audit Number can have 1-many ElementID

Output should be something like,

--------------------------------------------------------------
>-------- Audit_Nbr | SORTED_ElementID_Combination
--------------------------------------------------------------
>-------- 73459 | F5000000568, F5000000570
73471 | F5000000567
305995 | F5000000568, F5000000569, F5000000573,
F5000000575 313027 | F5000000567, F5000000569
Thanks in advance

There is no standard query that can produce that output from
your data. You would need a custom VBA function that will
create the comma separated list using an Audit_Nbr as an
input. Then you could build a query using that function.

SELECT DISTINCT Audit_Nbr, FunctionName(Audit_Nbr) AS Elements
FROM TblOne
That custom function is fConcatChild() by Dev Ashsh, available
at http://www.mvps.org/access/modules/mdl0004.htm and other
places.
--
Bob Quintal

PA is y I've altered my email address.

--
Posted via a free Usenet account from http://www.teranews.com

Oct 28 '06 #3
I've started to write my own custom code, however, I find that the
Find/FindNext and NoMatch doesn't seem to work as I expected. If you
can see any errors in my code please let me know.

I am expecting a pointer to move the current recordset pointer as it
FindFirst on the first criteria and then subsequent FindNext.
here is my test db I'm using to create this code.

Audit_Nbr | Element ID
---------------------------------
629 F046
130 F567
130 F569
615 F576
396 F576
396 F577
396 F578
629 F622

Public Function CombineElementIDgroupbyAuditNbr()

Dim dbRef As Database
Dim rsRef, rsIn As Recordset
Dim rsOut As Recordset
Dim ChargeAttrRelCombo As String

DoCmd.SetWarnings (False)
DoCmd.RunSQL ("DELETE * FROM ref_ElementID_combined_by_AuditNbr")

Set dbRef = OpenDatabase("L:\Mapping\Legacy Data Extracts.mdb")
Set rsRef = dbRef.OpenRecordset("SELECT Audit_Nbr, Count([Element
ID]) AS Count " & _
"FROM [fakePDDBLoad] " & _
"GROUP BY Audit_Nbr")

Set rsIn = dbRef.OpenRecordset("SELECT Audit_Nbr, [Element ID] " &
_
"FROM [fakePDDBLoad] " & _
"ORDER BY Audit_Nbr, [Element ID]")

Set rsOut =
CurrentDb().OpenRecordset("ref_ElementID_combined_ by_AuditNbr")

Do While Not rsRef.EOF

rsIn.FindFirst (rsIn![Audit_Nbr] = rsRef![Audit_Nbr])

ChargeAttrRelCombo = rsIn![Element ID]

rsIn.FindNext (rsIn![Audit_Nbr] = rsRef![Audit_Nbr])

While rsIn.NoMatch = False
ChargeAttrRelCombo = ChargeAttrRelCombo + ", " +
rsIn![Element ID]
rsIn.FindNext (rsIn![Audit_Nbr] = rsRef![Audit_Nbr])
Wend

MsgBox (ChargeAttrRelCombo)

rsRef.MoveNext
Loop
End Function

Oct 30 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mike Nolan | last post by:
I notice that 7.4 doesn't do default ordering on a 'group by', so you have to throw in an 'order by' clause to get the output in ascending group order. Is this something that most RDB's have...
2
by: Chris | last post by:
Hello all, I'm having some trouble setting up a query. Background: The table TBLSCREEN stores data about screenings of patients/subjects for eligibility to participate in a health study. A...
8
by: | last post by:
hi, i have a table( tblUpdate) with the following fields UpdateID, UpdateDate, ActionID UpdateID is the primary key ActionID is a foreign key i want to find the latest (by date) update...
5
by: jacob.dba | last post by:
I have a table with first name, last name, SSN(social security number) and other columns. I want to assign group number according to this business logic. 1. Records with equal SSN and (similar...
7
by: Jeff Kish | last post by:
Hi. I have read lots, but obviously not the right stuff. A reference pointer would be fine, or an answer. I in fact got an oracle solution that I am still researching, but was wondering about...
6
by: syvman | last post by:
Hi all... I have a quick question, if anyone knows the answer. I've built a query which looks at a set of records in a table (tblFilename) and I've set it up so that it displays the following...
0
by: jason.teen | last post by:
Hi, I have a scenario like so: TblOne -------------------------------------------- ElementID | Audit_Nbr --------------------------------------------- F5000000570 73459...
3
by: Danny J. Lesandrini | last post by:
-- previously posted on newsgroup :: <microsoft.public.access-- Has anyone else noticed this behavior? Focus jumping from current cell to upper left in embedded datasheet. If not, would you be...
3
by: rmotwani | last post by:
Hi, Everybody ! I have a small problem. If I run a query with group by having two outer joins, there, group by is not working i suppose. Case: table 1 : Product master having product...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
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...
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
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...
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...

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.