Solved

SQL - transposing data from duplicate records

Posted on 2013-06-26
3
267 Views
Last Modified: 2013-06-26
Okay here's what I'm trying to do.

I have a table with the columns

customer_id product_purchased

When I query it comes out like:

1 product1
1 product2
1 product4
2 product3
2 product4
3 product3
3 product2

What I want is:
1 product1, product2, product4
2 product3, product4
3 product3, product2

Any thoughts?  I've been trying this via SQL and Coldfusion, but I'm stumped after 2 days.
0
Comment
Question by:rrattie
3 Comments
 
LVL 39

Accepted Solution

by:
gdemaria earned 350 total points
ID: 39278687
<cfoutput group="customer_id">
  <br>#customer_id#
         <cfoutput>#product_purchased#</cfoutput>
</cfoutput>
0
 
LVL 52

Assisted Solution

by:_agx_
_agx_ earned 150 total points
ID: 39278814
If you want commas in between, you'd need to adjust gdemaria's example slightly. One way is:

<cfoutput query="qry" group="customer_id">
  #customer_id# 
  <cfoutput>
       <cfif currentRow gt 1 and qry.customer_id[currentRow] eq qry.customer_id[currentRow-1]>,</cfif>
      #product_purchased#
  </cfoutput>
  <br>
</cfoutput> 

Open in new window


You could also do it in SQL with XML PATH. But using CF is the simplest

http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/Q_28014813.html#a38837811
0
 
LVL 2

Author Closing Comment

by:rrattie
ID: 39278858
Thank you both!  I had gone down the SQL path and was way over complicating the issue.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.

820 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