SunQuest
           Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old April 30th, 2008, 07:36 AM
scruft scruft is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2005
Posts: 5 scruft User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 57 m 16 sec
Reputation Power: 0
Confusing SQL problem

Hello,

I have a fairly confusing SQL question. I'll explain it as best I can.

I have a table containing a lot of data regarding customers and items they've purchased. A CustomerID can (and does) appear multiple times, as do the ProductIDs. What I'd like to do is run an SQL which will give me results in the following format:

CustomerID ProductID1 ProductID2 ProductID3 ProductID4 ProductID5 ProductID6

Note that each product/customer relationship have one record each, so the SQL is essentially selecting the distinct customerIDs and grouping them, then finding each distinct productID that customer is linked to. Is this possible? So the result set has just one record for each CustomerID?

Any help would be fantastic.
Thanks,
Simon

Reply With Quote
  #2  
Old April 30th, 2008, 12:06 PM
pabloj's Avatar
pabloj pabloj is offline
Modding: Oracle MsSQL Firebird
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Jun 2001
Location: Outside US
Posts: 7,686 pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 5 Days 16 h 27 m 51 sec
Reputation Power: 259
Why dont' you show us the table structure and tell us which database are you using?

Reply With Quote
  #3  
Old April 30th, 2008, 03:51 PM
scruft scruft is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2005
Posts: 5 scruft User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 57 m 16 sec
Reputation Power: 0
The database is NexusDB. I don't think it's particularly common, but t accept most normal SQL scripts.

The table structure is as follows:

Code:
Key	CustomerID	ProductID
1	4221		12876
2	4221		98365
3	4222		67787
4	4221		12876
5	4223		12876
6	4221		67787


And I'd like the results to show like this:

Code:
CustomerID	ProductID1	ProductID2	ProductID3
4221		12876		98365		67787
4222		67787
4223		12876

Reply With Quote
  #4  
Old April 30th, 2008, 04:58 PM
TonyF123 TonyF123 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2007
Location: Purley, Surrey
Posts: 176 TonyF123 User rank is Sergeant (500 - 2000 Reputation Level)TonyF123 User rank is Sergeant (500 - 2000 Reputation Level)TonyF123 User rank is Sergeant (500 - 2000 Reputation Level)TonyF123 User rank is Sergeant (500 - 2000 Reputation Level)TonyF123 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 16 h 8 m 18 sec
Reputation Power: 10
Quote:
Originally Posted by scruft
The database is NexusDB. I don't think it's particularly common, but t accept most normal SQL scripts.

The table structure is as follows:

Code:
Key	CustomerID	ProductID
1	4221		12876
2	4221		98365
3	4222		67787
4	4221		12876
5	4223		12876
6	4221		67787


And I'd like the results to show like this:

Code:
CustomerID	ProductID1	ProductID2	ProductID3
4221		12876		98365		67787
4222		67787
4223		12876


Shame you aren't using MySQL as the group_concat function will do that, but don't think it is available in any other database. If you are outputting using PHP you can emulate it as described in this article.

http://www.outshine.com/blog/2007/0...ncat-in-php.php

Reply With Quote
  #5  
Old May 1st, 2008, 04:53 AM
pabloj's Avatar
pabloj pabloj is offline
Modding: Oracle MsSQL Firebird
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Jun 2001
Location: Outside US
Posts: 7,686 pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 5 Days 16 h 27 m 51 sec
Reputation Power: 259
Quote:
Originally Posted by TonyF123
Shame you aren't using MySQL as the group_concat function will do that, but don't think it is available in any other database. ...
I thought the same about MySQL.
Note that group_concat(), or an equivalent function, is available in many databases, as an example Firebird 2.1 and Sybase support LIST() which works basically the same and PostgreSQL has it's own group_concat() as part of a MySQL compatibility package.

Reply With Quote
  #6  
Old May 1st, 2008, 08:51 PM
scruft scruft is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2005
Posts: 5 scruft User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 57 m 16 sec
Reputation Power: 0
Quote:
Originally Posted by pabloj
I thought the same about MySQL.
Note that group_concat(), or an equivalent function, is available in many databases, as an example Firebird 2.1 and Sybase support LIST() which works basically the same and PostgreSQL has it's own group_concat() as part of a MySQL compatibility package.


I don't suppose you'd know if Nexus has such a function? I looked in the Manual & Reference guide, but couldn't see a mention of it.

As for MySQL, I've not had a great deal of experience with it, but I know I can install an ODBC driver for Nexus, to make it accessible from another DB. How would I go about having MySQL connect to the Nexus DB? Is it fairly straightforward or do I have to run scripts etc? (I'm a big fan of GUIs, not so much CLIs)..

any help is much appreciated, sorry for asking such newbie questions.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > Confusing SQL problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway