dbTalk Databases Forums  

Combining many records into 1

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Combining many records into 1 in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
rdraider
 
Posts: n/a

Default Combining many records into 1 - 07-17-2007 , 04:24 PM






Using SQL 2000, how can you combine multiple records into 1?
The source data is varchar(255), the destination will be text. I need help
with the select statement.

example tables:
CREATE TABLE [NoteHeader] (
[NoteID] [int],
[CustomerID] [int] ,
[Desc1] [varchar] (255),
[Date] [datetime] ,
)
GO

CREATE TABLE [NoteDetail] (
[NoteId] [int],
[SeqNum] [int] NOT NULL ,
[Note1] [varchar] (255),
[Note2] [varchar] (255),
[Note3] [varchar] (255),
[Note4] [varchar] (255),
[Note5] [varchar] (255)
)
GO


Sample script joining tables:
SELECT *
FROM NoteHeader INNER JOIN
NoteDetail ON NoteHeader.NoteID = NoteDetail.NoteId

Sample results:
NoteID CustomerID Desc1 Date
Note1 Note2
.....Note5
1111 987 Note Header Description 2007-07-15
Notes detail record 1 field 1 Notes detail record 1 field2 .....
1111 987 Note Header Description 2007-07-15
Notes detail record 2 field 1 Notes detail record 2 field 2


Desired results:
NoteID CustomerID Desc1 Date
CombinedNotes
1111 987 Note Header Description 2007-07-15
Notes detail record 1 field 1 +

Notes detail record 1 field2 +

Notes detail record 2 field 1 +

Notes detail record 2 field 2 +

through unlimited number of records up to 5
fields each



The NoteID field is the unique number. 1 record per NoteID in NoteHeader,
NoteDetail can have unlimited number of same NoteID (usually not more than
10)








Reply With Quote
  #2  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Combining many records into 1 - 07-17-2007 , 04:49 PM






rdraider (rdraider (AT) sbcglobal (DOT) net) writes:
Quote:
Using SQL 2000, how can you combine multiple records into 1?
The source data is varchar(255), the destination will be text. I need
help with the select statement.
SQL Server MVP Anith Sen as a couple of methods on
http://www.projectdmx.com/tsql/rowconcatenate.aspx.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
  #3  
Old   
rdraider
 
Posts: n/a

Default Re: Combining many records into 1 - 07-18-2007 , 01:50 PM



Thanks for the info. My problem is the resulting data will be too large for
varchar(8000). All these examples seem to use varchar(8000)
I need to convert to a text datatype. I can concat multiple varchar fields
from 1 record into text but the problem is how the source data is
structured.
The source data is from an app called 'Onyx' running SQL 6.5 (I'm naming
names !!). I upgraded the SQL 6.5 to SQL 2000. I don't hav SQL 2005.

There is blog style data in the form of:

NoteID + NoteDetailID + SeqNum + Note1 + Note 2 + Note3 + Note4 + Note 5

NoteID is the same for each complete blog record
NoteDetailID changes for each new entry to the blog
SeqNum ranges from 1 - 20 for each NoteDetailID
The blog entries are stored in Note1 through Note5 - each only varchar(255)
As each Note field fills up, it rolls to Note2, Note3, Note4, Note5, then
creates a new record with same NoteID and NoteDetailID.
A new NoteDetailID is created when a new entry is started (such as a user
adds more to the blog days later)

I assume it was designed this way because SQL 6.5 largest data type was
varchar(255) ?

I have already created a temp table to combine the Note1 + Note 2 + Note3 +
Note4 + Note 5 into 1 varchar(8000) field.
Still looking for a method to properly combine the blog entries to text in
the proper order.

Thanks again for any ideas.

RD

"Erland Sommarskog" <esquel (AT) sommarskog (DOT) se> wrote

Quote:
rdraider (rdraider (AT) sbcglobal (DOT) net) writes:
Using SQL 2000, how can you combine multiple records into 1?
The source data is varchar(255), the destination will be text. I need
help with the select statement.

SQL Server MVP Anith Sen as a couple of methods on
http://www.projectdmx.com/tsql/rowconcatenate.aspx.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx



Reply With Quote
  #4  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Combining many records into 1 - 07-18-2007 , 04:18 PM



rdraider (rdraider (AT) sbcglobal (DOT) net) writes:
Quote:
Thanks for the info. My problem is the resulting data will be too large
for varchar(8000). All these examples seem to use varchar(8000)
I need to convert to a text datatype. I can concat multiple varchar fields
from 1 record into text but the problem is how the source data is
structured.
The source data is from an app called 'Onyx' running SQL 6.5 (I'm naming
names !!). I upgraded the SQL 6.5 to SQL 2000. I don't hav SQL 2005.
I think you have two options:

1) Get SQL 2005.
2) Do it client-side.

I think you can do it on SQL 2000, but then you would have to run
a cursor, and use WRITETEXT and UPDATETEXT and it would be very very
painful. Please don't ask me to write the code for you, but if you
have problems with using WRITETEXT and UPDATETEXT, I can try to assist.

Quote:
I assume it was designed this way because SQL 6.5 largest data type was
varchar(255) ?
Yes, that is correct.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.