If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > concatenation / group by problem

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-10-06, 17:47
Edadcon Edadcon is offline
Registered User
 
Join Date: Oct 2004
Posts: 13
concatenation / group by problem

Hello does anybody knows a simple SQL answer on the following question:
presume the following table:
reference.|.productID..|..description
C1...........|.... 2211.....|....10µF 15V
C3...........|.... 2211.....|....10µF 15V
C6...........|.... 1392.....|....22µF 10V
R3...........|.... 0100.....|....10K
R4...........|.... 0100.....|....10K
R2...........|.... 0130.....|.... 4K7

The result should be...

productID..|.. references..|.. description..|.. amount
2211.........|.. C1,C3........|.. 10µF 15V....|.. 2
1392.........|.. C6.............|.. 22µF 10V....|.. 1
0100.........|.. R3,R4........|.. 10K............|.. 2
0130.........|.. R2.............|.. 4K7............|.. 1


Seems to be an ordenairy SUM and GROUP BY query
but the problem is the concatenation of the references.
I´m not sure this is possible with pure SQL
Who has the solution?

Thanks
Robert

Last edited by Edadcon; 01-10-06 at 19:56.
Reply With Quote
  #2 (permalink)  
Old 01-11-06, 03:59
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
No, this is not generally possible.
But you may get close, e.g. when you have an upper bound on the number of rows per group.
This is actually a FAQ; see also the thread in http://www.dbforums.com/db2/1207101-getting-data-several-rows-onto-one.html#post4507450

Assuming an upper bound of 3, the following would give something close to what you want (where tbl is your table):
Code:
SELECT A.productID, A.description,
       min(
       A.reference ||
       CASE WHEN B.reference IS NULL THEN ' ' ELSE
         ', ' || B.reference ||
         CASE WHEN C.reference IS NULL THEN ' ' ELSE
           ', ' || C.reference END END)
       AS references
FROM   tbl AS A
       LEFT OUTER JOIN tbl AS B
       ON A.productID = B.productID
          AND A.description = B.description
          AND A.reference < B.reference
       LEFT OUTER JOIN tbl AS C
       ON A.productID = C.productID
          AND A.description = C.description
          AND B.reference < C.reference
GROUP BY A.productID, A.description
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/

Last edited by Peter.Vanroose; 01-11-06 at 04:03.
Reply With Quote
  #3 (permalink)  
Old 02-08-06, 14:32
Edadcon Edadcon is offline
Registered User
 
Join Date: Oct 2004
Posts: 13
Hi Peter,

Many thanks for your advice.
This is an SQL solution indeed, but for my application,
where probably 40 references can exist for one component type, it doesnt fit.
I have to search for script or programming solutions.
As the matter of fact Cosmo suggested a VBA script on the Access forum that is worth to take a look at.
I'm not experienced in VBA but I managed to make it work.
Best regards

Robert (edadcon)
Reply With Quote
  #4 (permalink)  
Old 02-08-06, 16:29
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
yes, it's really difficult in general

however, it's trivially easy in MySQL, which has the GROUP_CONCAT function

why did i mention MySQL? because you posted in the non-specific SQL forum without mentioning which database system you're using

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 02-10-06, 18:37
Edadcon Edadcon is offline
Registered User
 
Join Date: Oct 2004
Posts: 13
oops you're perfectly right.
I used access to build this database but we do use MYSQL as well!
I'd better chosen the last one instead. Probably I will switc anyway, it is not so hard to convert the access datyabase to MySQL.
Best regards
Robert (edadcon)
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On