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

Combining fields in SQL statement -- not sure how to do this

Hi all,

We're importing data from a propriatery database, and below is a snippet of
several lines:

DateTime AcctNumber PtName Notes
12-23-2003 00432234 Smith, John Patient arrived from
12-23-2003 00432234 Smith, John ER with broken leg
12-23-2003 00432234 Smith, John and was admitted to
12-23-2003 00432234 Smith, John room 204.
12-24-2003 00432344 Thompson, Mike Patient sent by
12-24-2003 00432344 Thompson, Mike Primary Care physician
12-24-2003 00432344 Thompson, Mike and is room 205
(this is dummy data of course)

What I need is to group by DateTime, AcctNumber, and PtName and combine Notes
into one field, like this:

DateTime AcctNumber PtName Notes
12-23-2003 00432234 Smith, John Patient arrived from ER with broken leg
and was admitted to room 204.
12-24-2003 00432344 Thompson, Mike Patient sent by Primary Care
physician and is room 205

So in this example instead of 7 rows I'd only have two rows. I've played with
looping and several other methods, but nothing works. Can someone make a
suggestion?

Thanks. Oh, and this is on MS Access 2000 with all updates running on MS
Windows 2000 Pro.

Alex.
Nov 12 '05 #1
3 1290
Alex wrote:
Hi all,

We're importing data from a propriatery database, and below is a snippet
of several lines:

DateTime AcctNumber PtName Notes
12-23-2003 00432234 Smith, John Patient arrived from
12-23-2003 00432234 Smith, John ER with broken leg
12-23-2003 00432234 Smith, John and was admitted to
12-23-2003 00432234 Smith, John room 204.
12-24-2003 00432344 Thompson, Mike Patient sent by
12-24-2003 00432344 Thompson, Mike Primary Care physician
12-24-2003 00432344 Thompson, Mike and is room 205
(this is dummy data of course)

What I need is to group by DateTime, AcctNumber, and PtName and combine
Notes into one field, like this:

DateTime AcctNumber PtName Notes
12-23-2003 00432234 Smith, John Patient arrived from ER with broken leg and was admitted to room 204.
12-24-2003 00432344 Thompson, Mike Patient sent by Primary Care physician and is room 205

So in this example instead of 7 rows I'd only have two rows. I've
played with looping and several other methods, but nothing works. Can
someone make a suggestion?

Thanks. Oh, and this is on MS Access 2000 with all updates running on
MS Windows 2000 Pro.

Alex.


The page I sent didn't format properly (too many tabs I guess). Below is what I want the result table to look like

DateTime AcctNumber PtName Notes
12-23-2003 00432234 Smith, John Patient arrived from ER with broken leg and was admitted to room 204.
12-24-2003 00432344 Thompson, Mike Patient sent by Primary Care physician and is room 205

Thanks...

Alex.
Nov 12 '05 #2
Hi Alex,

This should do it, I think ...

Design a form based on the table that has the repeated data, and create a
command button on it named "cmdMergeMemo"
Copy and paste this code into it's "On Click" event procedure.

Notice that the form's navigation bar displays your record count = 7 before
you press the button, but only shows 2 records (with the complete memos)
after.

************************************************
Private Sub cmdMergeMemo_Click()
On Error Resume Next

Dim rst As DAO.Recordset
Set rst = Me.RecordsetClone

Dim strUnique As String
Dim strCompare
Dim strMemo As String
Dim i As Integer

With rst
.MoveLast
.MoveFirst

For i = 0 To .RecordCount - 1
'Get the values for the current record
strUnique = ""
strUnique = Nz(!DateTime, "Null") & Nz(!AcctNumber, "Null")

'1.) Store the contents of the memo field in the "strMemo" variable.
If Len(strMemo) > 0 Then
strMemo = strMemo & " " & Trim(!Notes)
Else
strMemo = Trim(!Notes)
End If

.MoveNext 'Move to the next record (momentarily) to test unique
values
strCompare = ""
strCompare = Nz(!DateTime, 0) & Nz(!AcctNumber, 0)
'---------------------------------------------------------------------

If strCompare = strUnique Then 'If the unique values ARE the same...
'2.) Delete the record 'cuz we know its repeated in the next record.

'---------------------------------------------------------------------

.MovePrevious
.Delete

'---------------------------------------------------------------------
Else 'If they're NOT the same....

'---------------------------------------------------------------------
'3.)Now that we are in the last record conaining the unique
value...
' Write the contents of the variable to the memo field.

.MovePrevious
.Edit
!Notes = strMemo
.Update
strMemo = ""
End If

.MoveNext

Next i
End With

Set rst = Nothing
strCompare = ""
End Sub
************************************************

--
HTH,
Don
=============================
Use My*****@Telus.Net for e-mail
Disclaimer:
Professional PartsPerson
Amateur Database Programmer {:o)

I'm an Access97 user, so all posted code
samples are also Access97- based
unless otherwise noted.

Do Until SinksIn = True
File/Save, <slam fingers in desk drawer>
Loop

================================
"Alex" <al**@totallynerd.com> wrote in message
news:Mu********************@sysmatrix.net...
Hi all,

We're importing data from a propriatery database, and below is a snippet of several lines:

DateTime AcctNumber PtName Notes
12-23-2003 00432234 Smith, John Patient arrived from
12-23-2003 00432234 Smith, John ER with broken leg
12-23-2003 00432234 Smith, John and was admitted to
12-23-2003 00432234 Smith, John room 204.
12-24-2003 00432344 Thompson, Mike Patient sent by
12-24-2003 00432344 Thompson, Mike Primary Care physician
12-24-2003 00432344 Thompson, Mike and is room 205
(this is dummy data of course)

What I need is to group by DateTime, AcctNumber, and PtName and combine Notes into one field, like this:

DateTime AcctNumber PtName Notes
12-23-2003 00432234 Smith, John Patient arrived from ER with broken leg
and was admitted to room 204.
12-24-2003 00432344 Thompson, Mike Patient sent by Primary Care
physician and is room 205

So in this example instead of 7 rows I'd only have two rows. I've played with looping and several other methods, but nothing works. Can someone make a
suggestion?

Thanks. Oh, and this is on MS Access 2000 with all updates running on MS
Windows 2000 Pro.

Alex.

Nov 12 '05 #3
Hi again,

Oops!
Add a "Me.Requery" just before that "End Sub" :-)

i.e. :
...
Set rst = Nothing
strCompare = ""
Me.Requery
End Sub

Don
"Don Leverton" <le****************@telusplanet.net> wrote in message
news:x5Eic.4268$en3.1404@edtnps89...
Hi Alex,

This should do it, I think ...

Design a form based on the table that has the repeated data, and create a
command button on it named "cmdMergeMemo"
Copy and paste this code into it's "On Click" event procedure.

Notice that the form's navigation bar displays your record count = 7 before you press the button, but only shows 2 records (with the complete memos)
after.

************************************************
Private Sub cmdMergeMemo_Click()
On Error Resume Next

Dim rst As DAO.Recordset
Set rst = Me.RecordsetClone

Dim strUnique As String
Dim strCompare
Dim strMemo As String
Dim i As Integer

With rst
.MoveLast
.MoveFirst

For i = 0 To .RecordCount - 1
'Get the values for the current record
strUnique = ""
strUnique = Nz(!DateTime, "Null") & Nz(!AcctNumber, "Null")

'1.) Store the contents of the memo field in the "strMemo" variable. If Len(strMemo) > 0 Then
strMemo = strMemo & " " & Trim(!Notes)
Else
strMemo = Trim(!Notes)
End If

.MoveNext 'Move to the next record (momentarily) to test unique values
strCompare = ""
strCompare = Nz(!DateTime, 0) & Nz(!AcctNumber, 0)
'---------------------------------------------------------------------

If strCompare = strUnique Then 'If the unique values ARE the same... '2.) Delete the record 'cuz we know its repeated in the next record.
'---------------------------------------------------------------------

.MovePrevious
.Delete

'---------------------------------------------------------------------
Else 'If they're NOT the same....

'---------------------------------------------------------------------
'3.)Now that we are in the last record conaining the unique
value...
' Write the contents of the variable to the memo field.

.MovePrevious
.Edit
!Notes = strMemo
.Update
strMemo = ""
End If

.MoveNext

Next i
End With

Set rst = Nothing
strCompare = ""
End Sub
************************************************

--
HTH,
Don
=============================
Use My*****@Telus.Net for e-mail
Disclaimer:
Professional PartsPerson
Amateur Database Programmer {:o)

I'm an Access97 user, so all posted code
samples are also Access97- based
unless otherwise noted.

Do Until SinksIn = True
File/Save, <slam fingers in desk drawer>
Loop

================================
"Alex" <al**@totallynerd.com> wrote in message
news:Mu********************@sysmatrix.net...
Hi all,

We're importing data from a propriatery database, and below is a snippet of
several lines:

DateTime AcctNumber PtName Notes
12-23-2003 00432234 Smith, John Patient arrived from
12-23-2003 00432234 Smith, John ER with broken leg
12-23-2003 00432234 Smith, John and was admitted to
12-23-2003 00432234 Smith, John room 204.
12-24-2003 00432344 Thompson, Mike Patient sent by
12-24-2003 00432344 Thompson, Mike Primary Care physician
12-24-2003 00432344 Thompson, Mike and is room 205
(this is dummy data of course)

What I need is to group by DateTime, AcctNumber, and PtName and combine

Notes
into one field, like this:

DateTime AcctNumber PtName Notes
12-23-2003 00432234 Smith, John Patient arrived from ER with broken leg
and was admitted to room 204.
12-24-2003 00432344 Thompson, Mike Patient sent by Primary Care
physician and is room 205

So in this example instead of 7 rows I'd only have two rows. I've played with
looping and several other methods, but nothing works. Can someone make

a suggestion?

Thanks. Oh, and this is on MS Access 2000 with all updates running on MS Windows 2000 Pro.

Alex.


Nov 12 '05 #4

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

Similar topics

8
by: Ilan | last post by:
Hi all I need to add data from two Excel sheets (both on the same workbook) to an existing table in my SQL DB. The problem is that each sheet holds different fields for the same record, though...
4
by: badbetty | last post by:
MS Access 97 query: Given 2 simple tables Person PersonRef (text) (primary key) Surname (text) e.g PersonRef Surname
3
by: Alex | last post by:
Hi all, We're importing data from a propriatery database, and below is a snippet of several lines: DateTime AcctNumber PtName Notes 12-23-2003 00432234 Smith, John Patient arrived from...
5
by: M.Stanley | last post by:
Hi, I'm attempting to create a query that will combine 2 columns of numbers into one. The followng comes from 1 table with 4 fields (A,B,C,D) A B RESULT 700 000 700000 700 001 ...
4
by: Omey Samaroo | last post by:
Dear Access Gurus, Can anyone provide me with some much needed assistance. I would like to combine the contents of 3 text fields into one field. Can someone provide some code or a method to do...
2
by: Will | last post by:
I have a table, tblManinstructions with fields Code & InstructionID, one Code can have many InstructionID. I also have tblinstructions (fields instructionID & instruction). What I want to do is...
4
by: justin tyme | last post by:
Hello Experts! I would like to combine (which may not be the correct technical term) two text fields from the same table in a query. Specifically, text field A and text field B are both lists of...
3
by: John Smith | last post by:
I have two text fields in a table. One is Height, one is width. Some examples of what might be each field: Height Width 35' 35' 8' 6' 4 to 6...
10
by: H | last post by:
Hi, I have the following address fields in a table: flat_number house_name_or_number street village postal_town county postcode
3
by: Ken Fine | last post by:
This is a question that someone familiar with ASP.NET and ADO.NET DataSets and DataTables should be able to answer fairly easily. The basic question is how I can efficiently match data from one...
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
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...
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.