Solved

Combine data from many rows into single row

Posted on 2012-04-12
4
417 Views
Last Modified: 2012-06-27
I have a master table called tbl_Job in which I want to populate a 4000char string, by pulling info from the child table tbl_JobBOM.  There can be up to 99 records in tbl_JobBOM from which several columns need extracted and placed into the parent table in a single row, single column, per PK.


WITH CTE AS 
(
SELECT 
    ItemNo + ' ' +QtyRequired) + ' ' + QtyOpen +  CHAR(13) + CHAR(10) 
  AS CompDetails, 
  
  ROW_NUMBER() OVER (PARTITION BY JobOper ORDER BY ItemNo) AS Seq,
    JobOper FROM tbl_JobBOM 
)  
    
UPDATE b SET ComponentsThisOper = 
LEFT( 
+ (SELECT MAX(CompDetails) FROM CTE WHERE JobOper=b.JobOper AND Seq=1) 
+ (SELECT MAX(CompDetails) FROM CTE WHERE JobOper=b.JobOper AND Seq=2) 
+ (SELECT MAX(CompDetails) FROM CTE WHERE JobOper=b.JobOper AND Seq=3) 
+ (SELECT MAX(CompDetails) FROM CTE WHERE JobOper=b.JobOper AND Seq=4) 
--... 
+ (SELECT MAX(CompDetails) FROM CTE WHERE JobOper=b.JobOper AND Seq=97) 
+ (SELECT MAX(CompDetails) FROM CTE WHERE JobOper=b.JobOper AND Seq=98) 
+ (SELECT MAX(CompDetails) FROM CTE WHERE JobOper=b.JobOper AND Seq=99)
,4000) 

FROM tbl_Job b

Open in new window



The tables are large and the query takes 2 minutes to run.  I'm looking for a better way to do this, that runs much faster and doesn't require the 99 lines of select statements.

Can someone please send a working code snippet to illustrate a better approach?

 I have searched all over Google and have read about pivots, coalesce, etc but am not able to understand how to apply the concepts to this problem.

Thanks in advance
0
Comment
Question by:LovinSpoonful
  • 3
4 Comments
 
LVL 37

Expert Comment

by:momi_sabag
ID: 37840578
try this
WITH CTE AS
(
SELECT
    ItemNo + ' ' +QtyRequired) + ' ' + QtyOpen +  CHAR(13) + CHAR(10)
  AS CompDetails,
 
  ROW_NUMBER() OVER (PARTITION BY JobOper ORDER BY ItemNo) AS Seq,
    JobOper FROM tbl_JobBOM
) ,
cte2 (CompDetails) as (
select CompDetails   from cte where seq between 1 and 99 order by seq for xml path('')
  )
UPDATE b SET ComponentsThisOper =
LEFT( CompDetails,4000)

FROM tbl_Job b
0
 

Author Comment

by:LovinSpoonful
ID: 37849766
I am not able to make this work.

Here is what I can make work:


WITH a AS (
SELECT JobOper,
       ItemNo + ' ' + CONVERT(VARCHAR,QtyRequired) + ' ' + CONVERT(VARCHAR,QtyOpen) +  CHAR(13) + CHAR(10) AS CompDetails,
       ROW_NUMBER() OVER (PARTITION BY JobOper ORDER BY ItemNo) AS Seq
FROM tbl_JobBOM
)  

SELECT CompDetails FROM a WHERE a.Seq between 1 and 99
  and JobOper = '623417.150'
ORDER BY seq FOR XML PATH('')

result:

<CompDetails>abcdef  6 0
</CompDetails>
<CompDetails>ghijklm 3 0
</CompDetails>
<CompDetails>nopqrs 3 0
</CompDetails>


I need to get the JobOper attached to each XML record, then update with a join.
I tried this but it doesn't work:


WITH a AS (
SELECT JobOper,
       ItemNo + ' ' + CONVERT(VARCHAR,QtyRequired) + ' ' + CONVERT(VARCHAR,QtyOpen) +  CHAR(13) + CHAR(10) AS CompDetails,
       ROW_NUMBER() OVER (PARTITION BY JobOper ORDER BY ItemNo) AS Seq
FROM tbl_JobBOM
)  

, b (JobOper, CompDetails) AS (SELECT JobOper, CompDetails FROM a WHERE a.Seq between 1 and 99 ORDER BY seq FOR XML PATH(''))

UPDATE j SET ComponentsThisOper = LEFT(b.CompDetails,4000)
FROM tbl_Job j
INNER JOIN b ON j.JobOper = b.JobOper
WHERE j.JobOper = b.JobOper


the error is:  
Msg 8159, Level 16, State 1, Line 9
'b' has fewer columns than were specified in the column list.
0
 

Accepted Solution

by:
LovinSpoonful earned 0 total points
ID: 38084070
The way this was eventually solved was to create an additional table to post the intermediary data into, the run a second update query.  then we deleted the temporary table.
0
 

Author Closing Comment

by:LovinSpoonful
ID: 38097725
didn't get an answer from the community
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL 2008 SSIS import 11 49
SQL Query 34 79
T-SQL: Nested CASE Statements 4 22
Adding Subtracting values in sql server 5 0
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now