473,398 Members | 2,125 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,398 software developers and data experts.

concatenating historical records

Is there a way to bring historical data into one field? I have activities
A1, A2, A3 ,etc in a history table. I need to check for the presence of
that data and bring it back if it exists for a record. I know I can do this
using (not tested):

With #temp as (

Select RecordID, Name from Table1 where . . . )

Select

RecordID

, Name

, substr((#A1.Activity || #A2.Activity || #A3.Activity),2) as History

from

#Temp T

left join (

Select ',' || Activity_CD as Activity from

#Temp T INNER JOIN History H on T.RecordID = H. RecordID

WHERE Activity_CD = 'A1') as #A1 on T.RecordID = #A1.RecordID

left join (

Select ',' || Activity_CD as Activity from

#Temp T INNER JOIN History H on T.RecordID = H. RecordID

WHERE Activity_CD = 'A2') as #A2 on T.RecordID = #A2.RecordID

left join (

Select ',' || Activity_CD as Activity from

#Temp T INNER JOIN History H on T.RecordID = H. RecordID

WHERE Activity_CD = 'A3') as #A3 on T.RecordID = #A3.RecordID

I don't have permissions to do a Stored Proc and I have to concatenate about
20 of these things. Is there an easier way so I can result in history only
reading like "A1, A3, A10" ?

TIA

candyman






Oct 27 '06 #1
3 2569
meter_man wrote:
Is there a way to bring historical data into one field? I have activities
A1, A2, A3 ,etc in a history table. I need to check for the presence of
that data and bring it back if it exists for a record. I know I can do
this using (not tested):

With #temp as (
Select RecordID, Name from Table1 where . . . )

Select
RecordID
, Name
, substr((#A1.Activity || #A2.Activity || #A3.Activity),2) as History
from
#Temp T
left join (
Select ',' || Activity_CD as Activity from
#Temp T INNER JOIN History H on T.RecordID = H. RecordID
WHERE Activity_CD = 'A1') as #A1 on T.RecordID = #A1.RecordID
left join (
Select ',' || Activity_CD as Activity from
#Temp T INNER JOIN History H on T.RecordID = H. RecordID
WHERE Activity_CD = 'A2') as #A2 on T.RecordID = #A2.RecordID
left join (
Select ',' || Activity_CD as Activity from
#Temp T INNER JOIN History H on T.RecordID = H. RecordID
WHERE Activity_CD = 'A3') as #A3 on T.RecordID = #A3.RecordID
I don't have permissions to do a Stored Proc and I have to concatenate
about
20 of these things. Is there an easier way so I can result in history
only reading like "A1, A3, A10" ?
Have a look at recursive queries and "Bill of materials" in the manual.
Alternatively, you can use the XMLAGGREGATE function to do the aggregation
from multiple rows into a single one.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Oct 27 '06 #2
Here's an example applying a recursive CTE, using an algorithm from
Molinaro's _SQL Cookbook_ (note: best viewed with fixed font):

WITH
PARTICIPANT_NAME
(
PRTCP_NM_ID,
PRTCP_ID,
FIRST_NM
)
AS
(VALUES (1,1,'JEFF'),
(2,1,'JEFFREY'),
(3,2,'EDDIE'),
(4,2,'EDWARD'),
(5,2,'TED'),
(6,3,'ELIZABETH')
),
PRTCP_NM
(
PRTCP_ID,
CNT,
LIST,
PRTCP_NM_ID,
LEN
)
AS
(
SELECT
PN1.PRTCP_ID,
COUNT(*) OVER (PARTITION BY PN1.PRTCP_ID),
CAST(PN1.FIRST_NM AS VARCHAR(4000)),
PN1.PRTCP_NM_ID,
1
FROM
PARTICIPANT_NAME PN1
UNION ALL
SELECT
PRTCP_NM.PRTCP_ID,
PRTCP_NM.CNT,
PRTCP_NM.LIST||', '||PN2.FIRST_NM,
PN2.PRTCP_NM_ID,
PRTCP_NM.LEN + 1
FROM
PARTICIPANT_NAME PN2,
PRTCP_NM
WHERE
PN2.PRTCP_ID = PRTCP_NM.PRTCP_ID
AND
PN2.PRTCP_NM_ID PRTCP_NM.PRTCP_NM_ID
)
SELECT
LIST
FROM
PRTCP_NM
WHERE
LEN = CNT

and here's a really cool example--from this forum, BTW--of using the
XMLAGG function:

CREATE TABLE EMPLOYEE(NAME VARCHAR(15), DEPT VARCHAR(15));

NAME DEPT
----- ------
MISO SOLUTIONS
JOHN DEVELOPMENT
SERGE SOLUTIONS
LEE L3
MARK ID
JACK L3
LILY QUALITY
BERNI SOLUTIONS
SELECT DEPT,
SUBSTR(NAMES, 1, LENGTH(NAMES) -1)
FROM (SELECT
DEPT,
REPLACE
(REPLACE
(XMLSERIALIZE
(CONTENT XMLAGG(XMLELEMENT(NAME A, NAME)
ORDER BY NAME)
AS VARCHAR(60)), '<A>', ''), '</A>', ',') AS NAMES
FROM EMPLOYEE GROUP BY DEPT) AS X;
DEPT NAMES
----- --------------------
SOLUTIONS BERNI, MISO, SERGE
DEVELOPMENT JOHN
L3 JACK, LEE
ID MARK
QUALITY LILY

HTH,

--Jeff

Knut Stolze wrote:
meter_man wrote:
Is there a way to bring historical data into one field? I have activities
A1, A2, A3 ,etc in a history table. I need to check for the presence of
that data and bring it back if it exists for a record. I know I can do
this using (not tested):

With #temp as (
Select RecordID, Name from Table1 where . . . )

Select
RecordID
, Name
, substr((#A1.Activity || #A2.Activity || #A3.Activity),2) as History
from
#Temp T
left join (
Select ',' || Activity_CD as Activity from
#Temp T INNER JOIN History H on T.RecordID = H. RecordID
WHERE Activity_CD = 'A1') as #A1 on T.RecordID = #A1.RecordID
left join (
Select ',' || Activity_CD as Activity from
#Temp T INNER JOIN History H on T.RecordID = H. RecordID
WHERE Activity_CD = 'A2') as #A2 on T.RecordID = #A2.RecordID
left join (
Select ',' || Activity_CD as Activity from
#Temp T INNER JOIN History H on T.RecordID = H. RecordID
WHERE Activity_CD = 'A3') as #A3 on T.RecordID = #A3.RecordID
I don't have permissions to do a Stored Proc and I have to concatenate
about
20 of these things. Is there an easier way so I can result in history
only reading like "A1, A3, A10" ?

Have a look at recursive queries and "Bill of materials" in the manual.
Alternatively, you can use the XMLAGGREGATE function to do the aggregation
from multiple rows into a single one.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Oct 27 '06 #3
Thanks you both so much. I have never even heard of recursive queries and
XMLAGGREGATE! I will be checking this out.
:)
"jefftyzzer" <je********@sbcglobal.netwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
Here's an example applying a recursive CTE, using an algorithm from
Molinaro's _SQL Cookbook_ (note: best viewed with fixed font):

WITH
PARTICIPANT_NAME
(
PRTCP_NM_ID,
PRTCP_ID,
FIRST_NM
)
AS
(VALUES (1,1,'JEFF'),
(2,1,'JEFFREY'),
(3,2,'EDDIE'),
(4,2,'EDWARD'),
(5,2,'TED'),
(6,3,'ELIZABETH')
),
PRTCP_NM
(
PRTCP_ID,
CNT,
LIST,
PRTCP_NM_ID,
LEN
)
AS
(
SELECT
PN1.PRTCP_ID,
COUNT(*) OVER (PARTITION BY PN1.PRTCP_ID),
CAST(PN1.FIRST_NM AS VARCHAR(4000)),
PN1.PRTCP_NM_ID,
1
FROM
PARTICIPANT_NAME PN1
UNION ALL
SELECT
PRTCP_NM.PRTCP_ID,
PRTCP_NM.CNT,
PRTCP_NM.LIST||', '||PN2.FIRST_NM,
PN2.PRTCP_NM_ID,
PRTCP_NM.LEN + 1
FROM
PARTICIPANT_NAME PN2,
PRTCP_NM
WHERE
PN2.PRTCP_ID = PRTCP_NM.PRTCP_ID
AND
PN2.PRTCP_NM_ID PRTCP_NM.PRTCP_NM_ID
)
SELECT
LIST
FROM
PRTCP_NM
WHERE
LEN = CNT

and here's a really cool example--from this forum, BTW--of using the
XMLAGG function:

CREATE TABLE EMPLOYEE(NAME VARCHAR(15), DEPT VARCHAR(15));

NAME DEPT
----- ------
MISO SOLUTIONS
JOHN DEVELOPMENT
SERGE SOLUTIONS
LEE L3
MARK ID
JACK L3
LILY QUALITY
BERNI SOLUTIONS
SELECT DEPT,
SUBSTR(NAMES, 1, LENGTH(NAMES) -1)
FROM (SELECT
DEPT,
REPLACE
(REPLACE
(XMLSERIALIZE
(CONTENT XMLAGG(XMLELEMENT(NAME A, NAME)
ORDER BY NAME)
AS VARCHAR(60)), '<A>', ''), '</A>', ',') AS NAMES
FROM EMPLOYEE GROUP BY DEPT) AS X;
DEPT NAMES
----- --------------------
SOLUTIONS BERNI, MISO, SERGE
DEVELOPMENT JOHN
L3 JACK, LEE
ID MARK
QUALITY LILY

HTH,

--Jeff

Knut Stolze wrote:
>meter_man wrote:
Is there a way to bring historical data into one field? I have
activities
A1, A2, A3 ,etc in a history table. I need to check for the presence
of
that data and bring it back if it exists for a record. I know I can do
this using (not tested):

With #temp as (
Select RecordID, Name from Table1 where . . . )

Select
RecordID
, Name
, substr((#A1.Activity || #A2.Activity || #A3.Activity),2) as History
from
#Temp T
left join (
Select ',' || Activity_CD as Activity from
#Temp T INNER JOIN History H on T.RecordID = H. RecordID
WHERE Activity_CD = 'A1') as #A1 on T.RecordID = #A1.RecordID
left join (
Select ',' || Activity_CD as Activity from
#Temp T INNER JOIN History H on T.RecordID = H. RecordID
WHERE Activity_CD = 'A2') as #A2 on T.RecordID = #A2.RecordID
left join (
Select ',' || Activity_CD as Activity from
#Temp T INNER JOIN History H on T.RecordID = H. RecordID
WHERE Activity_CD = 'A3') as #A3 on T.RecordID = #A3.RecordID
I don't have permissions to do a Stored Proc and I have to concatenate
about
20 of these things. Is there an easier way so I can result in history
only reading like "A1, A3, A10" ?

Have a look at recursive queries and "Bill of materials" in the manual.
Alternatively, you can use the XMLAGGREGATE function to do the
aggregation
from multiple rows into a single one.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany

Oct 28 '06 #4

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

Similar topics

5
by: Kelvin Chu | last post by:
Hi Group, (Using php5 on apache1) is there an equivalent of .= in PHP? I'm making a program and I have lots of debug statements being added using $debug = $debug . "some text"
2
by: Doug Shokes | last post by:
All, Given multiple records with identical values in all fields except a single varchar field, is there an efficient query that will group the records into a single record and concatenate the...
14
by: foodic | last post by:
i am fresher to C++ programming, and I just want to learn Concatenating Calls, I have written a program, class SetMe { public: void setX(int x) {_x = x;} void setY(int y) {_y = y;} void...
1
by: rottytooth | last post by:
A general data design question: We have data which changes every week. We had considered seperating historical records and current records into two different tables with the same columns, but...
3
by: Erwin | last post by:
I have a work assignment in which I have to put a historical archive within access which can be used for trendlines etc. It contains data about month, service percentages and numbers. Within a...
16
by: Dixie | last post by:
I have a problem using Dev Ashish's excellent module to concatenate the results of a field from several records into one record. I am using the code to concatenate certain awards onto a...
4
by: Juan | last post by:
Does any one know if there are reported bugs when concatenating strings? When debugging each variable has the correct value but when I try to concatenate them some values are missing (I canīt see...
3
by: deltauser2006 | last post by:
My database consists of information which is updated every quarter. Forms will compare data from the present quarter to quarters past. I need a way to make the database save a copy of itself every...
2
by: igendreau | last post by:
I need to have a form where my users can enter job info. One thing they need to enter is "Regular Hours". What I need to do is then calculate "Regular Cost" which =Regular Hours x Regular Rate...
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...
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...
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...

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.