Solved

SQL - transposing data from duplicate records

Posted on 2013-06-26
3
266 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

810 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