473,379 Members | 1,491 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,379 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 2007
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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 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.