Solved

SQL - transposing data from duplicate records

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
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.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

910 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

16 Experts available now in Live!

Get 1:1 Help Now