Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Using self-join to merge rows into same column
Message
From
12/07/2007 23:18:52
 
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Environment versions
Visual FoxPro:
VFP 9 SP1
Miscellaneous
Thread ID:
01239953
Message ID:
01240000
Views:
7
Hi Marcel.

What about something liek this?

Create cursor Source (ID i, DataType c(10), DataValue c(20))
Insert into Source values (531, "make", "FORD")
Insert into Source values (531, "color", "BLUE")
Insert into Source values (532, "make", "HONDA")
Insert into Source values (532, "color", "RED")
Insert into Source values (533, "color", "ORANGE")

Select DISTINCT ID, ;
(SELECT DataValue from Source where Source.DataType == "make " and Source.ID == MainTable.ID) as Make, ;
(SELECT DataValue from Source where Source.DataType == "color " and Source.ID == MainTable.ID) as Color ;
from Source as Maintable ;
into cursor Result
By using a select to get all of the values of the DataType column and a little macro substitution or ExecScript, you could even make a generic routine that will add new columns if new DataType's appear :-)

Ian Simcock.


>I have a cursor with 3 fields order_key, lup_type, lup_value.
>The data might look as follows:
>531 make FORD
>531 color BLUE
>532 make HONDA
>532 color RED
>533 color ORANGE (person didn't enter a car make)
>534...etc.
>
>These values were already retrieved from a vehicle_info table and a lookup table.
>
>How can I take these values and combine them so that it will have the following layout:
>
>order_key, veh_make, veh_color
>531, FORD, BLUE
>532, HONDA, RED
>533, , ORANGE
>
>I tried a self join but can't get the list to show unique order_key's.
>
>As always, thanks for your excellent help.
>
>Marcel
Previous
Reply
Map
View

Click here to load this message in the networking platform