SQL-fu : collapsing data

Status
Not open for further replies.
i'm not sure if this belongs in here or other-software, but here goes.

if i have the following de-normalized table:
code:<HR>
Code:
id    name     color     size
-----------------------------
1     shirt    red       M
1     shirt    blue      M
1     shirt    red       L
1     shirt    blue      L
1     shirt    green     XL
2     pants    orange    M
2     pants    blue      M
2     pants    orange    L
2     pants    blue      L
2     pants    orange    XL

how can i run a sql query that outputs the following (note: stored proc is ok, but undesired) (other note: SQL must run on db2, informix AND oracle)

code:<HR>
Code:
id    name     colors             sizes      combos
---------------------------------------------------
1     shirt    red, blue, green   M, L, XL   red$M blue$M red$L etc
2     pants    orange, blue       M, L, XL   orange$M, blue$M  etc

it is mostly a group by id/name but with all the colors and sizes put into a multivalue field and then another field with all the combinations put in from the original table. However, only valid combo's from the previous table are to be entered, i.e. it is not a pure cross product of all the two columns values.
 

Wozster

Smack-Fu Master, in training
56
A standard pl/sql stored function should work on all the platforms you've described.
I've got a few I've written that'll do something very similar to what you're describing if you'd like an example.

(note: stored proc is ok, but undesired)

1. I dont think you have a choice
2. Functions return values/strings, procedures don't
 

sp1t3

Seniorius Lurkius
20
Without using a stored proc or pl/sql, then I'd guess that about the only other way would be to do it thru a series of inline views within the select statement. It could get really messy, and I'm not sure how well the non-Oracle platforms would handle it.

Another alternative would be to use multiple queries and insert some of the results into temporary tables.
 
Status
Not open for further replies.