Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Variable fields to group by in a view?
Message
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Environment versions
Visual FoxPro:
VFP 8 SP1
OS:
Windows XP SP2
Miscellaneous
Thread ID:
01147438
Message ID:
01147714
Views:
16
This message has been marked as the solution to the initial question of the thread.
With a hardcoded field list like you're showing, it won't work. You must specify all non-aggregate fields in the GROUP BY clause. You would need to use expressions that look at the parameters for the field list instead of using them in the WHERE clause. Also, each expression in the WHERE clause is supposed to return a logical, not a string as you're doing. So, I would modify the statement like this:
SELECT IIF(EMPTY(vp_state),'  ',State) as GrpState, ;
       IIF(EMPTY(vp_orderdate),{},OrderDate as GrpOrdDate, ;
       IIF(EMPTY(vp_itemcode),'  ',ItemCode as GrpItmCode, ;
       count(*) ;
   FROM mytable ;
GROUP BY 1,2,3
Note that I've renamed the fields State, OrderDate and ItemCode because sometimes VFP gets messed up if you use the same name as a field in the expression. You should probably also give a name to the count field so you can access it programmatically.

The grouping will work in this case because the fields you're not interested in using for the grouping are all empty, which means they won't affect the order.

>I'm trying to build a view where I can vary the fields I'm grouping on, based on passed parameters.
>
>
>SELECT State, OrderDate, ItemCode, count(*) FROM mytable ;
>WHERE IIF(EMPTY(?vp_state), "Ignore State", "Group by State") ;
>  AND IIF(EMPTY(?vp_orderdate), "Ignore OrderDate", "Group by OrderDate") ;
>  AND IIF(EMPTY(?vp_itemcode), "ItemCode", "Group by ItemCode") ;
>GROUP BY 1,2,3
>
>*Pass State:
>State  OrderDate  ItemCode  Count
>TX     {}         ''        5
>NM     {}         ''        3
>
>*Pass OrderDate:
>State  OrderDate  ItemCode  Count
>''     {7/1/2006} ''        2
>''     {8/1/2006} ''        6
>
>*Pass State and OrderDate:
>State  OrderDate  ItemCode  Count
>TX     {7/1/2006} ''        1
>TX     {8/1/2006} ''        4
>NM     {7/1/2006} ''        1
>NM     {8/1/2006} ''        2
>
>
>Is there a good way to pull this off without multiple views or SQL Passthrough? I've looked at macro substituting WHERE and GROUP BY clauses, but not getting the results I want.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform