Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Crosstab SQL statement from two tables?
Message
From
20/07/2005 05:58:50
 
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Environment versions
Visual FoxPro:
VFP 9
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01033907
Message ID:
01034158
Views:
17
Thank you Sergey. This is essentially what I thought would be involved, but your precise syntax is very helpful.

Andrew

>It can be done with a query only if you know in advance all possible values of descrip and code them into SELECT using IIF(). Something like
SELECT t1.*, ;
>    IIF(t2.descrip = "A1", qty, 00000) as qty_a1, ;
>    IIF(t2.descrip = "b1", qty, 00000) as qty_b1, ;
>    ...
>  FROM table1 t1 ;
>  JOIN table2 t2 on t1.batchid = t2.batchid ;
>  GROUP BY 1,2,3
>
You can also build such query dynamicaly based on the list of descrip values in the table2.
>
>>Given the following data structure and relationship, is there a SQL statement that will allow me to extract a crosstab of the QTY field?
>>
>>Table 1.
>>
>>batchid
>>bincount
>>variety
>>
>>Table 2.
>>
>>batchid
>>descrip
>>qty
>>
>>The tables are related on batchid.
>>
>>The desired output format would have a column for each instance of a descrip while summing the qty for that descrip.
>>
>>Thank you for any pointers.
>>
>>Andrew
On a CLEAR day you can see forever.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform